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:
... and View JavaScript:
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:
Setting datetimes
datetimes
datetime
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:
Further Reading
When processing large datasets it is recommended to use batch operations. Please see the corresponding documentation here.
Last updated