-
Notifications
You must be signed in to change notification settings - Fork 376
Defining Models
Diogo Resende edited this page Jul 30, 2013
·
10 revisions
After [connecting](Connecting to Database), you can use the connection object (db
) to define your models. You need to specify the name of the model, a specification of the properties and options (optional). Here's a small example:
var Person = db.define('person', {
name : { type: "text" },
surname : { type: "text" },
age : { type: "number" }
}, {
methods : {
fullName: function () {
return this.name + " " + this.surname;
}
}
});
The model is called person
(which is usually the name of the table in the database), it has 3 properties (name and surname as text and age as number). The second object passed is the options. In this example there is a model method called fullName
. Here's an example of the usage of this model:
Person.get(73, function (err, person) {
if (err) throw err;
console.log("Hi, my name is %s", person.fullName());
});
This would get person with id=73
and print it's name and surname.
There are other types of [properties available](Model Properties).