文章目录

node-orm2是Node.js中sql数据库ORM框架中最流行之一,但是他都是回调的,当然也有Q.js版本,但是在koa中有yield连then都看着不舒服了,所以我用thunkify包装了一下orm的API,返回一个generator,这样在开发中就可以直接yield Persion.coFind()
项目地址:https://github.com/TivonJJ/co-orm

##安装##

npm install co-orm

##使用##

使用实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var orm = require('orm');
var coOrm = require('co-orm');
coOrm.coConnect("mysql://username:password@host/database", function (err, db) {
if (err)
console.error('mysql connection', err);
else {
demo(db);
}
});
function demo(db){
var Person = db.coDefine("User", {
name : String,
surname : String,
age : Number,
male : Boolean,
continent : [ "Europe", "America", "Asia", "Africa", "Australia", "Antartica" ], // ENUM type
photo : Buffer, // BLOB/BINARY
data : Object // JSON encoded
}
var Animal = db.coDefine("Animal",{
name:String,
age:Number
})
var personList = yield Person.coAll();
var jack = yield Person.coFind({name:"jack"});
jack.name = "new name";
yield jack.coSave();
yield Person.find({male:true}).order("id").offset(1).coRun();
Animal.coHasOne("owner",Person);
var tom = yield Animal.coGet(123);
var owner = yield tom.coGetOwner()
});
}

##支持方法##
coOrm.coConnect db.coDefine, db.coExecQuery
Model.coCreate, Model.coGet, Model.coOne, Model.coAll, Model.coCount, Model.coHasOne, Model.coHasMany,Model.coRun,Model.coFind
instance.coSave, instance.coRemove, instance.coValidate

更多ORM API https://github.com/dresende/node-orm2

此文是本站原创,转载请标注作者和链接出处!