bookshelf

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发挥其最大效用了。

原文地址:https://www.cnblogs.com/wang-jing/p/5721578.html