- please test-report configurable constraints on MySQL;
Node-ORM is a multi-database Object-Relational Mapping for NodeJS.
- validators;
- beforeSave/afterSave hooks;
npm install orm
var orm = require("orm");
var dsn = "mysql://username:password@hostname/database";
orm.connect(dsn, function (success, db) {
if (!success) {
console.log("Could not connect to database!");
return;
}
// you can now use db variable to define models
});
var orm = require("orm");
var mysql = require("mysql");
var client = mysql.createClient({
user: 'root',
password: 'root'
});
orm.connect("mysql", client, function (success, db) {
// same as above...
});
var Person = db.define("person", {
"name" : { "type": "string" },
"surname": { "type": "string", "default": "" },
"age" : { "type": "int" }
}, {
"methods" : {
"fullName" :function () {
return this.name + " " + this.surname;
}
}
});
Person.sync();
var John = new Person({
"name" : "John",
"surname" : "Doe",
"age" : 20
});
console.log("Hello, my name is " + John.fullName() + " and I'm " + John.age + " years old");
John.save(function (err, JohnCopy) {
if (!err) {
console.log("Saved! ID=" + John.id); // you can use John or JohnCopy
} else {
console.log("Something went wrong...");
console.dir(err);
}
});
Person.hasOne("father", Person);
// or just
Person.hasOne("mother"); // defaults to same model
// this will create "person_friends" table with "person_id" and "friend_id"
Person.hasMany("friends", Person, "friend");
John.setFather(Jeremy, function () {
John.setMother(Jane, function () {
John.addFriends(Jeremy, Jane, function () {
console.log("Jeremy and Jane (John's parents) are now his friends too");
});
});
});
John.getFather(function (JohnFather) {
console.log("John's father is " + JohnFather.name);
});
John.unsetMother(function () {
console.log("John has no mother now!");
});
John.removeFriends(Jeremy, Jane, function () {
console.log("John has no friends now!");
});
// or just don't send any, all will be removed
John.removeFriends(function () {
console.log("John has no friends now!");
});
Check out API for more.
Currently supported database types are:
- MySQL (via https://github.com/felixge/node-mysql)
- PostgreSQL (via https://github.com/brianc/node-postgres)
- MongoDB (alpha quality, via https://github.com/christkv/node-mongodb-native)
Currently, following data types affect only .sync() method (i.e. CREATE TABLE). Others, like .save() method (i.e. UPDATE), does not perform any data type checks.
Name | Description | MySQL Type | PostgreSQL Type | MongoDB Type |
---|---|---|---|---|
string | Small text | VARCHAR(255) | VARCHAR(255) | String |
text | Big text | TEXT | TEXT | String |
int, integer, num, number | Signed integer | INT | INTEGER | Int |
float | Floating point number | FLOAT | REAL | Float |
bool, boolean | True or false value | TINYINT(1) (true=1, false=0) | BOOLEAN | Boolean |
date | Date/time value (seconds precision) | DATETIME | TIMESTAMP | ? |
data | Binary data | BLOB | BYTEA | String |
enum | Enumerated value | ENUM | ENUM | String |
struct, object | Generic (and simple) object | TEXT (saved as JSON) | TEXT (saved as JSON) | Object |