Sequelize 常用操作demo
鏈接
1
2
|
var Sequelize = require( 'sequelize' ); var sequelize = new Sequelize( 'nodejs' , 'root' , '' , {host : '127.0.0.1' , port : '3306' , dialect : 'mysql' }); |
查詢
1
2
3
4
5
|
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); }) |
統計
1
2
3
4
5
|
Task.count({where : {title : 'test_title_1' }}, {logging : false }).on( 'success' , function(i){ console.log(i); }).on( 'failure' , function(err){ console.log(err); }); |
最大或最小
1
2
3
4
5
|
Task.max( 'id' ).on( 'success' , function(max){ console.log(max); }).on( 'failure' , function(err){ console.log(err); }); |
插入
1
2
3
4
5
6
7
8
9
10
11
|
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); }); |
修改
1
2
3
4
5
|
Task.update({description : 'test_description_2000' }, {id : '2' }).on( 'success' , function(msg){ console.log(msg); }).on( 'failure' , function(err){ console.log(err); }); |
刪除
1
2
3
4
5
|
Task.destroy({id : '4' }).on( 'success' , function(msg){ console.log(msg); }).on( 'failure' , function(err){ console.log(err); }); |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/bugall/article/details/44019601