IDBObjectStoreSync

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!
您也可以阅读此文章的English (US)版。

Important: The synchronous version of the IndexedDB API was originally intended for use only with Web Workers, and was eventually removed from the spec because its need was questionable. It may however be reintroduced in the future if there is enough demand from web developers.

The IDBObjectStoreSync interface of the IndexedDB API provides synchronous access to an object store of a database.

Method overview

any add (in any value, in optional any key) raises (IDBDatabaseException);
IDBIndexSync createIndex (in DOMString name, in DOMString storeName, in DOMString keypath, in optional boolean unique);
any get (in any key) raises (IDBDatabaseException);
IDBCursorSync openCursor (in optional IDBKeyRange range, in optional unsigned short direction) raises (IDBDatabaseException);
IDBIndexSync openIndex (in DOMString name) raises (IDBDatabaseException);
any put (in any value, in optional any key) raises (IDBDatabaseException);
void remove (in any key) raises (IDBDatabaseException);
void removeIndex (in DOMString indexName) raises (IDBDatabaseException);

Attributes

Attribute Type Description
indexNames readonly DOMStringList A list of the names of the indexes on this object store.
keyPath readonly DOMString The key path of this object store. If this attribute is set to null, then the application must provide a key for each modification operation.
mode readonly unsigned short The mode for isolating access to the data in this object store. For possible values, see Constants.
name readonly DOMString The name of this object store.

 Constants

Mode constants

Constant Value Description
READ_ONLY 1 Modification operations are not allowed on this object store.
READ_WRITE 0 Modification operations are allowed on this object store.
SNAPSHOT_READ 2 Any read operations must access a snapshot view of the data, which cannot change once it is created.

Methods

add()

Stores the given value into this object store, optionally with the specified key. If a record already exists with the given key, an exception is raised.

any add(
  in any value,
  in optional any key
) raises (IDBDatabaseException);
Parameters
Returns
Exceptions

This method can raise a IDBDatabaseException with the following codes:

value
The value to store into the index.
key
A key to use for identifying the record.
any
The key for the stored record.
CONSTRAINT_ERR
If a record exists in this index with a key corresponding to the key parameter or the index is auto-populated, or if no record exists with a key corresponding to the value parameter in the index's referenced object store.
DATA_ERR
If this object store uses out-of-line keys, and the key parameter was not passed.
SERIAL_ERR
If the data being stored could not be serialized by the internal structured cloning algorithm.

createIndex()

Creates and returns a new index with the given name in the connected database.

 IDBIndexSync createIndex (
    in DOMString name,
    in DOMString keypath,
    in optional boolean unique
 );
Parameters
name
The name of a new index.
keyPath
The key path used by the new index.
unique
If true, keys in the index must be unique; if false, duplicate keys are allowed.
Returns
IDBIndexSync
An object to access the newly created index.

get()

Retrieves and returns the value from this object store for the record that corresponds to the given key.

any get (
  in any key
) raises (IDBDatabaseException);
Parameters
key
The key that identifies the record to be retrieved.
Returns
any
The value retrieved from the object store.
Exceptions

This method can raise a IDBDatabaseException with the following codes:

SERIAL_ERR
If the data being stored could not be deserialized by the internal structured cloning algorithm.
NOT_FOUND_ERR
If no record exists in this index for the given key.

openCursor()

Creates a cursor over the records of this object store. The range of the new cursor matches the specified key range; if the key range is not specified or is null, then the range includes all the records.

CursorSync openCursor (
  in optional KeyRange range,
  in optional unsigned short direction
) raises (DatabaseException);
Parameters
range
The key range to use as the cursor's range.
direction
The cursor's required direction.
Returns
IDBIndexSync
An object for accessing the index.
Exceptions

This method can raise a DatabaseException with the following code:

NOT_FOUND_ERR
If no records exist in this index for the requested key range.

openIndex()

Opens the index with the given name, using the mode of the current transaction.

 IDBIndexSync openIndex (
   in DOMString name
 ) raises  (IDBDatabaseException);
Parameters
name
The name of the index to open.
Returns
IDBIndexSync
An object to access the index.
Exceptions

This method can raise an IDBDatabaseException with the following code:

NOT_FOUND_ERR
If the index with the given name does not exist in the connected database.

put()

Stores the given value in this object store and returns the key for the stored record. If a record already exists with the given key, it is overwritten.

any put (
  in any value,
  in optional any key
) raises (IDBDatabaseException);
Parameters
value
The value to be stored in the record.
key
The key to be used to identify the record.
Returns
any
The key for the stored record.
Exceptions

This method can raise an IDBDatabaseException with the following codes:

CONSTRAINT_ERR
If noOverwrite was true, and a record exists in this index for the given key or this index is auto-populated; or if no record exists with the given key in the index's referenced object store.
DATA_ERR
If this object store uses out-of-line keys and no key generator, but no key was given.
SERIAL_ERR
If the data being stored could not be serialized by the internal structured cloning algorithm.

remove()

Removes from this object store any records that correspond to the given key.

void remove (
  in any key
) raises (IDBDatabaseException);
Parameters
key
Key of the records to be removed.
Returns

void

Exceptions

This method can raise a IDBDatabaseException with the following code:

NOT_FOUND_ERR
If a record does not exist in this index with the given key.

removeIndex()

Destroys an index with the given name.

  void removeIndex (
    in DOMString indexName
  ) raises (IDBDatabaseException);
Parameters
indexName
The name of the existing index to remove.
Exceptions

This method can raise an IDBDatabaseException with the following code:

NOT_FOUND_ERR
If an index with the given name does not exist in the connected database.

文档标签和贡献者