@sanemethod/idbk
v1.0.0
Published
Simple promise-based key value store, via IndexedDB and idb.js.
Maintainers
Readme
IDBK - A Promise-Based Key-Value Store
IDBK provides a simple wrapper around the promise-based API of idb, intended for handling a simple but regularly-occuring use-case - the use of IndexedDB as a key-value store with a Web Storage API-inspired interface.
Features
- Familiar Interface: Provides an interface familiar to anyone who's used the Web Storage API.
- Promise-Based API: Streamlined interaction with asynchronous IndexedDB operations.
- idb Wrapper: Built on top of idb, meaning you can pull in its more comprehensive suite of promise-based IndexedDB operations if and when needed to supplement your use of IDBK without needing to give up on a Promise-based interface with IndexedDB.
Installation
You can install IDBK via npm:
npm install idbkBasic Usage
import IDBK from "idbk";
const PRODUCT_DB = "_product_db";
const DB_VERSION = 1;
const INDEXES = {
path:'path',
product:'product',
};
/**
* Instantiate IDBK, passing in the name of the database and the database version,
* and pass in schema handler function, which does the necessary in terms of creating or updating the database.
* See https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB#creating_and_structuring_the_store for more details on creating and structuring IndexedDB stores.
*/
this.db = new IDBK(PRODUCT_DB, DB_VERSION, (db, oldV, newV, tx) => {
let store;
try{
store = db.createObjectStore(PRODUCT_DB, {keyPath:'id'});
}catch(e){
store = tx.objectStore(PRODUCT_DB);
}
for (const index of Object.keys(INDEXES)){
try {
store.deleteIndex(index);
}catch(e){}
store.createIndex(index, INDEXES[index]);
}
});
// Get All Values
const all_records = await this.db.getAll();
// Get a value specified by key
const record = await this.db.get('key');
// Set a value
// @NOTE: 'key' and 'value' are not limited to strings, but can be any value supported by IndexedDB.
// See: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Basic_Terminology
await this.db.set('key', 'value');
// Remove a value
await this.db.remove('key');
// Remove all values
await this.db.clear();
// Get all keys as an array
const keys = await this.db.keys();License
The License.txt file contains the details - this project is licensed under the Mozilla Public License, Version 2.0.
