Debian Codegraph @

web/src/app/db.js

raw
1 import Dexie from "dexie";
2 import session from "./Session";
3
4 // Uses Dexie.js
5 // https://dexie.org/docs/API-Reference#quick-reference
6 //
7 // Notes:
8 // - As per docs, we only declare the indexable columns, not all columns
9
10 const createDatabase = (username) => {
11 const dbName = username ? `ntfy-${username}` : "ntfy"; // IndexedDB database is based on the logged-in user
12 const db = new Dexie(dbName);
13
14 db.version(3).stores({
15 subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]",
16 notifications: "&id,sequenceId,subscriptionId,time,new,[subscriptionId+new],[subscriptionId+sequenceId]",
17 users: "&baseUrl,username",
18 prefs: "&key",
19 });
20
21 // When another connection (e.g., service worker or another tab) wants to upgrade,
22 // close this connection gracefully to allow the upgrade to proceed
23 db.on("versionchange", () => {
24 console.log("[db] versionchange event: closing database");
25 db.close();
26 });
27
28 return db;
29 };
30
31 export const dbAsync = async () => {
32 const username = await session.usernameAsync();
33 return createDatabase(username);
34 };
35
36 const db = () => createDatabase(session.username());
37
38 export default db;