Indexy
IndexedDB little helper.
Mozilla Developers Network IndexedDB documentation:
https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
Importing/Require
CommonJS require
$ npm install indexy
const Indexy = require('indexy')
ES6+ import (ESModule)
$ npm install indexy
import Indexy from 'indexy'
Browser (AMD-module)
$ wget https://gitlab.com/eevargas/indexy/-/archive/<version>/indexy-<version>.[zip|tar.gz|tar.bz2|tar]
# ... extract your file according to the file format
<script src="<js_assets>/indexy-<version>/lib/indexy.js"></script>
Method examples
Initialize
Indexy::constructor(dbName: string, ver: integer, defaultStore: string, onUpgrade: Function): Promise
const myIndexedDB = Indexy(
'myDatabase',
1,
'myObjectStore',
(db) => {
const scripts = db.createObjectStore('myObjectStore', { keyPath: 'objectStoreKey' })
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/createIndex
scripts.createIndex('indexName', 'indexKeyPath', { unique: false })
}
)
Get the parameters used to initialize the database
Indexy::getEnv(): Promise
myIndexedDB
.getEnv()
.then((env) => { console.log(env) })
.catch((err) => { console.error(err.toString()) })
Add a new document to a store
Indexy::add(data: Object, [storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.
myIndexedDB
.add({ objectStoreKey: 'first', indexKeyPath:'first-key', data:[ 1, 2, 3 ] })
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
Update or create a document in a store
Indexy::put(data: Object, [storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.Put will update any existing document with the same key or create a new one.
myIndexedDB
.put({ objectStoreKey: 'first', indexName: 'first-key-updated', data: [1, 2, 3, 4 ] })
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
Get all documents from a store
Indexy::getAll([storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.
myIndexedDB
.getAll()
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
Get all documents keys from a store
Indexy::getAllKeys([storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.
myIndexedDB
.getAllKeys()
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
Get a document from a store by key
Indexy::getByKey(indexKey: String, [storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.
myIndexedDB
.getByKey('first')
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
Get a document from a store by index
Indexy::getByIndex(indexName: String, indexValue: String, [storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.
myIndexedDB
.getByIndex('indexName', 'first-key-updated')
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
Delete a document from a store by key
Indexy::delete(keyValue: String, [storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.
myIndexedDB
.delete('first')
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
ClearStore (delete) all documents from a store
Indexy::clearStore([storeName: string]): Promise
If the optional parameter storeName is not passed the defaultStore passed to the constructor will be used.
myIndexedDB
.clearStore()
.then((result) => { console.log(result) })
.catch((err) => { console.log(err.toString()) })
)
Delete the indexedDB database
Indexy::rmDatabase(): Promise
myObjectDB
.rmDatabase()
.then((evt) => { console.log(evt) })
.catch((err) => { console.log(err.toString()) })