Compare commits

..

6 Commits

Author SHA1 Message Date
068199b5e9 1.0.8 2020-07-11 17:19:29 +00:00
0eb8193e4a fix(core): update 2020-07-11 17:19:29 +00:00
34e898e0cf 1.0.7 2020-07-11 17:15:25 +00:00
3530e376d6 fix(core): update 2020-07-11 17:15:24 +00:00
ef141659e2 1.0.6 2020-07-11 16:26:36 +00:00
0b3dec53dd fix(core): update 2020-07-11 16:26:35 +00:00
4 changed files with 43 additions and 15 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/webstore", "name": "@pushrocks/webstore",
"version": "1.0.5", "version": "1.0.8",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/webstore", "name": "@pushrocks/webstore",
"version": "1.0.5", "version": "1.0.8",
"private": false, "private": false,
"description": "high performance storage in the browser using indexed db", "description": "high performance storage in the browser using indexed db",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -5,7 +5,8 @@ let testWebstore: webstore.WebStore;
tap.test('first test', async () => { tap.test('first test', async () => {
testWebstore = new webstore.WebStore({ testWebstore = new webstore.WebStore({
storeName: 'mystore' dbName: 'mytest-db',
storeName: 'mytest-store'
}); });
expect(testWebstore).to.be.instanceOf(webstore.WebStore); expect(testWebstore).to.be.instanceOf(webstore.WebStore);
}); });
@ -14,11 +15,32 @@ tap.test('should init the database', async () => {
await testWebstore.init(); await testWebstore.init();
}); });
tap.test('should store a value', async () => { tap.test('should allow storing a string', async () => {
await testWebstore.set('mystring', 'heythere');
expect(await testWebstore.get('mystring')).to.equal('heythere');
});
tap.test('should allow storing an object', async () => {
await testWebstore.set('testProp1', { await testWebstore.set('testProp1', {
wow: 'wowVal' wow: 'wowVal'
}); });
console.log(JSON.stringify(await testWebstore.get('testProp1'))); console.log(JSON.stringify(await testWebstore.get('testProp1')));
}); });
tap.test('should overwrite a value', async () => {
await testWebstore.set('testProp1', {
wow: 'wowVal2'
});
console.log(JSON.stringify(await testWebstore.get('testProp1')));
});
tap.test('should correctly check the existence of keys', async () => {
const resultNotThere = await testWebstore.check('notThere');
const resultThere = await testWebstore.check('testProp1');
expect(resultNotThere).to.be.false;
expect(resultThere).to.be.true;
});
tap.start(); tap.start();

View File

@ -1,6 +1,7 @@
import * as plugins from './webstore.plugins'; import * as plugins from './webstore.plugins';
export interface IWebStoreOptions { export interface IWebStoreOptions {
dbName: string;
storeName: string; storeName: string;
} }
@ -15,30 +16,35 @@ export class WebStore<T = any> {
} }
public async init () { public async init () {
this.db = await plugins.idb.openDB(this.options.storeName, 1, { this.db = await plugins.idb.openDB(this.options.dbName, 1, {
upgrade(db) { upgrade: (db) => {
db.createObjectStore('keyval'); db.createObjectStore(this.options.storeName);
}, },
}); });
} }
async get(key) { async get(key: string) {
return this.db.get('keyval', key); return this.db.get(this.options.storeName, key);
} }
async set(key, val) { async check(keyArg: string): Promise<boolean> {
return this.db.put('keyval', val, key); const result = await this.get(keyArg);
return !!result;
} }
async delete(key) { async set(key: string, val: T) {
return this.db.delete('keyval', key); return this.db.put(this.options.storeName, val, key);
}
async delete(key: string) {
return this.db.delete(this.options.storeName, key);
} }
async clear() { async clear() {
return this.db.clear('keyval'); return this.db.clear(this.options.storeName);
} }
async keys() { async keys() {
return this.db.getAllKeys('keyval'); return this.db.getAllKeys(this.options.storeName);
} }
} }