Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
00964a7db1 | |||
b00db37bf4 | |||
995ae20502 | |||
5c805e57a2 | |||
938207f1c5 | |||
e810bd9473 |
5
package-lock.json
generated
5
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/webstore",
|
"name": "@pushrocks/webstore",
|
||||||
"version": "1.0.11",
|
"version": "1.0.14",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -1915,8 +1915,7 @@
|
|||||||
"@pushrocks/smartpromise": {
|
"@pushrocks/smartpromise": {
|
||||||
"version": "3.0.6",
|
"version": "3.0.6",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.6.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.6.tgz",
|
||||||
"integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg==",
|
"integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"@pushrocks/smartpuppeteer": {
|
"@pushrocks/smartpuppeteer": {
|
||||||
"version": "1.0.15",
|
"version": "1.0.15",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/webstore",
|
"name": "@pushrocks/webstore",
|
||||||
"version": "1.0.11",
|
"version": "1.0.14",
|
||||||
"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",
|
||||||
@ -20,6 +20,7 @@
|
|||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"idb": "^5.0.6"
|
"idb": "^5.0.6"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
|
@ -11,10 +11,6 @@ tap.test('first test', async () => {
|
|||||||
expect(testWebstore).to.be.instanceOf(webstore.WebStore);
|
expect(testWebstore).to.be.instanceOf(webstore.WebStore);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should init the database', async () => {
|
|
||||||
await testWebstore.init();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should allow storing a string', async () => {
|
tap.test('should allow storing a string', async () => {
|
||||||
await testWebstore.set('mystring', 'heythere');
|
await testWebstore.set('mystring', 'heythere');
|
||||||
expect(await testWebstore.get('mystring')).to.equal('heythere');
|
expect(await testWebstore.get('mystring')).to.equal('heythere');
|
||||||
|
@ -5,45 +5,60 @@ export interface IWebStoreOptions {
|
|||||||
storeName: string;
|
storeName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WebStore<T> {
|
export class WebStore<T = any> {
|
||||||
public db: plugins.idb.IDBPDatabase;
|
public db: plugins.idb.IDBPDatabase;
|
||||||
public objectStore: plugins.idb.IDBPObjectStore;
|
public objectStore: plugins.idb.IDBPObjectStore;
|
||||||
public options: IWebStoreOptions;
|
public options: IWebStoreOptions;
|
||||||
|
private initCalled: boolean = false;
|
||||||
|
private readyDeferred = plugins.smartpromise.defer();
|
||||||
|
|
||||||
constructor(optionsArg: IWebStoreOptions) {
|
constructor(optionsArg: IWebStoreOptions) {
|
||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async init() {
|
public async init() {
|
||||||
|
if (this.initCalled) {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.initCalled = true;
|
||||||
this.db = await plugins.idb.openDB(this.options.dbName, 1, {
|
this.db = await plugins.idb.openDB(this.options.dbName, 1, {
|
||||||
upgrade: (db) => {
|
upgrade: (db) => {
|
||||||
db.createObjectStore(this.options.storeName);
|
db.createObjectStore(this.options.storeName);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
this.readyDeferred.resolve();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(key: string): Promise<T> {
|
async get(key: string): Promise<T> {
|
||||||
|
await this.init();
|
||||||
return this.db.get(this.options.storeName, key);
|
return this.db.get(this.options.storeName, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
async check(keyArg: string): Promise<boolean> {
|
async check(keyArg: string): Promise<boolean> {
|
||||||
|
await this.init();
|
||||||
const result = await this.get(keyArg);
|
const result = await this.get(keyArg);
|
||||||
return !!result;
|
return !!result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(key: string, val: T) {
|
async set(key: string, val: T) {
|
||||||
|
await this.init();
|
||||||
return this.db.put(this.options.storeName, val, key);
|
return this.db.put(this.options.storeName, val, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(key: string) {
|
async delete(key: string) {
|
||||||
|
await this.init();
|
||||||
return this.db.delete(this.options.storeName, key);
|
return this.db.delete(this.options.storeName, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
async clear() {
|
async clear() {
|
||||||
|
await this.init();
|
||||||
return this.db.clear(this.options.storeName);
|
return this.db.clear(this.options.storeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
async keys() {
|
async keys() {
|
||||||
|
await this.init();
|
||||||
return this.db.getAllKeys(this.options.storeName);
|
return this.db.getAllKeys(this.options.storeName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,11 @@
|
|||||||
|
// pushrocks scope
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
|
export {
|
||||||
|
smartpromise
|
||||||
|
};
|
||||||
|
|
||||||
|
// thirdparty scope
|
||||||
import * as idb from 'idb';
|
import * as idb from 'idb';
|
||||||
|
|
||||||
export { idb };
|
export { idb };
|
||||||
|
Reference in New Issue
Block a user