Update and delete operations in Jaydata

I am trying to write a simple CRUD function in Jaydata, I wrote this simple code for the update operation:

SampleClass.prototype.Load = function(input1,callback) { var param='it.Name=="'+input1+'"'; this.data.items.filter(param).forEach(function(ii) { callback(ii); }); this.data.items.saveChanges(); }; 

so when i call:

 t.Load('Entry4',function(res){console.log(res.Name)}) 

It works like a charm! But if I call the update operation for the callback, for example:

 t.Load('Entry4',function(res){res.Name="Entry5"}) 

This does not change anything in the database. I saw something like the beginTransaction function, as in http://jaydata.org/examples/JayDataPro/ToDoList_complex , but I could not understand its essence.

+4
source share
2 answers

Special thanks to Gabor Dolla

To update a value in JayData:

  • The database must contain the primary key .
  • Change non-key attributes
  • After that, call the asynchronous save function ().

solution of the question: after changing the definition of the field of the object as follows:

 Name{ type:'string', **key:true**} 

You can request something, but only change non-keys from them

 t.Load('Entry4',function(res){res.LastName="Entry5";res.save()}); 
+3
source

I think that only attach () is missing before modifying an object.

 this.data.items.attach(res); 

By the way, I would move saveChanges to the update callback because you don't need it in the readonly script.

+1
source

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


All Articles