Invalid key path in IndexedDB: restrictions?

I am trying to create a really simple IndexedDB with some JavaScript, but it no longer works in the handler. Obviously, the browser (Chrome 57) cannot parse the keyPath (in Concepts ) of my repository.

I follow more or less the following simple examples: MDN or Opera-Dev .

Suppose I want to store objects like this in a DB:

 { "1": 23, // the unique id "2": 'Name', "3": 'Description', "4": null, "5": null } 

Here is the code:

 var sStoreNodes = 'nodes'; var sIdFieldNode = '1'; // the important part // event is fired for creating the DB and upgrading the version request.onupgradeneeded = function(event) { var db = event.target.result; // Create an objectStore for nodes. Unique key should be the id of the node, on property 1. // So ID will be the key! var objectStore = db.createObjectStore( sStoreNodes, { // changing to a plain string works, if it is a valid identifier and not just a strigified number 'keyPath' : [ sIdFieldNode ], 'autoIncrement' : false // really important here }); }; 

The error message reads as follows:

DOMException failed: Failed to execute 'createObjectStore' in 'IDBDatabase': keyPath parameter is not a valid path. in IDBOpenDBRequest.CCapIndexedDB.request.onupgradeneeded

I can also try to exclude the key path, but I wonder why this happens, and I want to, can I do this if I really need to use the (complex) key path.

Regarding the specification:

I'm not sure if this can be applied here:

A value is considered a valid key if it is one of the following ECMAScript types [ECMA-262]: a primitive Number value, a primitive String value, a Date object, or an Array object.

and what this actually means:

If the key path is DOMString, the value [to get the key path] will be DOMString equal to the key of the key. If the key path is a sequence, the value will be a new array filled with extra rows equal to each DOMString in the sequence.


Change This works if you are not using a string number, but instead a string that is a valid identifier (starting with the character [a-zA-Z]). So, 'keyPath' : 'b' is fine. I think this is because this value is used to create paths like abc .

+5
source share
1 answer

Here is the definition of the key path from the specification:

The key path is a DOMString or sequence that determines how to extract the key from the value. Valid key path:

  • Empty string DOMString.
  • An identifier that is a DOMString string corresponding to the IdentifierName product from the ECMAScript language specification [ECMA-262].
  • A DOMString string consisting of two or more identifiers separated by periods (ASCII character code 46).
  • A nonempty sequence containing only DOMStrings matching the above requirements.

For a string containing an integer, it is clear that the first, third, and fourth parameters are not applied. Secondly, we should see what IdentifierName is , which is a bit complicated, but basically it should start with a letter, underscore, or dollar sign. This means that a string containing only an integer is not a valid key path.

If you really have an object where the primary key is in a field whose name is a string containing an integer, you can either rename the field or not use the key paths (in this case, you must manually specify the key as the second argument to IDBObjectStore.add and IDBObjectStore.put ).

You contacted the key definition , which defines the valid values ​​that the key can have (for example, for the object {a: 1} where the path to the key is 'a' key is 1 , which is valid).

The other thing you are referring to is related to key paths such as abc referencing {a: {b: {c: 1}}} .

+2
source

Source: https://habr.com/ru/post/1266462/


All Articles