Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
995ae20502 | |||
5c805e57a2 | |||
938207f1c5 | |||
e810bd9473 | |||
10d1bb35ce | |||
8e97b51f1f | |||
7159b33425 | |||
5860066621 | |||
fca8c434f6 | |||
6bd3bd4f37 | |||
068199b5e9 | |||
0eb8193e4a |
2165
package-lock.json
generated
2165
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/webstore",
|
||||
"version": "1.0.7",
|
||||
"version": "1.0.13",
|
||||
"private": false,
|
||||
"description": "high performance storage in the browser using indexed db",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -9,19 +9,19 @@
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)",
|
||||
"format": "(gitzone format)"
|
||||
"build": "(tsbuild --web)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tstest": "^1.0.39",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^14.0.20",
|
||||
"tslint": "^6.1.2",
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^14.11.1",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"idb": "^5.0.4"
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
"idb": "^5.0.6"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
|
@ -6,7 +6,7 @@ let testWebstore: webstore.WebStore;
|
||||
tap.test('first test', async () => {
|
||||
testWebstore = new webstore.WebStore({
|
||||
dbName: 'mytest-db',
|
||||
storeName: 'mytest-store'
|
||||
storeName: 'mytest-store',
|
||||
});
|
||||
expect(testWebstore).to.be.instanceOf(webstore.WebStore);
|
||||
});
|
||||
@ -15,16 +15,21 @@ tap.test('should init the database', async () => {
|
||||
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', {
|
||||
wow: 'wowVal'
|
||||
wow: 'wowVal',
|
||||
});
|
||||
console.log(JSON.stringify(await testWebstore.get('testProp1')));
|
||||
});
|
||||
|
||||
tap.test('should overwrite a value', async () => {
|
||||
await testWebstore.set('testProp1', {
|
||||
wow: 'wowVal2'
|
||||
wow: 'wowVal2',
|
||||
});
|
||||
console.log(JSON.stringify(await testWebstore.get('testProp1')));
|
||||
});
|
||||
|
@ -1 +1 @@
|
||||
export * from './webstiore.classes.webstore';
|
||||
export * from './webstore.classes.webstore';
|
||||
|
@ -9,25 +9,33 @@ export class WebStore<T = any> {
|
||||
public db: plugins.idb.IDBPDatabase;
|
||||
public objectStore: plugins.idb.IDBPObjectStore;
|
||||
public options: IWebStoreOptions;
|
||||
|
||||
private initCalled: boolean = false;
|
||||
private readyDeferred = plugins.smartpromise.defer();
|
||||
|
||||
constructor(optionsArg: IWebStoreOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
public async init() {
|
||||
if (this.initCalled) {
|
||||
return this.readyDeferred.promise;
|
||||
}
|
||||
this.initCalled = true;
|
||||
this.db = await plugins.idb.openDB(this.options.dbName, 1, {
|
||||
upgrade: (db) => {
|
||||
db.createObjectStore(this.options.storeName);
|
||||
},
|
||||
});
|
||||
this.readyDeferred.resolve();
|
||||
}
|
||||
|
||||
async get(key: string) {
|
||||
async get(key: string): Promise<T> {
|
||||
await this.init();
|
||||
return this.db.get(this.options.storeName, key);
|
||||
}
|
||||
|
||||
async check(keyArg: string): Promise<boolean> {
|
||||
await this.init();
|
||||
const result = await this.get(keyArg);
|
||||
return !!result;
|
||||
}
|
||||
@ -37,14 +45,17 @@ export class WebStore<T = any> {
|
||||
}
|
||||
|
||||
async delete(key: string) {
|
||||
await this.init();
|
||||
return this.db.delete(this.options.storeName, key);
|
||||
}
|
||||
|
||||
async clear() {
|
||||
await this.init();
|
||||
return this.db.clear(this.options.storeName);
|
||||
}
|
||||
|
||||
async keys() {
|
||||
await this.init();
|
||||
return this.db.getAllKeys(this.options.storeName);
|
||||
}
|
||||
}
|
@ -1,5 +1,11 @@
|
||||
import * as idb from 'idb';
|
||||
// pushrocks scope
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export {
|
||||
idb
|
||||
}
|
||||
smartpromise
|
||||
};
|
||||
|
||||
// thirdparty scope
|
||||
import * as idb from 'idb';
|
||||
|
||||
export { idb };
|
||||
|
Reference in New Issue
Block a user