fix(core): update
This commit is contained in:
49
ts/webstore.classes.webstore.ts
Normal file
49
ts/webstore.classes.webstore.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user