2023-09-07 14:58:53 +00:00
|
|
|
import * as dexie from "https://esm.sh/dexie@3.2.4";
|
|
|
|
import { NostrEvent, NostrKind, Tag } from "../lib/nostr-ts/nostr.ts";
|
|
|
|
import { EventsAdapter, Indices } from "../database.ts";
|
2023-07-11 09:49:58 +00:00
|
|
|
|
2023-09-07 14:58:53 +00:00
|
|
|
export class DexieDatabase extends dexie.Dexie implements EventsAdapter {
|
|
|
|
// 'events' is added by dexie when declaring the stores()
|
2023-07-11 09:49:58 +00:00
|
|
|
// We just tell the typing system this is the case
|
|
|
|
events!: dexie.Table<NostrEvent>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super("Events");
|
|
|
|
this.version(6).stores({
|
|
|
|
events: "&id, created_at, kind, tags, pubkey", // indices
|
|
|
|
});
|
|
|
|
}
|
2023-09-07 14:58:53 +00:00
|
|
|
filter(f: (e: NostrEvent) => boolean): Promise<NostrEvent[]> {
|
|
|
|
return this.events.filter(f).toArray();
|
|
|
|
}
|
|
|
|
get(keys: Indices) {
|
|
|
|
return this.events.get(keys);
|
|
|
|
}
|
|
|
|
async put(e: NostrEvent<NostrKind, Tag>): Promise<void> {
|
|
|
|
this.events.put(e);
|
|
|
|
}
|
2023-09-12 14:51:27 +00:00
|
|
|
|
|
|
|
async remove(id: string) {
|
|
|
|
this.events.delete(id);
|
|
|
|
}
|
2023-07-11 09:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function NewIndexedDB(): DexieDatabase | Error {
|
|
|
|
try {
|
|
|
|
const db = new DexieDatabase();
|
|
|
|
return db;
|
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|