compactIDB
compactIDB
operations can be used to perform basic IndexedDB operations such as add, read/write, modify and remove. These functions are asynchronous and return a promise. Contains the following operations.
NOTE: Compact IndexedDB operations have all been promisified. All output needs to be handled using .then and .catch
Example:
compactIDB.someFunction(parameters)
.then(result => process_resolve(result))
.catch(error => process_reject(error))
Default database is always use if database name argument (dbName
) is not passed to database operators
Default database can be set using either of the following:
compactIDB.setDefaultDB(dbName);
(or)
compactIDB.default = dbName;
- dbName (optional) - This is the name of default database to be used.
- Note: this operation is neither promisified nor returns a value. It just sets the default DB
NOTE: Configured default database name can be returned using getter compactIDB.default
.
Example:
var defaultDB = compactIDB.default;
compactIDB.initDB(dbName, objectStores = {})
Initializes new IndexedDB.
- dbName (optional) - Specifies database to be initialized.
- objectStores *(optional)() - Accepts an object containing various objectStores (or) array of objectStore names to be initiazed when creating an IDB.
- Resolves: Status (string) | Rejects: error
Example:
compactIDB.initDB('sampleDB1', ['employee', 'student'])
compactIDB.initDB('sampleDB2', {
employee: {},
student: {}
})
compactIDB.initDB('sampleDB3', {
'employee': {
options: <objectStoreOptions>,
indexes: {
name: {},
age: {},
address: <indexesOptions>
}
}
})
sampleDB1 and sampleDB2 are equivalent databases
compactIDB.openDB(dbName)
Opens and resolves the database.
- dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: database (IDB) | Rejects: error
compactIDB.deleteDB(dbName)
Deletes the specified database.
- dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: Status (string) | Rejects: error
compactIDB.writeData(obsName, data, key, dbName)
Writes specified data into the database if data doesn't exists or updates it when data is already present.
- obsName - object store name in which the data is to be written.
- data - data that has to be written in specified object store.
- key (optional) - Primary key of the data (
null
orfalse
can be used for objectstores with autoincrement keys) (DEFAULT:false
) - dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: Status (string) | Rejects: error
compactIDB.addData(obsName, data, key, dbName)
Adds a new data into object store. If data already exists, it will return an error.
- obsName - Object store name in which has to be stored.
- data - The data which has to be added to obeject store.
- key (optional) - Primary key of the data (
null
orfalse
can be used for objectstores with autoincrement keys) (DEFAULT:false
) - dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: Status (string) | Rejects: error
compactDB.removeData(obsName, key, dbName)
Deletes data from specified object store using primary key.
- obsName - Name of object store from which the data has to be removed.
- key - Primary key of the data.
- dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: Status (string) | Rejects: error
compactDB.clearData(obsName, dbName)
Clears all data in the objectStore.
- obsName - Name of object store from which the data has to be removed.
- dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: Status (string) | Rejects: error
compactDB.readData(obsName, key, dbName)
Reads the data from object store associated with specified key.
- obsName - Name of object store from which the data has to be retrieved.
- key - Primary key of the data to read.
- dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: data (Object) | Rejects: error
compactDB.readAllData(obsName, dbName)
Reads all the data from specified object store using IndexedDB openCursor method.
- obsName - Name of object store from which the data has to be retrieved.
- dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: data (Object) | Rejects: error
compactIDB.searchData(obsName, options, dbName)
Search and resolve data that satisfy the conditions.
- obsName - Name of object store from which the data has to be retrieved.
- options (optional) - Accepts an object of options. Available options:
-
lowerKey
: records after given key (inclusive) -
upperKey
: records before given key (inclusive) -
atKey
: record at given key -
limit
: max number of records to resolve -
lastOnly
: resolve only the last record matching the search -
reverse
: search and resolve the records in reverse order -
patternEval
: a function to check record key and value for match. Example:(key, value) => value > 10
will resolve all records with value greater than 10.
-
- dbName (optional) - Name of the database (uses defaultDB if not specified)
- Resolves: data (Object) | Rejects: error