indexeddb-promised
This library implements an interface for indexedDB where all the functions return a promise for the result of the underlying indexedDB function they call.
It also uses the builder pattern to configure the database schema and return an object to interact with it.
Bug reports are welcome at https://github.com/vergara/indexeddb-promised.
Getting started
Using browserify
In an empty folder, run the commands:
npm install -g gulp browserify
npm install indexeddb-promised vinyl-source-stream
Create the files:
gulpfile.js:
var browserify = ;var gulp = ;var source = ; var sourceFile = './js/app.js';var destFolder = './';var destFile = 'bundle.js'; gulp; gulp;
index.html:
Press Enter to create note. Title: Contnet: Notes: Sorted by title Sorted by creation date
js/app.js:
var app = ;
js/index.js:
module { window;};
Run the command:
gulp
Now you can open the index.html file from your browser and give it a try.
Creating an instance of indexeddb and a database schema
var Builder = ; var builder = 'myApp'; var myAppDB = builder; var user1 = myAppDBusers;var user2 = myAppDBusersByEmail;
Functions
Builder Constructor: new Builder(dbname)
dbname string represents the name of the database that is going to be opened or created in indexedDB.
addObjectStore(storeDefinitionObject)
storeDefinitionObject:
name: name of the store in indexedDB. Also used to expose an objectStore with the same name in the db object:
var myAppDB = 'myApp'
keyType: can be {autoIncrement: true}, {keyPath: key}, both, or undefined for out of line keys.
indexes: contains index definition objects:
name: makes access to the index available through objectStoreByName.
var user = myAppDBusersByEmail;
keyPath: name of the key in the value object used for the index to lookup data.
indexOptions: see the Mozilla documentation on createIndex. Example: {unique: false}
Example:
var Builder = ; var myAppDB = 'myApp';
setVersion(number)
number must be an integer. Changes in the myAppDB, like adding objectStores or indexes to existing objectStores, must be accompanied with an increase in the version number in the setVersion() function in order for the schema changes to take effect.
setDebug()
Makes a database with name 'dbName' available in the global object as 'indexeddbPromised_dbName'.
API
indexeddb.objectStore.add(record[, key])
record can be any type of object. If not using the keyPath key type in the object store, then it can also be a primitive.
Returns a promise for the key of the record. Useful when using key type autoIncrement: true.
indexeddb.objectStore.count()
Returns a promise for an integer number that represents the number of records in the object store.
indexeddb.objectStore.get(key)
Return a promise for the value of the record identified by key.
indexeddb.objectStore.delete(key)
Returns a promise resolved with null.
indexeddb.objectStore.clear()
Deletes all records from the object store. Returns a promise resolved with null.
indexeddb.objectStore.put(record[, key])
Similar to add(), but replaces existing records. The key parameter is not required if keyPath key type is used and the record has the property used as keyPath populated.
indexeddb.objectStore.getAll()
Returns a promise for an array with all values from all records.
indexeddb.objectStore.getAllKeys()
Returns a promise for an array with the keys of all records.
indexeddb.objectStore.openCursor([IDBKeyRange][, 'prev'])
Returns a promise for a cursor that can be iterated using the standard iterator syntax:
indexeddbtestObjStore// Iterate key-value pairs in reverse order from 4 through 2, including 4 and 2;
For more information on IDBKeyRange see the Mozilla documentation on IDBKeyRange.
indexeddb.objectStore.openProgressiveCursor([IDBKeyRange][, 'prev'])
Similar to openCursor(), but optimized for memory usage. Use this method when retrieving big amounts of data. If the amounts of data are small, openCursor() is faster.
Returns a promise for a cursor that can be iterated using the standard iterator syntax. Each result returned by the iterator is a promise for a record. This is different to how openCursor() works, which returns the record directly. This way of working allows to destroy each record after it has been used so memory usage can be kept low while the iteration is in progress.
Another advantage of this method over openCursor() is that the records can be used immediately as they become available. In the case of openCursor(), all the records must be delivered by the database before they can be used.
indexeddbtestObjStore;
indexeddb.execTransaction(operations,objectStores[, mode])
Low level method to execute a transaction in the database. The first parameter is an array of functions where each function is an operation that is to be executed in the transaction. The second array contains strings with the names of the obectStores used in the transaction. The last parameter is the transaction mode which can be "readonly" (default) or "readwrite".
The function returns a promise for an array with the accumulated results of each operation. Falsey values are accumulated as null.
The operations are defined as follows:
{ // Perform operations using indexedDB's API ... var objectStore = transaction; var request = objectStore; return request;}
Operations take a transaction as a parameter that can be used to retrieve objectStores and indexes. They can return a request if the result is useful, or null if the database operation doesn't return a value or the result of the operation is to be discarded.
Full example:
var testRecord = testKey: "testValue"; var { var objectStore = transaction; var request = objectStore; return request;}; var { var objectStore = transaction; var records = ; objectStore { var cursor = eventtargetresult; var result; if cursor var record = {}; recordkey = cursorkey; recordvalue = cursorvalue; records; cursor; }; return records;}; var { var objectStore = transaction; var request = objectStore; return request;}; var transactions = addRecord getRecords deleteRecord;return indexeddb