6.1. Object Store Storage Operation

The steps to store a record into an object store with store, value, an optional key, and a no-overwrite flag are as follows.

  1. If store uses a key generator, then:

    1. If key is undefined, then:

      1. Let key be the result of running the steps to generate a key for store.

      2. If key is failure, then this operation failed with a “[ConstraintError](https://www.w3.org/TR/WebIDL-1/#constrainterror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException). Abort this algorithm without taking any further steps.

      3. If store also uses in-line keys, then run the steps to inject a key into a value using a key path with value, key and store’s key path.

    2. Otherwise, run the steps to possibly update the key generator for store with key.

  2. If the no-overwrite flag was given to these steps and is set, and a record already exists in store with its key equal to key, then this operation failed with a “[ConstraintError](https://www.w3.org/TR/WebIDL-1/#constrainterror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException). Abort this algorithm without taking any further steps.

  3. If a record already exists in store with its key equal to key, then remove the record from store using the steps to delete records from an object store.

  4. Store a record in store containing key as its key and ! StructuredSerializeForStorage(value) as its value. The record is stored in the object store’s list of records such that the list is sorted according to the key of the records in ascending order.

  5. For each index which reference store:

    1. Let index key be the result of running the steps to extract a key from a value using a key path with value, index’s key path, and index’s multiEntry flag.

    2. If index key is an exception, or invalid, or failure, take no further actions for index, and continue these steps for the next index.

      An exception thrown in this step is not rethrown.

    3. If index’s multiEntry flag is unset, or if index key is not an array key, and if index already contains a record with key equal to index key, and index has its unique flag set, then this operation failed with a “[ConstraintError](https://www.w3.org/TR/WebIDL-1/#constrainterror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException). Abort this algorithm without taking any further steps.

    4. If index’s multiEntry flag is set and index key is an array key, and if index already contains a record with key equal to any of the subkeys of index key, and index has its unique flag set, then this operation failed with a “[ConstraintError](https://www.w3.org/TR/WebIDL-1/#constrainterror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException). Abort this algorithm without taking any further steps.

    5. If index’s multiEntry flag is unset, or if index key is not an array key then store a record in index containing index key as its key and key as its value. The record is stored in index’s list of records such that the list is sorted primarily on the records keys, and secondarily on the records values, in ascending order.

    6. If index’s multiEntry flag is set and index key is an array key, then for each subkey of the subkeys of index key store a record in index containing subkey as its key and key as its value. The records are stored in index’s list of records such that the list is sorted primarily on the records keys, and secondarily on the records values, in ascending order.

      It is valid for there to be no subkeys. In this case no records are added to the index.

      Even if any member of subkeys is itself an array key, the member is used directly as the key for the index record. Nested array keys are not flattened or “unpacked” to produce multiple rows; only the outer-most array key is.

  6. Return key.