silverbullet/lib/data/indexeddb_kv_primitives.ts

80 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2024-07-30 23:33:33 +08:00
import type { KV, KvKey } from "../../plug-api/types.ts";
import type { KvPrimitives, KvQueryOptions } from "./kv_primitives.ts";
2024-10-11 21:52:37 +08:00
import {
type IDBPDatabase,
openDB,
} from "https://esm.sh/idb@7.1.1/with-async-ittr";
2023-09-04 03:15:17 +08:00
const sep = "\0";
const objectStoreName = "data";
2023-09-04 03:15:17 +08:00
export class IndexedDBKvPrimitives implements KvPrimitives {
db!: IDBPDatabase<any>;
constructor(
private dbName: string,
) {
}
async init() {
this.db = await openDB(this.dbName, 1, {
upgrade: (db) => {
db.createObjectStore(objectStoreName);
2023-09-04 03:15:17 +08:00
},
});
}
batchGet(keys: KvKey[]): Promise<any[]> {
const tx = this.db.transaction(objectStoreName, "readonly");
2023-09-04 03:15:17 +08:00
return Promise.all(keys.map((key) => tx.store.get(this.buildKey(key))));
}
async batchSet(entries: KV[]): Promise<void> {
const tx = this.db.transaction(objectStoreName, "readwrite");
2023-09-04 03:15:17 +08:00
await Promise.all([
...entries.map(({ key, value }) =>
tx.store.put(value, this.buildKey(key))
),
tx.done,
]);
}
async batchDelete(keys: KvKey[]): Promise<void> {
const tx = this.db.transaction(objectStoreName, "readwrite");
2023-09-04 03:15:17 +08:00
await Promise.all([
...keys.map((key) => tx.store.delete(this.buildKey(key))),
tx.done,
]);
}
async *query({ prefix }: KvQueryOptions): AsyncIterableIterator<KV> {
const tx = this.db.transaction(objectStoreName, "readonly");
2023-09-04 03:15:17 +08:00
prefix = prefix || [];
for await (
const entry of tx.store.iterate(IDBKeyRange.bound(
this.buildKey([...prefix, ""]),
this.buildKey([...prefix, "\uffff"]),
2023-09-04 03:15:17 +08:00
))
) {
yield { key: this.extractKey(entry.key), value: entry.value };
}
}
private buildKey(key: KvKey): string {
for (const k of key) {
if (k.includes(sep)) {
throw new Error(`Key cannot contain ${sep}`);
}
}
return key.join(sep);
}
private extractKey(key: string): KvKey {
return key.split(sep);
}
close() {
this.db.close();
}
}