webstore/ts/webstiore.classes.webstore.ts

45 lines
959 B
TypeScript
Raw Normal View History

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 {
2020-07-11 16:26:35 +00:00
dbName: string;
2020-07-09 23:03:01 +00:00
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 () {
2020-07-11 16:26:35 +00:00
this.db = await plugins.idb.openDB(this.options.dbName, 1, {
upgrade: (db) => {
db.createObjectStore(this.options.storeName);
2020-07-09 23:03:01 +00:00
},
});
}
async get(key) {
2020-07-11 16:26:35 +00:00
return this.db.get(this.options.storeName, key);
2020-07-09 23:03:01 +00:00
}
async set(key, val) {
2020-07-11 16:26:35 +00:00
return this.db.put(this.options.storeName, val, key);
2020-07-09 23:03:01 +00:00
}
async delete(key) {
2020-07-11 16:26:35 +00:00
return this.db.delete(this.options.storeName, key);
2020-07-09 23:03:01 +00:00
}
async clear() {
2020-07-11 16:26:35 +00:00
return this.db.clear(this.options.storeName);
2020-07-09 23:03:01 +00:00
}
async keys() {
2020-07-11 16:26:35 +00:00
return this.db.getAllKeys(this.options.storeName);
2020-07-09 23:03:01 +00:00
}
2020-07-09 22:30:33 +00:00
}