Start a new topic

Update array of entities nodejs

I am trying to save a bulk update of existing entities using the save method but it returns the error "Unable to create an array of entities, Please create entities one by one."

Is there any way around trying to save the entities one by one?



Hi Michael,


You can save/update only one entity at a time with one call to dataStore.save. If you want to update multiple entities at once, you can use 'For' loop. Check following code snippet:


                let dataStore = DataStore<Book>.collection(.network)


                let book0 = Book()
                book0.authorName = "a1"
                book0.pages = 1
                book0.title = "t1"


                let book1 = Book()
                book1.authorName = "a2"
                book1.pages = 2
                book1.title = "t2"


                let book2 = Book()
                book2.authorName = "a3"
                book2.pages = 3
                book2.title = "t3"
           
                var array = [book0,book1,book2];
               
                for i in 0...2 {
                   
                    let book = array[i]
                   
                    dataStore.save(book, options: nil) { (result: Result<Book, Swift.Error>) in
                        switch result {
                        case .success(let book):
                            print("Book: \(book)")
                        case .failure(let error):
                            print("Error: \(error)")
                        }
                    }
                }



If you have a JSON or CSV file of data records, you can import that data into the collection using 'Import' feature. Go to your collection-> Settings-> Import.



Thanks,

Pranav




Thanks Pranav,

I have a related issue, I am using a cache datastore and often have save calls inside my queries subscription calls. The problem is the save operation is called twice, once when the cache query completes then when the network query completes. This results in the error:

SyncError: Data is already being pushed to the backend. Please wait for it to complete before pushing new data to the
 backend.

Is there a best practice for handling this case?

Sorry Pranav on closer inspection I don't understand your syntax for saving the Book, could you explain? This does not appear to be a promise as outlined in docs.

Michael,


Now you are using '.cache' datastore. Please take a look at the example mentioned on this link. It says while saving an entity, the entity will be saved to the device and your backend. If you do not have a network connection, the entity will be stored locally. You will need to push it to the server when network reconnects, using dataStore.push().


Please ignore my suggestion about using 'For' loop as it is Swift code. Regarding your question about 'Buld Update', it is not supported. In a single request, you can save/update only one entity at a time. For 'Bulk Upload'/'Bulk Saving', see the CSV/JSON import feature on the Kinvey console (navigate to the collection, click Settings, then click Import Data). Bulk update is not supported. You can fetch and delete multiple entities in a single request using a query.


Thanks,

Pranav


I understand the an array of entities can't be saved at one time, but the issue is if I want to save them one at a time the issue is the first save operation does not complete before the following save operation. Is there any guidance you can offer in this scenario?


Login or Signup to post a comment