javascript - IndexedDB: Modifying pre-existing objects in an object store -


i trying wrap head around indexeddb.

i created object store uses key-generator no key-path.

var objectstore = db.createobjectstore("domains", {autoincrement: true }); objectstore.createindex("domain", "domain", { unique: true, multientry: true }); 

in order access records, created index references object store. index's key path domain property of records.

here example of 1 record in object store might like:

{ domain: ["stackexchange",            "stackoverflow",            "superuser",            "askubuntu",            "serverfault"],     bold          : ["<strong>",1],    italic        : ["<em>",1],   strikethrough : ["<del>",1],   superscript   : ["<sup>",1],   subscript     : ["<sub>",1],   heading1      : ["<h1>",1],   heading2      : ["<h2>",1],   heading3      : ["<h3>",1],   blockquote    : ["<blockquote>",1],   code          : ["<code>",1],   newline       : ["<br>",2],   horizontal    : ["<hr>",2] }, 

each entry appearing in domain property guaranteed unique among set consisting of union of every domain key's value out of every record of object store. i.e. won't find of these domains anywhere else in object store.

i need let user's edit objects in object store. provided single domain, can retrieve associated object this:

var dbopenrequest= window.indexeddb.open(db_name, db_version); dbopenrequest.onsuccess= function(event){        var db= dbopenrequest.result;     var domains=db.transaction('domains', 'readwrite').objectstore('domains');      var query = domains.index("domain").get("stackoverflow");    }; 

now, here part confused. once user has changed properties want edit/added new properties please, need save changes replacing object retrieved above new object user created. (or modify original object , save changes).

i think need use idbobjectstore.put() this.

the put() method takes 2 parameters, 1 required , 1 optional:

value     value stored. key     key use identify record. if unspecified, results null. 

the second parameter presumably optional instances in using inline keys. using out-of-line keys generated key generator.

how can (auto-generated, out-of-line) key of object retrieved through index, can communicate object store want modify pre-existing object , not create new one?

is there way should going this?

below text indexeddb specs indexes.

each index has unique flag. when flag set true, index enforces no 2 records in index has same key. if record in index's referenced object store attempted inserted or modified such evaluating index's key path on records new value yields result exists in index, attempted modification object store fails.

so, "put()" cannot used if unique index created on property. so, instead can create primary key i.e. use keypath while creating object store.

var objectstore = db.createobjectstore("domains", {keypath: "domain"}); 

check below 2 urls more insight not possible perform update if unique index exists on it.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -