ntfy/web/src/app/db.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

32 lines
866 B
JavaScript
Raw Normal View History

import Dexie from "dexie";
import session from "./Session";
// Uses Dexie.js
// https://dexie.org/docs/API-Reference#quick-reference
//
// Notes:
// - As per docs, we only declare the indexable columns, not all columns
2023-06-10 06:32:34 +12:00
const createDatabase = (username) => {
const dbName = username ? `ntfy-${username}` : "ntfy"; // IndexedDB database is based on the logged-in user
const db = new Dexie(dbName);
2023-06-10 06:32:34 +12:00
db.version(1).stores({
2023-06-08 19:22:56 +12:00
subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]",
notifications: "&id,subscriptionId,time,new,[subscriptionId+new]", // compound key for query performance
users: "&baseUrl,username",
prefs: "&key",
});
return db;
};
2023-06-10 06:32:34 +12:00
export const dbAsync = async () => {
const username = await session.usernameAsync();
2023-06-10 06:32:34 +12:00
return createDatabase(username);
};
2023-06-14 00:02:54 +12:00
const db = () => createDatabase(session.username());
2023-06-10 06:32:34 +12:00
export default db;