Storing Data
Using the insert
, update
and remove
methods
To store data in the database, you must first have an entity created and published. For the examples below we will continue using the Book
entity from the first steps of the Data Access Objects compendium.
insert
Insert new rows in your database entity using the insert
method.
var rows = db.getDao("Book").insert({ "ds_book": "Harry Potter and Hallows of Death" });
This shall insert a new row in our entity. Don't mind the primary key, the platform will already provide values for that column. You can retrieve that id
using a syntax like that:
// Create your object with the data you wish to insert
var book = { "ds_book": "Harry Potter and Hallows of Death" };
// Invoke the insert method with that object
var rows = db.getDao("Book").insert(book);
// retrive the data form the id_book attribute
var bookId = book["id_book"];
If you have any required columns with default values, those will be populated before the insert automatically.
update
We can also update values from the Entity Data Access interface:
// Update it ...
var book = {
"id_book": 15,
"ds_author": "J. K. Rowling"
};
dao.getDao("Book").update(book);
Updates are based on the primary key value, for this example, we used the id_book
. You must provide the value of your key to update values in that row.
Using filter
For the best performance in your transactions always use the Primary Key for the updates. When that is not possible you can also use the
filter
interface for updates.
remove
We can also remove values from the Entity Data Access interface:
// Remove it ...
var idBook = 15;
dao.getDao("Book")
.filter('id_book').equalsTo(idBook)
.delete();
Wildcards
For updates and inserts you have some wildcard values that are automatically replaced from the platform. Check it out:
var rows = db.getDao("Book").update({
"id_book": 15
"dt_lastaccess": "$now"
});
The dt_lastaccess
column is a Datetime
field. In that case you can use the $now
wildcard which will be automatically replaced by the current date/time in the server.
$now
Current Date/Time (Datetime, Date and Long columns)
$user
Current User Id (Integer and Long columns)
Updated about 3 years ago