SilverBullet pivot to become an offline-first PWA (#403)
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import Dexie, { Table } from "dexie";
|
||||
import type { KV, KVStore } from "./kv_store.ts";
|
||||
|
||||
export class DexieKVStore implements KVStore {
|
||||
db: Dexie;
|
||||
items: Table<KV, string>;
|
||||
constructor(
|
||||
private dbName: string,
|
||||
private tableName: string,
|
||||
private indexedDB?: any,
|
||||
) {
|
||||
this.db = new Dexie(dbName, {
|
||||
indexedDB,
|
||||
});
|
||||
this.db.version(1).stores({
|
||||
[tableName]: "key",
|
||||
});
|
||||
this.items = this.db.table<KV, string>(tableName);
|
||||
}
|
||||
|
||||
async del(key: string) {
|
||||
await this.items.delete(key);
|
||||
}
|
||||
|
||||
async deletePrefix(prefix: string) {
|
||||
await this.items.where("key").startsWith(prefix).delete();
|
||||
}
|
||||
|
||||
async deleteAll() {
|
||||
await this.items.clear();
|
||||
}
|
||||
|
||||
async set(key: string, value: any) {
|
||||
await this.items.put({
|
||||
key,
|
||||
value,
|
||||
});
|
||||
}
|
||||
|
||||
async batchSet(kvs: KV[]) {
|
||||
await this.items.bulkPut(
|
||||
kvs.map(({ key, value }) => ({
|
||||
key,
|
||||
value,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
async batchDelete(keys: string[]) {
|
||||
await this.items.bulkDelete(keys);
|
||||
}
|
||||
|
||||
async batchGet(
|
||||
keys: string[],
|
||||
): Promise<(any | undefined)[]> {
|
||||
return (await this.items.bulkGet(keys)).map((result) => result?.value);
|
||||
}
|
||||
|
||||
async get(key: string): Promise<any | null> {
|
||||
const result = await this.items.get({ key });
|
||||
return result ? result.value : null;
|
||||
}
|
||||
|
||||
async has(key: string): Promise<boolean> {
|
||||
return await this.items.get({
|
||||
key,
|
||||
}) !== undefined;
|
||||
}
|
||||
|
||||
async queryPrefix(
|
||||
keyPrefix: string,
|
||||
): Promise<{ key: string; value: any }[]> {
|
||||
const results = await this.items.where("key").startsWith(keyPrefix)
|
||||
.toArray();
|
||||
return results.map((result) => ({
|
||||
key: result.key,
|
||||
value: result.value,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
export type KV = {
|
||||
key: string;
|
||||
value: any;
|
||||
};
|
||||
|
||||
/**
|
||||
* An interface to any simple key-value store.
|
||||
*/
|
||||
export interface KVStore {
|
||||
/**
|
||||
* Deletes the value associated with a given key.
|
||||
*/
|
||||
del(key: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Deletes all keys that start with a specific prefix.
|
||||
*/
|
||||
deletePrefix(prefix: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Deletes all keys in the store.
|
||||
*/
|
||||
deleteAll(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Sets the value for a given key.
|
||||
*/
|
||||
set(key: string, value: any): Promise<void>;
|
||||
|
||||
/**
|
||||
* Sets the values for a list of key-value pairs.
|
||||
*/
|
||||
batchSet(kvs: KV[]): Promise<void>;
|
||||
|
||||
/**
|
||||
* Deletes a list of keys.
|
||||
*/
|
||||
batchDelete(keys: string[]): Promise<void>;
|
||||
|
||||
/**
|
||||
* Gets the values for a list of keys.
|
||||
*/
|
||||
batchGet(keys: string[]): Promise<(any | undefined)[]>;
|
||||
|
||||
/**
|
||||
* Gets the value for a given key.
|
||||
*/
|
||||
get(key: string): Promise<any | null>;
|
||||
|
||||
/**
|
||||
* Checks whether a given key exists in the store.
|
||||
*/
|
||||
has(key: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Gets all key-value pairs where the key starts with a specific prefix.
|
||||
*/
|
||||
queryPrefix(keyPrefix: string): Promise<{ key: string; value: any }[]>;
|
||||
}
|
||||
Reference in New Issue
Block a user