webstore/ts/webstiore.classes.webstore.ts
2020-07-11 17:15:24 +00:00

50 lines
1.1 KiB
TypeScript

import * as plugins from './webstore.plugins';
export interface IWebStoreOptions {
dbName: string;
storeName: string;
}
export class WebStore<T = any> {
public db: plugins.idb.IDBPDatabase;
public objectStore: plugins.idb.IDBPObjectStore;
public options: IWebStoreOptions;
constructor(optionsArg: IWebStoreOptions) {
this.options = optionsArg;
}
public async init () {
this.db = await plugins.idb.openDB(this.options.dbName, 1, {
upgrade: (db) => {
db.createObjectStore(this.options.storeName);
},
});
}
async get(key: string) {
return this.db.get(this.options.storeName, key);
}
async check(keyArg: string): Promise<boolean> {
const result = await this.get(keyArg);
return !!result;
}
async set(key: string, val: T) {
return this.db.put(this.options.storeName, val, key);
}
async delete(key: string) {
return this.db.delete(this.options.storeName, key);
}
async clear() {
return this.db.clear(this.options.storeName);
}
async keys() {
return this.db.getAllKeys(this.options.storeName);
}
}