Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
89875393ef | |||
fe2c6f312b |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/webstore",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.4",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/webstore",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.4",
|
||||
"private": false,
|
||||
"description": "high performance storage in the browser using indexed db",
|
||||
"main": "dist_ts/index.js",
|
||||
|
@ -1,11 +1,24 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as webstore from '../ts/index';
|
||||
|
||||
let testWebstore = new webstore.WebStore();
|
||||
let testWebstore: webstore.WebStore;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testWebstore = new webstore.WebStore();
|
||||
testWebstore = new webstore.WebStore({
|
||||
storeName: 'mystore'
|
||||
});
|
||||
expect(testWebstore).to.be.instanceOf(webstore.WebStore);
|
||||
});
|
||||
|
||||
tap.test('should init the database', async () => {
|
||||
await testWebstore.init();
|
||||
});
|
||||
|
||||
tap.test('should store a value', async () => {
|
||||
await testWebstore.set('testProp1', {
|
||||
wow: 'wowVal'
|
||||
});
|
||||
console.log(JSON.stringify(await testWebstore.get('testProp1')));
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -1,5 +1,44 @@
|
||||
import * as plugins from './webstore.plugins';
|
||||
|
||||
export class WebStore {
|
||||
|
||||
export interface IWebStoreOptions {
|
||||
storeName: string;
|
||||
}
|
||||
|
||||
export class WebStore<T> {
|
||||
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.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');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user