node mysql orm2_node.js对象关系映射 – node-orm2

这是一个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";

});

});

});

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值