Object Relational Mapping
Install
npm install orm@2.0.0-alpha6
Despite the alpha tag, this is the recommended version for new applications.
DBMS Support
Introduction
This is a node.js object relational mapping module.
Here is an example on how to use it:
var orm = require('orm');
orm.connect("mysql://username:password@host/database", function (err, db) {
if (err) throw err;
var Person = db.define('person', {
name : String,
surname : String,
age : Number,
male : Boolean,
continent : [ 'Europe', 'America', 'Asia', 'Africa', 'Australia', 'Antartica' ],
photo : Buffer,
data : Object
}, {
methods: {
fullName: function () {
return this.name + ' ' + this.surname;
}
},
validations: {
age: orm.validators.rangeNumber(18, undefined, 'under-age')
}
});
Person.find({ surname: "Doe" }, function (err, people) {
console.log("People found: %d", people.length);
console.log("First person: %s, age %d", people[0].fullName(), people[0].age);
people[0].age = 16;
people[0].save(function (err) {
});
});
});
Models
A Model is a structure binded to one or more tables, depending on the associations. The model name is assumed to be the table name. After defining a model you can use it to manipulate the table.
After defining a Model you can get a specific element or find one or more based on some conditions.
Finding Items
Model.get(id, [ options ], cb)
To get a specific element from the database use Model.get
.
Person.get(123, function (err, person) {
});
Model.find([ conditions ] [, options ] [, limit ] [, order ] [, cb ])
Finding one or more elements has more options, each one can be given in no specific parameter order. Only options
has to be after conditions
(even if it's an empty object).
Person.find({ name: "John", surname: "Doe" }, 3, function (err, people) {
});
If you need to sort the results because you're limiting or just because you want them sorted do:
Person.find({ surname: "Doe" }, "name", function (err, people) {
});
Person.find({ surname: "Doe" }, [ "name", "Z" ], function (err, people) {
});
There are more options that you can pass to find something. These options are passed in a second object:
Person.find({ surname: "Doe" }, { offset: 2 }, function (err, people) {
});
Available options
offset
: discards the first N
elementslimit
: although it can be passed as a direct argument, you can use it here if you preferonly
: if you don't want all properties, you can give an array with the list of properties you want
Chaining
If you prefer another less complicated syntax you can chain .find()
by not giving a callback parameter.
Person.find({ surname: "Doe" }).limit(3).offset(2).only("name", "surname").run(function (err, people) {
});
Associations
An association is a relation between one or more tables.
hasOne vs. hasMany Associations
If you have a relation of 1 to 0 or 1 to 1, you should use hasOne
association. This assumes a column in the model that has the id of the other end of the relation.
var Person = db.define('person', {
name : String
});
var Animal = db.define('animal', {
name : String
});
Animal.hasOne("owner", Person);
Animal.get(123, function (err, Foo) {
Foo.getOwner(function (err, John) {
});
});
For relations of 1 to many you have to use hasMany
associations. This assumes another table that has 2 columns, one for each table in the association.
var Person = db.define('person', {
name : String
});
Person.hasMany("friends");
Person.get(123, function (err, John) {
John.getFriends(function (err, friends) {
});
});
The hasMany
associations can have additional properties that are assumed to be in the association table.
var Person = db.define('person', {
name : String
});
Person.hasMany("friends", {
rate : Number
});
Person.get(123, function (err, John) {
John.getFriends(function (err, friends) {
});
});
If you prefer you can activate autoFetch
. This way associations are automatically fetched when you get or find instances of a model.
var Person = db.define('person', {
name : String
});
Person.hasMany("friends", {
rate : Number
}, {
autoFetch : true
});
Person.get(123, function (err, John) {
});
You can also define this option globally instead of a per association basis.
var Person = db.define('person', {
name : String
}, {
autoFetch : true
});
Person.hasMany("friends", {
rate : Number
});