Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
34e898e0cf | |||
3530e376d6 | |||
ef141659e2 | |||
0b3dec53dd | |||
1d5cb914ac | |||
ce7a457509 | |||
89875393ef | |||
fe2c6f312b | |||
905238a962 | |||
e7cd65fbae |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/webstore",
|
"name": "@pushrocks/webstore",
|
||||||
"version": "1.0.2",
|
"version": "1.0.7",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/webstore",
|
"name": "@pushrocks/webstore",
|
||||||
"version": "1.0.2",
|
"version": "1.0.7",
|
||||||
"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",
|
||||||
@ -8,8 +8,8 @@
|
|||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild)",
|
"build": "(tsbuild --web)",
|
||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -1,11 +1,39 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as webstore from '../ts/index';
|
import * as webstore from '../ts/index';
|
||||||
|
|
||||||
let testWebstore = new webstore.WebStore();
|
let testWebstore: webstore.WebStore;
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
testWebstore = new webstore.WebStore();
|
testWebstore = new webstore.WebStore({
|
||||||
|
dbName: 'mytest-db',
|
||||||
|
storeName: 'mytest-store'
|
||||||
|
});
|
||||||
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 store a value', async () => {
|
||||||
|
await testWebstore.set('testProp1', {
|
||||||
|
wow: 'wowVal'
|
||||||
|
});
|
||||||
|
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();
|
||||||
|
@ -1,5 +1,50 @@
|
|||||||
import * as plugins from './webstore.plugins';
|
import * as plugins from './webstore.plugins';
|
||||||
|
|
||||||
export class WebStore {
|
export interface IWebStoreOptions {
|
||||||
|
dbName: string;
|
||||||
|
storeName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WebStore<T = any> {
|
||||||
|
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.dbName, 1, {
|
||||||
|
upgrade: (db) => {
|
||||||
|
db.createObjectStore(this.options.storeName);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(key: string) {
|
||||||
|
return this.db.get(this.options.storeName, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
async check(keyArg: string): Promise<boolean> {
|
||||||
|
const result = await this.get(keyArg);
|
||||||
|
return !!result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async set(key: string, val: T) {
|
||||||
|
return this.db.put(this.options.storeName, val, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(key: string) {
|
||||||
|
return this.db.delete(this.options.storeName, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
async clear() {
|
||||||
|
return this.db.clear(this.options.storeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
async keys() {
|
||||||
|
return this.db.getAllKeys(this.options.storeName);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user