webstore/ts/webstiore.classes.webstore.ts
2020-07-11 16:26:35 +00:00

45 lines
959 B
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) {
return this.db.get(this.options.storeName, key);
}
async set(key, val) {
return this.db.put(this.options.storeName, val, key);
}
async delete(key) {
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);
}
}