The getKey( ) method returns the primary key value of the entity to which it is applied.
var ent = ds.Person.find("name = :1", "Smith");
var key = ent.getKey();
This method is useful to identify the entity to update for instance. The KEY value is also needed when you pass a POST request
The getModifiedAttributes( ) method returns an array containing the names of attributes that have been modified in the entity. This method is useful in validation control functions.(validate events for instance or methods, functions..)
Array of attributes
model.Invoice.events.validate = function(event) {
//get the array of attributes that were changed
var attributeMods = this.getModifiedAttributes();
//cycle through them
attributeMods.forEach(function(attribName){
switch (attribName){
case 'ID': //if the ID was changed
case 'InvoiceDate': //or the invoiceDate was changed
//assign an error number and message
result = {error: 1000, errorMessage: 'Invalid Change !'};
break;
}
});
return result; //return result regardless
}
//Then each time you perform a save action the entity will go through this validation process
var a = ds.Invoice.first();
a.ID = 12;
a.save();
// Running this will thow the error message 'Invalid Change !
The getStamp( ) method returns the current value of the internal stamp of the entity. The internal locking stamp is automatically incremented by Wakanda each time the entity is saved on the server. It manages concurrent user access
The entity's STAMP value is needed when executing POST request throught XRH to update an entity
returns the timestamp of the last save of the entity to which it is applied
Date object in local format
returns True or False depending on whether the entity iterator points to an entity that is currently loaded in memory
returns True or False depending on whether the entity to which it is applied has been modified since the last time it was loaded from the datastore.
You can use this method to find out if you need to save the entity. This method always returns True for a new entity.
emp = ds.Employee.first();
//... process the data in the entity
if (emp.isModified()) // if at least one of the attributes has been changed
emp.save();
// otherwise, no need to save the entity
returns True or False depending on whether the entity to which it is applied has just been created in the datastore (and not saved yet)
The lock( ) method tries to lock the entity for the session and returns true if the entity is locked successfully, or false if the entity is already locked by another session.
True if the entity is locked for the session, false otherwise
puts the entity pointer on the next entity within an iteration of entities
for
loop var myColl = ds.People.query("nationality = :1", "FR");
for (var onePerson = myColl.first(); onePerson != null; onePerson=onePerson.next())
// onePerson will return null after the last entity of the collection
{
onePerson.name = onePerson.name.toUpperCase();
onePerson.save();
}
while
`
javascript
var myColl = ds.People.query("nationality = :1", "FR");
var onePerson = myColl.first();
while (onePerson != null)
{
onePerson.name = onePerson.name.toUpperCase();
onePerson.save();
onePerson=onePerson.next();
}
reloads the entity as it is stored in the datastore
releases the entity from memory
In principle, the garbage collection mechanism of JavaScript will purge unused objects from memory. However, this mechanism operates * * autonomously and can prove to be insufficient in some cases. For example, when the server has loaded dozens of large pictures on the JavaScript side, only references are handled, which may not require the intervention of the garbage collector.
However, on the server side, the memory is in high demand. In a case like this, it is useful to be able to "force" entities to be unloaded using the release( ) method.
Note that after calling release( ), if you want to make sure that the JavaScript reference to an entity has been deleted without having to wait for garbage collection (and thus for a subsequent access to the entity to return an error), you must force its value to null.
#### Example :
```javascript
myEntity.release(); //unload the entity from the server
myEntity = null; // delete its reference
removes the entity from the datastore
When this method is executed, it triggers a call to the remove event on the server if it has been set for the entity's datastore class or one of the datastore class attributes.
For more information and examples about the remove() method, check its description here (collection section)
Saves the changes made to the entity in the datastore
How to update an existing entity :
var a = ds.Employee.first();
a.firstname = "MyNewFirstName" ;
a.lastname = "MyNewLastName" ;
a.save();
How to create then save an entity:
new ds.Employee(
{
firstname : "MyNewFirstName" ,
lastname : "MyNewLastName"
}).save();
***
When executing a save() action, on the server side the events (if defined) are performed in the following order :
//You can intercept and manage the error returned by the engine when the entity's internal stamp being saved is different from the one that is saved in the data
//To do this, you can place the save statement in a try/catch type structure. For example:
// select an entity and change its name to uppercase
function toUpperEmployee(lastName, firstName)
{
var emp = ds.Employee.find("lastName = :1 and firstName = :2", lastName, firstName);
emp.lastName.toUppercase();
try
{
emp.save();
}
catch(e)
{ ... // put the error-processing code here
}
returns a string representation of the entity or entity collection Examples are available here
The unlock() method unlocks the entity in the running session. This method must be called after the lock( ) method to unlock the entity for the other sessions.
The validate( ) method passes the entity through the validation process. Precisely it means that on the server, the code associated with the validate (attribute) and validate (datastore class) event(s) is executed.
True in case of success (the entity successfully passes the validation process)
Returns the datastore class (object of the DatastoreClass type) of the entity.
For more information go here