bookshelf
nodejs mysql ORM
比node-mysql好用多了。
支持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的更多相关文章
- POJ3628 Bookshelf 2(01背包+dfs)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8745 Accepted: 3974 Descr ...
- Bookshelf 2
Bookshelf 2 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3628 Bookshelf 2(01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9488 Accepted: 4311 Descr ...
- POJ3628:Bookshelf 2【01背包】
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- Node的关系型数据库ORM库:bookshelf
NodeJs 关系数据库ORM库:Bookshelf.js bookshelf.js是基于knex的一个关系型数据库的ORM库.简单易用,内置了Promise的支持.这里主要罗列一些使用的例子,例子就 ...
- POJ 3268 Bookshelf 2 动态规划法题解
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- HOJ-2056 Bookshelf(线性动态规划)
L is a rather sluttish guy. He almost never clean up his surroundings or regulate his personal goods ...
- poj_3628 Bookshelf 2
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- 书架 bookshelf
书架 bookshelf 题目描述 当Farmer John闲下来的时候,他喜欢坐下来读一本好书. 多年来,他已经收集了N本书 (1 <= N <= 100,000). 他想要建立一个多层 ...
随机推荐
- 简单实用的Windows命令(一)
前几天新买了一台笔记本电脑,使用了一下几个简单的查看电脑配置的命令,觉得非常的不错,在此记录一下 一:运行命令的方式有两种 1:使用快捷键WIN+R,然后在弹出的“运行”对话框中输入对应的命令 2:在 ...
- Window中常见的dos命令
1.如何实行操作dos命令:如果是Windows电脑,从开始--->所有程序---->附件--->命令提示 这样就可以开始命令提示符了 2关于一些dos命令: 2.1 盘符切换:盘符 ...
- Hadoop HDFS编程 API入门系列之简单综合版本1(四)
不多说,直接上代码. 代码 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs4; import java.io.IOException; import ja ...
- WPF的图片操作效果(一):RenderTransform
一.RenderTransform类的成员: 1.TranslateTransform 平移效果 2.RotateTransform 旋转效果 3.ScaleTransform 缩放效果 ...
- [纯小白学习OpenCV系列]官方例程00:世界观与方法论
2015-11-11 ----------------------------------------------------------------------------------- 其实,写博 ...
- MFC中控制COMBOBOX控件的下拉框高度
这是使用Visual Stiduo的小技巧哦.今天上网找来的.在界面设计面板上,点击ComboBox的下拉箭头,会另外出现一个虚边框.可以调整其大小.这个就是实现运行的时候下拉边框的默认值啦.
- eval 函数的应用 (去除包装在列表外面的引号)
a="[u'ANDROID-5a9ac5c22ad94e26b2fa24e296787a35', u'0', 0, 0, 0, 1]" 此时的a是一个字符串,目的是要去掉a上面的引 ...
- OA 办公自动化系统:权限管理模块的实现原理思路
OA系统分有许多的模块,如系统管理模块.等一些比较高级的业务操作.此类业务是不允许让普通员工来操作的,思路如下: 给系统添加角色表,每个用户对应一个角色,每个角色可以拥有多个权限, 如下:创建权限表( ...
- Python_sklearn机器学习库学习笔记(三)logistic regression(逻辑回归)
# 逻辑回归 ## 逻辑回归处理二元分类 %matplotlib inline import matplotlib.pyplot as plt #显示中文 from matplotlib.font_m ...
- C++和C#混合编程
最近需要利用C++和C#混合编程,然后就写了一个C#调用C++生成的DLL的DEMO.困扰我好久的就是C#中string类型在C++里面怎么表达,现在把C++生成DLL供C#调用的流程写出来. 源码: ...