nodejs mysql ORM

比node-mysql好用多了。

bookshelf

支持restful功能,用到的时候研究下:https://www.sitepoint.com/getting-started-bookshelf-js/

create

// Data其实是一个model
new Data({
'data_name': 'bookshelf',
'data_info': 'info',
'user_id': 1,
'add_time': '2016.07.24',
'is_del': 0
}).save().then(function(model){
console.log(util.inspect({model: model}));
}).catch(function(err){
console.log(err);
});
// Data() 包含参数时,默认为update操作
// 如果想create时,需要把参数写在save中
// 同时插入多条数据

.collection()的参数是:

[{id: 1, name: 'name1'}, {id: 2, name:'name2'}];

endTable.collection(JSON.parse(req.body.e_t)).invokeThen('save', null, {method: 'insert'}).then(function(model){

console.log('success');

console.log(util.inspect({model: model}));

res.send({

"name": '/submit'

})

}).catch(function(err){

console.log('fail');

console.log(err);

res.send({

"name": '/submit'

})

});

select

Data.where('data_id', 1).fetch().then(function(data){
console.log(data.toJSON());
});
Data.where({'data_id': 1, argu2: '123'}).fetch().then(function(data){
console.log(data.toJSON());
});
// Data.fetchAll() new Data({
'data_id': 81
}).fetch().then(function(model){
console.log(model.get('data_name'))
}) // 查询所有的
basemodels.Users.forge().fetchAll().then(function(data){
cb(null, data.toJSON());
})

association 看不懂

transaction 看不懂 http://www.tuicool.com/articles/JjQJv2E

query

User.query('where', 'user_id', '=', 12)
.fetch()
.then(function(model){
console.log(model.get('user_name'));
})
// 多个查询条件 andWhere
User.query({
where: {user_id: 22},
orWhere: {user_name: 'admin'}
})
.fetch()
.then(function(model){
console.log(model.get('user_id'));
})
// 复杂的查询条件
model.query(function(qb) {
qb.where('other_person', 'LIKE', '%Demo').orWhere('other_id', '>', 10);
})
// model.query() : 返回查询构造器

where

// !=
model.where('favorite_color', '<>', 'green')
// ==
model.where('favorite_color', 'red')
// 多个条件
model.where({favorite_color: 'red', shoe_size: 12})

update

new Data().where("data_id", '=', 81).save({
"user_id": 2
}, {patch: true}).then(function(data){
console.log(util.inspect({data: data}));
}).catch(function(err){
console.log(util.inspect({err: err}));
})
// 只更新传递的字段 添加{patch: true}
// 必须要有where

del

new Data().where("data_id", 80).destroy().then(function(data){
console.log(util.inspect({data: data}));
}).catch(function(err){
console.log(util.inspect({err: err}));
})
// 必须要有where

belongsTo

Data.where({'id': 1}).fetch({withRelated: ['user']}).then(function(data){
console.log(data.related('user').toJSON());
})
// data: id, user_id
// user: id
// 表的字段有一定的要求

hasMany

User.where({id: 12}).fetch({withRelated: ['result']}).then(function(user) {
console.log(JSON.stringify(user.related('result')));
});

安装

npm install mysql --save

npm install knex --save

npm install bookshelf --save

ORM要用!但关键部位不能用!

因为对于一般的Web应用开发来说,使用ORM确实能带来上述的诸多好处,而且在大部分情况下涉及不到ORM的不好的地方。但是在系统里面有大数据量、大运算量、复杂查询的地方,就不要用ORM。ORM的性能问题将给你带来灾难。在这些地方就可以使用纯SQL或者其他简单轻量的DB Helper库了。在详细了解ORM之后,你就可以扬长避短让ORM发挥其最大效用了。

bookshelf的更多相关文章

  1. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  2. Bookshelf 2

    Bookshelf 2 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  3. POJ 3628 Bookshelf 2(01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9488   Accepted: 4311 Descr ...

  4. POJ3628:Bookshelf 2【01背包】

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  5. Node的关系型数据库ORM库:bookshelf

    NodeJs 关系数据库ORM库:Bookshelf.js bookshelf.js是基于knex的一个关系型数据库的ORM库.简单易用,内置了Promise的支持.这里主要罗列一些使用的例子,例子就 ...

  6. POJ 3268 Bookshelf 2 动态规划法题解

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  7. HOJ-2056 Bookshelf(线性动态规划)

    L is a rather sluttish guy. He almost never clean up his surroundings or regulate his personal goods ...

  8. poj_3628 Bookshelf 2

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  9. 书架 bookshelf

    书架 bookshelf 题目描述 当Farmer John闲下来的时候,他喜欢坐下来读一本好书. 多年来,他已经收集了N本书 (1 <= N <= 100,000). 他想要建立一个多层 ...

随机推荐

  1. #pragma pack(n)

    #pragma pack(n) 重要规则: 1,复杂类型中各个成员按照它们被声明的顺序在内存中顺序存储,第一个成员的地址和整个类型的地址相同: 2,每个成员分别对齐,即每个成员按自己的方式对齐,并最小 ...

  2. nodejs http 请求延时的处理方法(防止程序崩溃)

    有时候因为接口没开,或者其他原因,导致http.request 请求延时,一直耗费资源不说,还会导致程序报错崩溃,延时处理其实也是一种错误处理. 直接上代码: var APIGET = functio ...

  3. ZC706以太网扩展板接口

    端口 BANK-VADJ PIN NAME PIN_NAME RGMII0 rgmii_0_txc 10 AB14 LA15_N PHY_0_GTXCLK rgmii_0_rxc 10 AE13 LA ...

  4. 百度地图API示例之添加定位相关控件

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...

  5. Android开发--adb,SQLite数据库运用

    一.玩转adb  adb的全称为Android Debug Bridge,就是起到调试桥的作用.   adb有什么用?:借助adb工具,我们可以管理设备或手机模拟器的状态.还可以进行很多手机操作,如安 ...

  6. 异步编程:When.js快速上手

    前些天我在团内做了一个关于AngularJS的分享.由于AngularJS大量使用Promise,所以我把基于Promise的异步编程也一并介绍了下.很多东西都是一带而过,这里再记录下. Angula ...

  7. JavaScript入门

    本篇内容是学习慕课网相关课程后,总结出可能未来会忘记的内容 (一)JavaScript入门操作 1.js代码插入位置,以及执行顺序 <head> <script type=" ...

  8. 2016.02.14 总结JS事件

    今天主要总结JS事件的基本知识以及使用技巧,并作出相应的DEMO.

  9. svn 命令行创建和删除 分支和tags

    创建分支 svn cp -m "create branch" http://svn_server/xxx_repository/trunk http://svn_server/xx ...

  10. LoadRunner参数化详解(转)

    距离上次使用loadrunner 已经有一年多的时间了.初做测试时在项目中用过,后面项目中用不到,自己把重点放在了工具之外的东西上,认为性能测试不仅仅是会用工具,最近又想有一把好的利器毕竟可以帮助自己 ...