这是一个node.js对象关系映射模块。
安装
npm install orm
支持Node.js版本
支持:0.12-4.0+
如果你希望可以本地运行测试的话,可以在 Travis CI
?上运行:
npm test
数据库管理系统支持
MySQL & MariaDB
PostgreSQL
Amazon Redshift
SQLite
MongoDB
功能特征:
创建模型,同步,下拉,批量创建,获取,查找,删除,计数,聚合函数
创建模型关联,查找,检查,创建和删除
定义自定义验证
模块实例缓存和完整性
示例
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, // FLOAT
male : Boolean,
continent : [ "Europe", "America", "Asia", "Africa", "Australia", "Antartica" ], // ENUM type
photo : Buffer, // BLOB/BINARY
data : Object // JSON encoded
}, {
methods: {
fullName: function () {
return this.name + ' ' + this.surname;
}
},
validations: {
age: orm.enforce.ranges.number(18, undefined, "under-age")
}
});
// add the table to the database
db.sync(function(err) {
if (err) throw err;
// add a row to the person table
Person.create({ id: 1, name: "John", surname: "Doe", age: 27 }, function(err) {
if (err) throw err;
// query the person table by surname
Person.find({ surname: "Doe" }, function (err, people) {
// SQL: "SELECT * FROM person WHERE surname = 'Doe'"
if (err) throw err;
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) {
// err.msg = "under-age";
});
});
});
});
});