Stores
Indices
handler a function called on unexpected database events. It receives object parameter describing the error. Default handler is console.error, which is in action until redefined. IDB functions returning Promise don't reject unless the sentinel throws - in that case, the error can be caught immediately in .catch() Promise chain method.
The handler parametr object has these properties:
source name of the IDB function that caused the issue
message the issue message
params the parameter values to the IDB function
Resolves on req.onsuccess with value req.result
Rejects on req.onerror with value req.error
logs object from store users with index name=John
logs teenagers from users store
unsigned int current version of IDB database. If it is not created, random number 1 to 10e7 is picked as initial version (so the age of the page/database can not be guessed by clients)
Methods creating or dropping stores or indices can be run only during onupgradeneeded event soon after the database is opened. When they are called, the database needs to be closed and reopened. During the closing the database waits for running transactions to finish and doesn't accept new ones. Reopening is also made in separate thread and is not instantious. dbPromise avoids this gap when IDBRequest would cause error.
safe IDBDatabase object with with opened connection to IDB database
List all available databases (including versions)
Returned object may be null during the database opening period or unusable during the closing period when database manipulation functions are called. If such events don't happen (like clicking the button to retrieve some storage data) it is more or less safe to call the db object directly to simplify the code. (Keep in mind the IDB database can be changed in another browser tab in the same domain and the browser or OS may decide to close the connection for various reasons.)
List all available databases (including versions)
promise if true is provided as the first argument, Promise to IDBTransaction is returned. This can be skipped if the unsafe phase does not occur diring the call in which case the object is returned directly. Optional, default false.
stores can be a string (single store) or string[] (multiple stores) involved in the transaction. Multiple stores allows to revert the data changes in the middle of the writing process simply by calling abort() on the transaction.
w truthy value means transaction with writing priviledges, otherwise it is read-only (multiple read-only transaction may access the same store simultaneously)
Create account with 200 credits for every user named Kyle
promise see transaction
store the store name on which a single-store transaction is opened
w see transaction
place user with id=23 into output element
promise see transaction
store the store name on which the index is defined
name the name of the index which should be returned
log all teenagers from users store
Creates a IDBKeyRange object in a way common in XSL or bash habits, not the weird way the ad-hoc standard defines. This object is created internally in IDB.cursor and IDB.delete functions and (together with IDB.store) provides tools to work directly with IndexedDB.
functors identifiers are: eq (equals), lt (lower than), lte (lower than or equal), gt (greater than), gte (greater than or equal)
value1, value2 second parameter to the above function (the first one is the key of the object).
Creates a functor that (applied on age index) selects all teenagers from storage (sorted by age). See the full example below.
Upgrades (closes and reopens) the database with incremented version number without doing any change in the structure.
undefined, IDB.db is safe after the operation
promise see transaction
Log all stores in IDB and handle the blocked state
name The name of the store to create
indices Names of the stored objects attributes to be used as indices (index name is the same as attribute name in IDB). Indices allows to sort and filter records from store. First index serves as primary key, which is unique. Each index (and primary key) can be array of strings itself (compound index) which can filter records in OR logic. If the store should not have unique key, null can be set as the first key which creates autoincrement generator. Optional, default [null].
force true if the store already exists, it is dropped and re-created. false if the store already exists, do nothing. Optional, default false.
true if the store was created, false if not
string the store name which could not be created, when force is set to true (likely due to HW problem or resources locked).
MultiEntry or unique indices can be created only with IDB.createIndex(). If not called during the upgrade phase, this method closes and reopens the database.
name The name of the store to delete
true if store existed and has been deleted, false otherwise
string the store name which could not be deleted, when force is set to true (likely due to HW problem or resources locked).
The deletion requests are run in 3 separate concurrent threads.
Also deletes all keys in the object store. If not called during the upgrade phase, this method closes and reopens the database.
promise see transaction
storage the storage name to get the indices from
store store where the index should be created
index the key of the index; can be string[] to create compound index. In that case, the name is created by joining the array items with underscore separator (i.e. ["name","age"] leads to name_age index name.
params object with keys unique, multiEntry or locale, described here. Default non-unique, single-entry, without locale.
IDBIndex, the newly created index
[store,index,params] the store on which the index with provided params could not be created.
Don't confuse compound and multiEntry index. Compound indices are usually single-entry. MultiEntry indices are usually not unique. If not called during the upgrade phase, this method closes and reopens the database.
store store to delete the index from
index name of the index to delete (array only for compound index, string is also accepted)
bool, true if the index existed and was deleted, false otherwise
[store,index] the store on which the index could not be deleted.
If not called during the upgrade phase, this method closes and reopens the database.
name name of the store where the data goes
data can be object or array of objects - in that case all the array items are added as records one by one in the order in the array.
keyPath the unique key path, if the store was created without keys (see out-of-line keys). If the object store was created by IDB.storeCreate function with first key set to null, it is autoincrement out-of-line key. Optional, do not use with inline keys.
int the number of records inserted, resolves only if all the records were inserted successfully.
[name,data,keyPath,fn] the parameters that caused the insert fail. Data can be array (if the whole insert failed, i.e. non-existent store) or a single record (i.e. its keyPath already exists in the store). fn is "add"; the same underlying function is used for IDB.upsert, where fn is "put".
Records are structural clones of the original objects, which is functionally equivalent to serialized and then deserialized original object (references are transformed to values). If inline keyPath was specified on the object store, keyPath parameter must not be used.
This function acts like IDB.insert when the record with the same primary key (or keyPath) doesn't exist in the store. When it does, IDB.insert rejects, while IDB.update updates existing record.
name name of the store where to update the records
index name of the index for the range constraints. Optional.
range Only records that match the index and the range will be passed to updator. Can boost the performance if updator is slow. Optional, take all available records to updator by default.
updator Takes the record as the parameter. To update it in the store, it must return non-falsy value, otherwise the record is not updated.
int, the number of affected records
[name,index,range,fn] the params that caused the update to fail. fn is "update". Same underlying function is used for IDB.delete, where the fn is "delete".
Raise the age of all users by 1 and log the number of affected users.
If index is used, its value must not be updated in updator, otherwise deadlock may happen.
Act similarly like the IDB.update above, the only difference is the last parameter, deletor function.
deletor Takes the record as the parameter. If it returns truthy value, the record is deleted from the store.
store store name to delete the record from
pk the primary key value
undefined
[store, pk] the params that caused the delete to fail.
Deletes a single record based on the primary key value, which is safe as far as you can guarantee the PK value is not compromised. IDB.update and IDB.delete can destroy a lot of data and should be therefore called with special care.
name name of the store to delete all records from
undefined
string the name of the store that failed to clear
This function was intentionally separated from IDB.delete to avoid deleting all records by mistake.
store The name of the store to count the records from.
index Optional. If provided, the count is limited to records in the index.
int the number of the records in the object
name the name of the store to retrieve a record from
pk the value of the primary key of the requested record; type must also match
The object with the specified primary key (if found), undefined if not found
tx transaction on which the cursor is defined. If ommited, it is created as single-store read-only transaction from the name parameter.
name name of the storage to sort or filter.
index index to use for sorting or filtering. Optional, if falsy it sorts the store by primary key (default).
range see IDB.range. Optional, falsy values means no filtering.
dir next, nextunique (ASC), prev, prevunique (DESC). Optional, default next.
IDBRequest that resolves in IDBCursor in read-only transaction (so the request.onsuccess event can be handled).
log all users one by one to console, last inserted first
IDBCursor calls its onsuccess method for each record found. This method creates cursor in read-only transaction. But IDBCursor has also update method: this is utilized in IDB.update function, while read only scenarios can be mostly solved with IDB.walk and IDB.filter below. These functions are to be called in IDB.cursor().then(). Their only parameter is a function, that gets called for every record the cursor yields.
The following functions get these parameters:
record structural clone of the record as their only parameter
pk primary key value
tx the transaction of the cursor (meaningful on for write transactions)
The return value of the parameter function is ignored, it is meant just to traverse all the records.
log all users one by one to console, last inserted first
The parameter function should return truthy value iff the record shall pass the filter. The IDB.filter function itself then returns array of filtered data (in Promise) for further processing.
log all users, sorted by name and having "an" in their name