indexeddb,如何使用游标更新对象数组

时间:2018-03-16 04:37:42

标签: javascript html indexeddb

问题:如何存储一个包含大量对象的大型数组,所有这些对象都有5个属性,并且必须更新除id属性之外的所有属性。此外,为什么下面的代码不起作用,我如何格式化以处理主要问题?

我看过的信息:

https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openCursor

https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB

https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/createIndex

注意:我知道setInterval及其低效率,它用于测试目的,所以我不必多次点击来检查结果。

<html>
<body>
<script type="text/javascript">
    let count =0;
    let storeBuilt = false;
    const dbName = "the_name";
    let version=82;
    let storeName= "store82";
    let storeBuilding= false;
    setInterval(build,1000/24);
    function build(){
        hello()
    }

    function hello(){

        let customerData = [];

        for(let i=0;i<=50000;i++){
            customerData.push({name:"bob",minX:random(),minY:random(),maxX:random(),maxY:random(),id:random()})
        }



        let request = indexedDB.open(dbName, version);

        request.onsuccess= function(event){
            let db = event.target.result;
            let transaction = db.transaction( storeName,"readwrite").objectStore(storeName);




            if( storeBuilding=== false&& storeBuilt=== false){
                storeBuilding= true;
                let additem = addData(customerData, transaction);
                additem.onsuccess= function(e){storeBuilt=true}
            } else if (storeBuilt=== true){

                let updateitem= updateData(customerData, transaction);
            }



        };





        request.onupgradeneeded = function(event) {
            let db = event.target.result;

            // Create an objectStore to hold information about our customers. We're
            // going to use "ssn" as our key path because it's guaranteed to be
            // unique - or at least that's what I was told during the kickoff meeting.
            let objectStore = db.createObjectStore(storeName, {keyPath:"names",autoIncrement:true});
            objectStore.createIndex("name","name",{unique:true});

            // Use transaction oncomplete to make sure the objectStore creation is
            // finished before adding data into it.
            objectStore.transaction.oncomplete = function(event) {
                // Store values in the newly created objectStore.
                let customerObjectStore = db.transaction(storeName, "readwrite").objectStore(storeName);





            }
        };}










    function random (){
        return (Math.floor((Math.random() * 10) + 1))
    }
    function addData(data,transaction){
        return transaction.add(data)
    }

    function updateData(data,transaction){

        let openCursor = transaction.index("name").openCursor();

        openCursor.onsuccess= function(event){
            let cursor = event.target.result;

            if (cursor){
                alert (cursor);
                for(let I in data){
                    let item = data[I];
                    if(item.id === cursor.value.id){
                        let updateProperty = cursor.value;
                        updateProperty.minX = item.minX;
                        cursor.update(updateProperty);
                        cursor.continue()
                    }
                }
            }{alert("none")}
        }
    }

    function deleteData(data,transaction){


    }



</script>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

不确定我是否清楚地理解了这个问题,但通常您需要从对象库加载对象,修改每个对象的属性,然后将对象存储在对象库中。有几种方法可以做到这一点。一种方法是使用cursor.update,但我认为你根本不需要这样做。只需覆盖对象。

function storeThings(db, things, callback) {
  var txn = db.transaction('store82', 'readwrite');
  txn.oncomplete = callback;
  var store = txn.objectStore('store82');
  for(var thing of things) {
    store.put(thing);
  } 
}

function open(callback) {
  var request = indexedDB.open();
  request.onsuccess = _ => callback(request.result);
}

var things = [{id:1}, {id:2}, {id:3}];
open(db => storeThings(db, things, _ => console.log('done')));

我使用IDBObjectStore.prototype.put来存储对象。 put方法将创建或覆盖商店中的对象。当根据键路径找不到匹配的对象时,它将在商店中创建一个新对象。当找到匹配的对象时,它将替换存储中的现有对象。

在您的情况下,您使用ssn字符串作为键路径。因此,换句话说,如果找不到ssn,它将创建新人,或者如果找到ssn,则会覆盖人员。您只需要确保在传递给的每个person对象中定义了ssn属性,否则indexedDB会抱怨。