IDBObjectStoreSync

Important : The synchronous version of the IndexedDB API was originally intended for use only with Web 工作者 , 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.

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 );

属性

属性 Type 描述
indexNames readonly DOMStringList A list of the names of the indexes on this object store.
keyPath readonly DOMString 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 常量 .
name readonly DOMString The name of this object store.

常量

Mode constants

常量 描述
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.

方法

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);
					
参数
返回
异常

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 ,和 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
 );
					
参数
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.
返回
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);
					
参数
key
The key that identifies the record to be retrieved.
返回
any
The value retrieved from the object store.
异常

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()

创建 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);
					
参数
range
The key range to use as the cursor's range.
direction
The cursor's required direction.
返回
IDBIndexSync
An object for accessing the index.
异常

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);
					
参数
name
The name of the index to open.
返回
IDBIndexSync
An object to access the index.
异常

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);
					
参数
value
The value to be stored in the record.
key
The key to be used to identify the record.
返回
any
The key for the stored record.
异常

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);
					
参数
key
Key of the records to be removed.
返回

void

异常

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);
					
参数
indexName
The name of the existing index to remove.
异常

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.