Manipulate DB Objects
Creating objects
A new object can be created using the syntax DB.model.create().
As an example, suppose we have a model "person" with a field "name".
We would define a variable for the object, and initialise it as follows:
View XML:
<view ...>
<var name="visitor" type="person" />
</view>... and View JavaScript:
function init() {
view.visitor = DB.person.create();
view.visitor.name = "John";
}All objects that are defined as variables in a view are saved automatically whenever a view is dismissed, but not when the user presses the back button. However, when defining an object only in JavaScript/TypeScript (and not in the view), you need to save the object manually:
var visitor = DB.person.create();
visitor.name = "John";
visitor.save();let visitor = await DB.person.create();
visitor.name = "John";
await visitor.save();Setting datetimes
datetimesdatetime fields are represented as the JavaScript/TypeScript Date type. To set a datetime to the current date and time, use new Date(). To set it to a specific time and date, use new Date(year, month, day, hour, minute, second). For more details, see this article at Mozilla.
Example:
Setting dates
date fields are represented as a Day object. To set a date field to the current date, use new Day(). To set it to a specific date, use new Day(year, month, day).
Examples:
The Day object has additional functionality:
Deleting objects
An object can be deleted using its destroy() function.
As an example, suppose we have a model "person":
View XML:
... and View JavaScript:
Setting relationships
Unlike fields, a relationship is set using a setter function.
As an example, suppose that in our Data Model every person is part of a household:
In our view we define our household and person:
... and initialize them in JavaScript:
As you can see, we set the "household" relationship on the "member" by calling the household() function and passing the related "household" object as an argument.
As a more typical example, the household will be created in another view and passed as a parameter:
... and the JavaScript:
Getting relationships
Getting a related object works that a particular object belongs to is done by calling a getter function, as seen below:
Last updated