Added IndexedDB

This commit is contained in:
Dmitriy Shishkov 2021-07-31 21:29:25 +03:00
parent 96f7f8462b
commit 12ce54e95d
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
5 changed files with 27 additions and 1 deletions

View File

@ -22,6 +22,7 @@
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"idb": "^6.1.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"wouter": "^2.7.4"

View File

@ -1,3 +1,5 @@
export const API_URL = process.env.PUBLIC_API_URL;
// export const BASE_URL = process.env.PUBLIC_BASE_URL;
export const CACHE = "v1";
export const DB_NAME = "publite";
export const DB_VERSION = 1;

13
src/serviceWorker/db.ts Normal file
View File

@ -0,0 +1,13 @@
import * as idb from "idb";
import { DB_NAME, DB_VERSION } from "../constants";
import { PubliteDB } from "./schema";
/**
* Opens IndexedDB for interactions
*/
export const openDB = () =>
idb.openDB<PubliteDB>(DB_NAME, DB_VERSION, {
upgrade: (db, oldVersion, _, tsx) => {
if (oldVersion < 1) db.createObjectStore("Books", { keyPath: "hash" });
},
});

View File

@ -1,4 +1,5 @@
import { precache } from "./cache";
import { openDB as createDB } from "./db";
declare const self: ServiceWorkerGlobalScope;
@ -7,4 +8,4 @@ self.addEventListener("install", (event) => {
event.waitUntil(precache());
});
export type {};
self.addEventListener("activate", (event) => event.waitUntil(createDB()));

View File

@ -0,0 +1,9 @@
import { DBSchema } from "idb";
import { BookT } from "../types/book";
export interface PubliteDB extends DBSchema {
Books: {
key: string;
value: BookT;
};
}