2023-05-25 07:36:01 +12:00
|
|
|
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
|
2023-05-25 07:36:01 +12:00
|
|
|
const db = new Dexie(dbName);
|
|
|
|
|
2023-06-15 05:08:35 +12:00
|
|
|
db.version(2).stores({
|
2023-06-08 19:22:56 +12:00
|
|
|
subscriptions: "&id,baseUrl,[baseUrl+mutedUntil]",
|
2023-05-25 07:36:01 +12:00
|
|
|
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 () => {
|
2023-06-14 00:00:51 +12:00
|
|
|
const username = await session.usernameAsync();
|
2023-06-10 06:32:34 +12:00
|
|
|
return createDatabase(username);
|
2023-05-25 07:36:01 +12:00
|
|
|
};
|
|
|
|
|
2023-06-14 00:02:54 +12:00
|
|
|
const db = () => createDatabase(session.username());
|
2023-05-25 07:36:01 +12:00
|
|
|
|
2023-06-10 06:32:34 +12:00
|
|
|
export default db;
|