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. 【练习】flashback基于scn的闪回查询

    1.创建表dept1: :: SCOTT@ORA11GR2>create table dept1 as select * from dept; Table created. :: SCOTT@O ...

  2. python模块概况,json/pickle,time/datetime,logging

    参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html http://www.cnblogs.com/alex3714/articles/51 ...

  3. 初学matlab----函数用法(随学习更新中)

    sort(A) 若A是向量不管是列还是行向量,默认都是对A进行升序排列. sort(A)是默认的升序,而sort(A,'descend')是降序排序. sort(A)若A是矩阵,默认对A的各列进行升序 ...

  4. JSP调用JAVA方法小例子

    用JAVA编写的函数 package doc; //定义一个包 public class Dy { //定义一个类 public static int Sub(int x,int y){ //定义函数 ...

  5. 单元测试-NUint最基本使用详解

    花了一上午,熟悉了下NUint的使用,网上找了好久,没有很详细的,全是一段文字一写什么都没了 第一步下载:程序安装   :http://launchpad.net/nunitv2/trunk/2.6. ...

  6. Python学习笔记(三)数据类型

    在内存中存储的数据可以有多种类型,在Python中,能够直接处理的数据类型有以下几种: 数字(Numbers) 字符串(String) 列表(List) 元组(Tuple) 字典(Dictionary ...

  7. 进阶篇:以IL为剑,直指async/await

    接上篇:30分钟?不需要,轻松读懂IL,这篇主要从IL入手来理解async/await的工作原理. 先简单介绍下async/await,这是.net 4.5引入的语法糖,配合Task使用可以非常优雅的 ...

  8. 解决VMware“该虚拟机似乎正在使用中”问题

    http://jingyan.baidu.com/article/4ae03de3fa2ae93eff9e6bb0.html

  9. HTML 透明、阴影,圆角等知识点

     table两个属性:cellpadding:内容与单元格边框的距离,内部距离cellspacing:单元格之间的距离,外部距离    table合并边框线:  border-collapse: co ...

  10. jQuery 屏幕遮罩

    1.先做一个可以覆盖整个屏幕的div,颜色为黑色,然后再设置透明度,作为遮罩#zhezhao { position: absolute; top: 0px; left: 0px; width: 100 ...