This document defines APIs for a database of records holding simple values and hierarchical objects. Each record consists of a key and some value. Moreover, the database maintains indexes over records it stores. An application developer directly uses an API to locate records either by their key or by using an index. A query language can be layered on this API. An indexed database can be implemented using a persistent B-tree data structure.
The features in Section 3.3 Synchronous APIs are at risk and may be removed, due to potential lack of implementations.
The synchronous database API methods provide a blocking access pattern to IndexedDB databases. Since they block the calling thread they are only available from workers.
WorkerUtils
objects MUST implement the
IDBEnvironmentSync
interface.
When invoked, this method MUST throw a DOMException
of type InvalidStateError
when it is called within the IDBTransactionCallback of a transaction
method or the IDBVersionChangeCallback of a open
method. Otherwise this method
synchronously runs the steps for opening a database. Let origin be the origin of the
IDBEnvironmentSync used to access this IDBFactorySync, name, version
and upgrade callback be the name, version and upgradeCallback
arguments passed to this function.
If no version is specified and a database exists, use the current database version and follow the steps for opening a database. If no version is specified and no database exists, set database version to 1, follow the steps for opening a database, and return a database without object stores.
If a timeout parameter was supplied, then this limits the total waiting time allowed in
step 3 of steps for opening a database and step 4 of the steps for running a
"versionchange"
transaction. If more waiting time would be needed in order to progress,
then abort those algorithms and throw a DOMException
of type TimeoutError.
The timeout parameter does not affect how long the upgradeCallback can run.
If an error is returned from the steps above, then the implementation MUST
throw a DOMException
with its name and message set to appropriate values for the error.
If the steps above are successful, the implementation MUST create an IDBDatabaseSync object representing the created connection and return it.
Processing a open
call may take a long time as it could require running a
"versionchange"
transaction which requires all other connections to the database to be closed, which
in turn may depend on user input or other long-running tasks. If blocking for a long period of time
is not acceptable for a given scenario then the asynchronous API should be used for version changes.
If the value for version
is 0
(zero) or a negative number, this method MUST
throw a JavaScript TypeError
exception.
"versionchange"
transaction. If the parameter is not provided,
the value of timeout is infinite.open
method was called within the IDBTransactionCallback of a
transaction
method or IDBVersionChangeCallback of a
open
method.
version
is 0
(zero) or a negative number.When invoked, this method synchronously runs the steps for deleting a database. Let origin be the origin of the IDBEnvironmentSync used to access this IDBFactorySync and name be the name argument passed to this function.
If an error is returned from the steps above, then the implementation MUST
throw a DOMException
with its name and message set to appropriate values for the error.
When invoked, this method MUST compare two keys. The method returns 1 if the first key is greater than the second, -1 if the first is less than the second, and 0 if the first is equal to the second.
A database object provides access to the schema and data of a particular database.
"versionchange"
transaction if
createObjectStore
or deleteObjectStore
is called on this IDBDatabaseSync instance itself.
This method creates and returns a new object store with the
given name in the connected database. This method
should only be called from inside a "versionchange"
transaction.
This method synchronously modifies the IDBDatabaseSync.objectStoreNames property. However it only modifies the IDBDatabaseSync.objectStoreNames property on the IDBDatabaseSync instance on which it was called.
If the optionalParameters argument is specified and has a keyPath
property
which is not null
, then set keyPath to the value
of this property. If keyPath is an Array
, then each item in the array is
converted to a string. If keyPath is not an Array
, it is converted to a
string.
If keyPath is not a valid key path then a DOMException
of type SyntaxError MUST be
thrown. Otherwise set the created object store's key path to the value of
keyPath.
If the optionalParameters parameter is specified, and autoIncrement
is set to true, and
the keyPath
parameter is specified to the empty string, or specified to an Array
,
this function MUST throw a InvalidAccessError
exception.
keyPath
specifies the key path of the new
object store. If the attribute is null
no key path is
specified and thus keys are out-of-line.
autoIncrement
specifies
whether the object store created should have a key generator."versionchange"
transaction.
Also occurs if a request is made on a source object that has been deleted or removed.
Array
containing the empty string.
This method destroys an object store with the
given name as well as all indexes that are
referencing that object store. This method
should only be called from inside a "versionchange"
transaction.
This method synchronously modifies the IDBDatabaseSync.objectStoreNames property. However it only modifies the IDBDatabaseSync.objectStoreNames property on the IDBDatabaseSync instance on which it was called.
"versionchange"
transaction callback.
Also occurs if a request is made on a source object that has been deleted or removed.
This method, when called MUST execute the
steps for creating a transaction in a sychronous
fashion. The storeNames, callback, mode, and
timeout arguments are forwarded to the algorithm as-is. The connection
argument is set to the IDBDatabaseSync that the transaction()
method was
called on.
The method returns an IDBTransactionSync object representing the transaction returned by the steps above.
This method MUST throw an DOMException
of type InvalidStateError
when it is called within the IDBTransactionCallback of a transaction
method or the
IDBVersionChangeCallback of a open
method.
"readonly"
.
close()
method has been called on this IDBDatabase instance.
Also thrown when The transaction()
method was called within the
IDBTransactionCallback of a transaction
method or the
IDBVersionChangeCallback of a open
method.In the following example, we set up an object store to use the key path id. This object store is also designed to use a key generator.
Using this database, we can store records in the Contact object store.
A stored value can be retrieved using the same key used by the first put operation.
A put operation will overwrite the record stored by the first add operation with the same key.
Now when the object store is read with the same key, the result is different compared to the object read earlier.
Additionally, all the records of an object store matching a certain key range can be retrieved in key order.
DOMString
,
an Array
of DOMString
s or null
.
This method throws a DOMException
of type ReadOnlyError if the transaction which this IDBObjectStoreSync
belongs to
has its mode set to "readonly"
. If any of the following conditions are true, this method
throws a DOMException
of type DataError:
Otherwise this method creates a structured clone of the value parameter. If the structured clone algorithm throws an exception, that exception is rethrown. Otherwise, run the steps for synchronously executing a request and return the result, in this case the key of the stored object. The steps are run with this IDBObjectStoreSync as source and the steps for storing a record into an object store as operation, using this IDBObjectStoreSync as store, the created clone as value, the key parameter as key, and with the no-overwrite flag flag set to false.
"readonly"
.
This method throws a DOMException
of type ReadOnlyError if the transaction which this IDBObjectStoreSync belongs to is
has its mode set to "readonly"
. If any of the following conditions are true, this method
throws a DOMException
of type DataError:
Otherwise this method creates a structured clone of the value parameter. If the structured clone algorithm throws an exception, that exception is rethrown. Otherwise, run the steps for synchronously executing a request and return the key of the stored object. The steps are run with this IDBObjectStoreSync as source and the steps for storing a record into an object store as operation, using this IDBObjectStoreSync as store, the created clone as value, the key parameter as key, and with the no-overwrite flag flag set to true.
"readonly"
.
This method throws a DOMException
of type ReadOnlyError if the transaction which this IDBObjectStoreSync belongs to is
has its mode set to "readonly"
. If the key parameter is not a valid key
or a key range this method throws a DOMException
of type DataError.
Otherwise this method runs the steps for synchronously executing a request. The steps are run with this IDBObjectStoreSync as source and the steps for deleting records from an object store as operation, using this IDBObjectStoreSync as store and the key parameter as key. The function returns the result of running these steps.
Unlike other methods which take keys or key ranges, this method does not allow null to be passed as key. This is to reduce the risk that a small bug would clear a whole object store.
"readonly"
.
If the key parameter is not a valid key or a key range, this method
throws a DOMException
of type DataError. Otherwise, this method runs the steps for synchronously executing
a request and returns the result of the operation. The steps are run with this
IDBObjectStoreSync as source and the steps for retrieving a value from an
object store as operation, using this IDBObjectStoreSync as store and the
key parameter as key.
This function produces the same result if a record with the given key doesn't exist as when a record
exists, but has undefined
as value. If you need to tell the two situations apart, you can use
openCursor with the same key. This will return a cursor with
undefined
as value if a record exists, or no cursor if no such record exists.
This method throws a DOMException
of type ReadOnlyError if the transaction which this IDBObjectStoreSync belongs to is
has its mode set to "readonly"
.
Otherwise this method runs the steps for synchronously executing a request. The steps are run with this IDBObjectStoreSync as source and the steps for clearing an object store as operation, using this IDBObjectStoreSync as store.
"readonly"
.
This creates and returns a new index with the given name and parameters in the
connected database. Note that this method must only
be called from a "versionchange"
transaction. The created
index has its unique and multiEntry flags are set to the values of the
unique and multiEntry properties in the optionalParameters argument.
If the keyPath argument is an Array
, then each item in the array is
converted to a DOMString
. If keyPath is not an Array
, it is converted to a
DOMString
.
If keyPath is not a valid key path then a DOMException
of type SyntaxError MUST be
thrown. Otherwise set the created object store's key path to the value of
keyPath. If keyPath is
an Array
and the multiEntry property in the optionalParameters is true,
then a DOMException
of type InvalidAccessError MUST be thrown. Otherwise set the created index's
key path to the value of keyPath.
unique
specifies whether the index's unique flag is
set.
multiEntry
specifies whether the index's multiEntry flag is
set.
"versionchange"
transaction. Also occurs if a request is made on a source object that has been deleted or removed.
This method destroys the index with the given name in the
connected database. Note that this
method must only be called from a "versionchange"
transaction.
"versionchange"
transaction.
If the range parameter is specified but is not a valid key or a key range,
this method throws a DOMException
of type DataError. Otherwise, this method creates a cursor. The cursor
MUST implement the IDBCursorWithValueSync interface.
The newly created cursor MUST have an undefined position, a direction set to the value of the direction parameter, false as iterable flag value, and undefined key and value. The source of the cursor is the IDBObjectStoreSync this function was called on.
If the range parameter is a key range then the cursor's range is set to that range. Otherwise, if the range parameter is a valid key then the cursor's range is set to key range containing only that key value. If the range parameter is not specified, the cursor's key range is left as undefined.
This method runs the steps for synchronously executing a request and returns the result of the operation, in this case an IDBCursorSync object. The steps are run with this IDBObjectStoreSync as source and the steps for iterating a cursor as operation, using the created cursor as cursor and with undefined as key.
If the optional key parameter is not a valid key or a key range,
this method throws a DOMException
of type DataError.
This method runs the steps for asynchronously executing a request and returns the IDBRequest created by these steps.
The steps are run with this IDBObjectStore as source and the steps for iterating a cursor as operation,
using the created cursor as cursor.
If provided, use the key parameter as key, otherwise, use undefined as key.
If the result of the algorithm is null return 0
(zero) as the result for the request.
Otherwise, use the return cursor to determine the total number of objects that share the
key or key range and return that value as the result for the request.
IDBKeyRange
.
An index can be created for retrieving records other than by using record keys. Continuing the earlier example, an index could be maintained on the name property of objects in the Contact object store.
For example, the id of an object with the name property value 'Lincoln' can be retrieved using the ContactName index.
Additionally, all the records of an object store matching a certain range index keys can be retrieved in key order. When objects are retrieved from the Contact object store, they are arranged by the value of the id attribute. On the other hand, when objects are retrieved using the ContactName index, they are arranged by the value of the name property.
If, on the other hand, we only want the names and keys but not the whole Contact objects for a given range, then we can use a different mechanism for that.
DOMString
or
an Array
of DOMString
s.
If the range parameter is specified but is not a valid key or a key range,
this method throws a DOMException
of type DataError. Otherwise, this method creates a cursor. The cursor
MUST implement the IDBCursorWithValueSync interface.
The newly created cursor MUST have an undefined position, a direction set to the value of the direction parameter, false as iterable flag value, and undefined key and value. The source of the cursor is the IDBIndexSync this function was called on.
If the range parameter is a key range then the cursor's range is set to that range. Otherwise, if the range parameter is a valid key then the cursor's range is set to key range containing only that key value. If the range parameter is not specified, the cursor's key range is left as undefined.
This method runs the steps for synchronously executing a request and returns the result, in this case a cursor object. The steps are run with this IDBIndexSync as source and the steps for iterating a cursor as operation, using the created cursor as cursor and with undefined as key
If the range parameter is specified but is not a valid key or a key range,
this method throws a DOMException
of type DataError. Otherwise, this method creates a cursor. The cursor
MUST implement the IDBCursorSync interface and MUST NOT implement the IDBCursorWithValueSync
interface.
The newly created cursor MUST have an undefined position, a direction set to the value of the direction parameter, false as iterable flag value, and undefined key and value. The source of the cursor is the IDBIndexSync this function was called on.
If the range parameter is a key range then the cursor's range is set to that range. Otherwise, if the range parameter is a valid key then the cursor's range is set to key range containing only that key value. If the range parameter is not specified, the cursor's key range is left as undefined.
This method runs the steps for synchronously executing a request and returns the result, in this case a cursor object. The steps are run with this IDBIndexSync as source and the steps for iterating a cursor as operation, using the created cursor as cursor and with undefined as key
If the key parameter is not a valid key or a key range, this method
throws a DOMException
of type DataError. This method runs the steps for synchronously executing a request
and returns the result from that, in this case an object from the underlying store. The steps are
run with this IDBIndexSync as source and the steps
for retrieving a referenced value from an index as operation, using this IDBIndexSync
as index and the key parameter as key.
This function produces the same result if a record with the given key doesn't exist as when a record
exists, but has undefined
as value. If you need to tell the two situations apart, you can use
openCursor with the same key. This will return a cursor with
undefined
as value if a record exists, or no cursor if no such record exists.
If the key parameter is not a valid key or a key range, this method
throws a DOMException
of type DataError. This method runs the steps for synchronously executing a request
and returns the result from that, in this case an index value (a key). The steps are
run with the IDBObjectStoreSync associated with this index as source and the steps
for retrieving a value from an index as operation, using this IDBIndexSync
as index and the key parameter as key.
If the optional key parameter is not a valid key or a key range,
this method throws a DOMException
of type DataError.
This method runs the steps for asynchronously executing a request and returns the IDBRequest created by these steps.
The steps are run with this IDBIndex as source and the steps for iterating a cursor as operation,
using the created cursor as cursor.
If provided, use the key parameter as key, otherwise, use undefined as key.
If the result of the algorithm is null return 0
(zero) as the result for the request.
Otherwise, use the return cursor to determine the total number of objects that share the
key or key range and return that value as the result for the request.
IDBKeyRange
.
Using the synchronous API, an application can process all the records in the cursor's range.
By default, a cursor walks over objects starting at the first record and ending at the last record including all the duplicates encountered along the way.
To start at the last record and end in the first record, the cursor
should be created with the direction parameter "prev"
.
To start at a certain key and end in the last record, i.e., for a lower-bounded cursor, while skipping duplicates, the cursor should be created with both the required start key and the direction parameter.
It is also possible to create a bounded cursor, i.e., with
application-specified starting and ending points, the
cursor should be created with both the required keys.
If the range is inclusive of both keys, then additional
flags are required. In the following example, all keys
with values in the inclusive range (start
,
end
) are returned with all their duplicates,
from the beginning of the range to its end.
Array
), it returns the same
object instance every time it is inspected, until the cursor is iterated.
This means that if the object is modified,
those modifications will be seen by anyone inspecting the value of the cursor. However modifying such an object
does not modify the contents of the database.Array
), it returns the same
object instance every time it is inspected, until the cursor is iterated.
This means that if the object is modified,
those modifications will be seen by anyone inspecting the value of the cursor. However modifying such an object
does not modify the contents of the database.
This method throws a DOMException
of type ReadOnlyError
if the transaction which this IDBCursorSync belongs to has its mode set to "readonly"
.
If this cursor's got value flag is false or if this cursor was created using
openKeyCursor
, this method
throws a DOMException
of type InvalidStateError.
If the effective object store of this cursor uses in-line
keys and evaluating the key path
of the value parameter results in a different value than the cursor's effective key,
this method throws DOMException
of type DataError.
Otherwise this method creates a structured clone of the value parameter. If the structured clone algorithm throws an exception, that exception is rethrown. Otherwise, run the steps for synchronously executing a request and return the result returned by these steps. The steps are run with this IDBCursorSync as source and the steps for storing a record into an object store as operation, using this cursor's effective object store as store, the created clone as value, this cursor's effective key as key, and with the no-overwrite flag flag set to false.
A result of running the steps for storing a record into an object store is that if the record has been deleted since the cursor moved to it, a new record will be created.
"readonly"
openKeyCursor
or if the cursor
is currently being iterated or has iterated past the end.
This method runs the steps for synchronously executing a request.
The steps are run with the cursor's source as source.
The operation runs the steps for iterating a cursor count number of times
with null as key and this cursor as cursor. If the steps for synchronously
executing a request returns a cursor, then this function returns true
. Otherwise
this function returns false
.
Before this method returns, unless an exception was thrown, it sets the got value flag in the cursor to false.
Calling this method more than once before new cursor data has been loaded is not allowed
and results in a DOMException
of type InvalidStateError being thrown.
For example, calling advance()
twice from the same onsuccess handler
results in a DOMException
of type InvalidStateError being thrown on the second call.
If the value for count
is 0
(zero) or a negative number, this method MUST
throw a JavaScript TypeError
exception.
count
parameter was zero or a negative number.
If this cursor's got value flag is false, this method throws a DOMException
of type InvalidStateError.
If the key parameter is specified and fulfills any of these conditions this
method MUST throw a DOMException
of type DataError:
"next"
or "nextunique"
.
"prev"
or "prevunique"
.
Otherwise this method runs the steps for synchronously executing a request.
The steps are run with the cursor's source as source and the steps
for iterating a cursor as operation, using the cursor this is called on as cursor and
the key parameter as key. If the steps for synchronously executing a request returns
a cursor, then this function returns true
. Otherwise this function returns false
.
Before this method returns, unless an exception was thrown, it sets the got value flag in the cursor to false.
Calling this method more than once before new cursor data has been loaded is not allowed
and results in a DOMException
of type InvalidStateError being thrown.
For example, calling continue()
twice from the same onsuccess handler
results in a DOMException
of type InvalidStateError being thrown on the second call.
This method throws a DOMException
of type ReadOnlyError if the transaction which this IDBCursorSync belongs to
has its mode set to "readonly"
. If this cursor's got value flag is false,
or if this cursor was created using
openKeyCursor
a
DOMException
of type InvalidStateError is thrown.
Otherwise this method runs the steps for synchronously executing a request. The steps are run with this IDBCursorSync as source and the steps for deleting records from an object store as operation, using this cursor's effective object store and effective key as store and key respectively. The function returns the result of running these steps.
"readonly"
.openKeyCursor
or the cursor
is currently being iterated or has iterated past the end.When an application creates a transaction synchronously, it blocks until the user agent is able to reserve the required database objects.
abort
function, this property
returns null
.
If this transaction was aborted due to a failed request, this property returns the same
DOMError
as the request which caused this transaction to be aborted.
If this transaction was aborted due to an error when committing the transaction, and not due to a failed
request, this property returns a DOMError
which contains the reason for the transaction
failure (e.g. QuotaExceededError
or UnknownError
).
DOMException
of type InvalidStateError.
Otherwise this method sets the transactions's active flag to false and
aborts the transaction by running the steps for aborting a transaction with the error
parameter set to null
.
Once a transaction is aborted or committed, the active transaction on this database connection is removed. A new transaction can be created to perform operations atomically.