2020-07-09 22:30:33 +00:00
|
|
|
import * as plugins from './webstore.plugins';
|
|
|
|
|
2020-07-09 23:03:01 +00:00
|
|
|
export interface IWebStoreOptions {
|
|
|
|
storeName: string;
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:03:46 +00:00
|
|
|
export class WebStore<T = any> {
|
2020-07-09 23:03:01 +00:00
|
|
|
public db: plugins.idb.IDBPDatabase;
|
|
|
|
public objectStore: plugins.idb.IDBPObjectStore;
|
|
|
|
public options: IWebStoreOptions;
|
2020-07-09 22:30:33 +00:00
|
|
|
|
2020-07-09 23:03:01 +00:00
|
|
|
|
|
|
|
constructor(optionsArg: IWebStoreOptions) {
|
|
|
|
this.options = optionsArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async init () {
|
|
|
|
this.db = await plugins.idb.openDB(this.options.storeName, 1, {
|
|
|
|
upgrade(db) {
|
|
|
|
db.createObjectStore('keyval');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(key) {
|
|
|
|
return this.db.get('keyval', key);
|
|
|
|
}
|
|
|
|
|
|
|
|
async set(key, val) {
|
|
|
|
return this.db.put('keyval', val, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(key) {
|
|
|
|
return this.db.delete('keyval', key);
|
|
|
|
}
|
|
|
|
|
|
|
|
async clear() {
|
|
|
|
return this.db.clear('keyval');
|
|
|
|
}
|
|
|
|
|
|
|
|
async keys() {
|
|
|
|
return this.db.getAllKeys('keyval');
|
|
|
|
}
|
2020-07-09 22:30:33 +00:00
|
|
|
}
|