欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Sequelize 常用操作详解及实例代码

程序员文章站 2024-03-09 17:31:05
sequelize 常用操作demo 链接 var sequelize = require('sequelize'); var sequelize = n...

sequelize 常用操作demo

链接

var sequelize = require('sequelize');
var sequelize = new sequelize('nodejs', 'root', '', {host : '127.0.0.1', port : '3306', dialect : 'mysql'});

查询

task.findall({limit : 10, age:{gt:3},order : 'id asc'}, {raw : true, logging : true, plain : false}).on('success', function(res){
  console.log(res);
}).on('failure', function(err){
  console.log(err);
})

统计

task.count({where : {title : 'test_title_1'}}, {logging : false}).on('success', function(i){
  console.log(i);
}).on('failure', function(err){
  console.log(err);
});

最大或最小

task.max('id').on('success', function(max){
  console.log(max);
}).on('failure', function(err){
  console.log(err);
});

插入

task.build({title : 'test_title_3', 'description' : 'test_description_3'}).save().on('success', function(msg){
  console.log(msg);
}).on('failure', function(err){
  console.log(err);
});

task.create({title : 'test_title_4', 'description' : 'test_description_4'}).on('success', function(msg){
  console.log(msg);
}).on('failure', function(err){
  console.log(err);
});

修改

task.update({description : 'test_description_2000'}, {id : '2'}).on('success', function(msg){
  console.log(msg);
}).on('failure', function(err){
  console.log(err);
});

删除

task.destroy({id : '4'}).on('success', function(msg){
  console.log(msg);
}).on('failure', function(err){
  console.log(err);
});

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!