Home > php教程 > PHP开发 > Detailed explanation of common operations of Sequelize and example codes

Detailed explanation of common operations of Sequelize and example codes

高洛峰
Release: 2016-12-28 11:14:09
Original
1540 people have browsed it

Sequelize common operation demo

Link

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

Query

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);
})
Copy after login

Statistics

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

Maximum or minimum

Task.max('id').on('success', function(max){
  console.log(max);
}).on('failure', function(err){
  console.log(err);
});
Copy after login

Insert

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);
});
Copy after login

Modify

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

Delete

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

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed explanations of common Sequelize operations and sample code related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template