Managing data using Wakanda’s SSJS API

by Christophe Keromen on Sep 17, 2009

In this post, we’ll see how simple it is to use Wakanda’s Server-Side JavaScript API to manage data. Read the previous post for a description of the Relational Model and Entity Models used in this series.

Create an entity

The following snippet of code will create an employee using the custom employees Entity Model:

var emp = new db.employees();
emp.name = 'Muller';
emp.firstname = 'Pascal';
emp.salary = 2000000;

emp.save();

return emp;

The db global object represents the currently opened database. By default, it’s the one defined in the Project.

db.employees is a class automatically and dynamically generated by Wakanda Server, implementing the description made in the Entity Model employees.

The code of this class is not available to the developer; this class inherits from an EntityModel class that provides supplementary functions available for all members, like the find() and count() functions that we will see soon.

All attributes defined in the model can be easily accessed using dot notation on the instance of the generated class.

For example, here we access the attribute name of the Entity Model employees by writing: emp.name where emp references an instance of employees.

var entitySet=db.employees.all();// select all employees
var emp=entitySet.first();//load the first entity of the collection
var name=emp.name;

Query Entity

You can query entities by using the find() or query() functions. They both expect a Query By Entity Language statement to build an entitySet, which is a collection of entities, verifying the statement. Find() adds the feature of loading the first entity of the entitySet.

var emp=db.employees.find("name=Wood AND firstname=Eric");

This code searches for employees matching the criteria and loads the first one from the collection.

var managers=db.employees.query ("manages is not null");

return managers.count();

This code searches for employees where the navigation attribute manages is not null, which means it has related entities through the corresponding relationship, so it builds a collection of employees who are managers. Notice how intuitive this syntax appears compared to performing joins at relational level.

Update entity and Iterate through an Entity Set

We want to decrease the salary of managers living in Australia. Suppose an entity method decreaseSalary has been declared in the Entity Model.

var x="Australia";
var entitySet=db.employees.query("manages is not null and livesIn.country=:x");
var emp;
for(emp=entitySet.first();emp.valid();emp.next())
{
    emp.decreaseSalary();
}

Some interesting things to  take note of in this snippet:

•    The query statement is parametered: It uses an x variable, which allows you to build more generic code.

•    An iterator is used to browse the entitySet:

valid() and next() are entity methods, valid() returning false if current entity is null, and next() navigating to the next entity in the entitySet.

•    No need to call emp.save() in the loop as emp.next() automatically checks if the current entity has been modified and saves it in case.

•    Just declare entity methods in the Entity Model and you can call them on the corresponding object in JavaScript.

Delete Entity
For example, delete all cities in our database:

var cities=db.$cities.all();
for(city=cities.first();city.valid();city.next())
{
    city.drop();
}

Associating entities
Suppose we want to associate a manager to a newly created employee.

var emp=new db.employees();
emp.firstname="Joe";
emp.name="Bird";
emp.salary=2000000;
var manager=db.employees.find("name=Wood AND firstname=Eric"); // here we simplify by supposing there is only one matching
emp.manager=manager;
emp.save();

Notice that we don’t use any foreign keys: This is left to the database layer, which knows what an emp.manager is. So we can directly put a manager object in the navigation attribute emp.manager in a very natural manner, and the association will be created.

So we just saw the basics of the Wakanda Server-Side JavaScript API to manage data. As you can see, it’s both efficient and intuitive. Consult the forthcoming API documentation to learn more…  We’ll be sure to let you know when it’s ready!

  • email
  • Facebook
  • Twitter
  • Digg
  • LinkedIn
  • viadeo FR
  • Netvibes

Tags: ,

Subscribe to commentsFollow comments

Post a comment