-
Notifications
You must be signed in to change notification settings - Fork 372
Model Properties
Diogo Resende edited this page Jul 30, 2013
·
9 revisions
Models and some associations can have one or more properties. Every property has a type and a couple of optional settings you can choose (or leave the default).
The supported types are:
-
text: A text string; -
number: A number value; -
boolean: A true/false value; -
date: A date object; -
enum: A value from a list of possible values; -
object: A JSON object; -
point: A N-dimensional point (not generally supported); -
binary: Binary data.
Each type can have additional options. Here's a model definition using most of them:
var Person = db.define("person", {
name : { type: "text", size: 50 },
surname : { type: "text", defaultValue: "Doe" },
male : { type: "boolean" },
vat : { type: "number", rational: false, unique: true },
country : { type: "enum", values: [ "USA", "Canada", "Rest of the World" ] },
birth : { type: "date", time: false }
});All types support required (boolean), unique (boolean) and defaultValue (text). Text type also supports maximum size of string (number) and big (boolean - for very long strings). Number type also supports rational (boolean - float/integer), size (number - byte size) and unsigned (boolean). Date type supports time (boolean).
Note that 8 byte numbers have limitations.
If you're using default options, you can use native types to specify property types:
var Person = db.define("person", {
name : String,
male : Boolean,
vat : Number,
birth : Date,
country : [ "USA", "Canada", "Rest of the World" ],
meta : Object, // JSON
photo : Buffer // binary
});