有熟练nodejs sequelizeejs的吗

node.js开发环境中基于sequelize对mysql基本操作的简单实例 - CNode技术社区
这家伙很懒,什么个性签名都没有留下。
注:以下代码经过测试,但不能保证在其他node.js环境中也能正常运行。
sequelize documents :
npm install sequelize
// 链接
var Sequelize = require('sequelize');
var sequelize = new Sequelize('nodejs', 'root', '', {host : '127.0.0.1', port : '3306', dialect : 'mysql'});
// definition
var Task = sequelize.define('Task', {
// auto increment, primaryKey, unique
id : {type : Sequelize.INTEGER, autoIncrement : true, primaryKey : true, unique : true},
// comment
title : {type : Sequelize.STRING, comment : 'Task title'},
// allow null
description : {type : Sequelize.TEXT, allowNull : true},
// default value
deadline : {type : Sequelize.DATE, defaultValue : Sequelize.NOW}
Task.sync().on('success', function(){
console.log('aa..');
}).on('failure', function(){
console.log('bb..');
// sequelize.query('your query', [, callee], [, options], [, replacements])
// Callee is the model definition. This allows you to easily map a query to a predefined model for sequelizejs
// Options is an object with the following keys:
// {
//
logging: console.log, a function (or false) for logging your queries
//
plain: false,
if plain is true, then sequelize will return all of the records within an array, otherwise it will return a single object/first record returned.
//
raw: false,
Set this to true if you don't have a model definition for your query
// }
// Replacements is a simple array that replaces all of the bindings within your query
sequelize.query('select * from user
where title = ? and description = ?', null, {logging : true, plain : true,
raw : true}, ['test_title_1', 'test_description_1']).success(function(res){
console.log(res);
// find
Task.findAll({limit : 10, order : 'id asc'}, {raw : true, logging : true, plain : false}).on('success', function(res){
console.log(res);
}).on('failure', function(err){
console.log(err);
// count
Task.count({where : {title : 'test_title_1'}}, {logging : false}).on('success', function(i){
console.log(i);
}).on('failure', function(err){
console.log(err);
// max or min
Task.max('id').on('success', function(max){
console.log(max);
}).on('failure', function(err){
console.log(err);
// 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);
// update
Task.update({description : 'test_description_2000'}, {id : '2'}).on('success', function(msg){
console.log(msg);
}).on('failure', function(err){
console.log(err);
// delete
Task.destroy({id : '4'}).on('success', function(msg){
console.log(msg);
}).on('failure', function(err){
console.log(err);
主外建是怎么用的,不会用,请指导
Task is ready.
Project is another.
Task.belongsTo(Project);
Project.hasMany(Task);
如果用promise方式调用呢
楼主的是3年前的写法; 如今的sequelize已经改为promise写法了,返回的就是prmoise实例;看一下官方文档一目了然
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
服务器赞助商为
,存储赞助商为
,由提供应用性能服务。
新手搭建 Node.js 服务器,推荐使用无需备案的6被浏览4791分享邀请回答3添加评论分享收藏感谢收起a.findAll({
31 条评论分享收藏感谢收起node.js和MySQL使用Sequelize - CSDN博客
node.js和MySQL使用Sequelize
node.js和MySQL使用Sequelize
Sequelize 其实就是node.j和MySQL之间的一个桥梁,是用javascript写的object关系映射库。
1. 首先安装依赖:
npm install mysql
npm install sequelize
— Setting up the connection with mysql
2.连接MySQL
var Sequelize = require("sequelize");
var sequelize = new Sequelize('your-database-name', 'db-username', 'db-password', {
host: "localhost",
port: 3306,
dialect: 'mysql'
检查连接的状态
sequelize.authenticate().complete(function (err) {
if (err) {
console.log('There is connection in ERROR');
console.log('Connection has been established successfully');
3.用node.js 创建一个表
var Item = sequelize.define('Item', {
id: Sequelize.STRING,
name:Sequelize.STRING,
description: Sequelize.STRING,
qty: Sequelize.INTEGER
sequelize.sync({force:true}).complete(function (err) {
console.log('An error occur while creating table');
console.log('Item table created successfully');
对数据表的操作
1.插入操作
有两种方法,第一种:
var item1 = Item.build({
name:'Laptop',
description: 'Acer 2340TL',
item1.save().complete(function (err) {
if (err) {
console.log('Error in Inserting Record');
console.log('Data successfully inserted');
sequelize.sync().success(function () {
Item.create({
name:'Cell Phone',
description: 'Sony',
}).success(function (data) {
console.log(data.values)
2.从表中读取数据
Item.find({}).complete(function (err,data) {
console.log(data);
Item.find({where:{name:'Laptop'}}).complete(function (err, data) {
console.log(data);
3.更新数据表
Item.find({where:{name:'Laptop'}}).complete(function (err, data) {
console.log(err);
data.updateAttributes({
name:'Computer'
}).success(function (data1) {
console.log(data1);
4.从表中删除数据
Item.find({where: {name: 'Computer'}}).complete(function (err, data) {
if (err) {
console.log(err);
data.destroy({}).success(function (err, data) {
console.log(err);
console.log(data);
console.log(data);
你可以在 GITHUB上点击查看,也可以查看sequelise
本文已收录于以下专栏:
相关文章推荐
关于sequelize的准备工作这里不再赘述.
一、引入sequelize模块
var Sequelize = require('sequelize');
二、连接数据库
1.定义数据模型module
2.把一个model转化为table,使用define()方法,例如:
var UserRole = sequelize.define(&UserRole&, {
sequelizequery与ProjectfindAll对比
sequelizequery
参数绑定介绍
在nodejs中使用sequlize库来查询m...
如果你照着书上的代码照抄,是无论如何也实现不了这个例子的。说实话,这是我第一次遇到这种情况。这说明学习node.js这种不是很成熟的技术还是有风险的。不过还好,通过查询node.js的中文社区,exp...
1.1 实验内容
Sequelize 是一个 Node.js 平台基于 Promise 的ORM。用于操作管理 MySQL、Postgres、SQLite 等关系型数据库。本课程主要学习使用 S...
1.起因我们有个需要事物的业务场景,上线之初一直运行正常,可是在晚上高峰的时候一直会有逻辑错误的问题,刚开始绝的是逻辑有问题。在阿里RDS后台发现出现大量的锁...
nodejs使用express框架 ,和sequelize实现分页
我们直接来分析代码
//我们首先获取前端传来的page 和pagesize 的值
var page ,pagesize = ''...
作者:短工邦技术部 - 陈文哲经过《nodejs MODEL层 封装(一)》的封装,MODEL层变得简洁了,但是所有MODEL层的方法都一样,所以显而易见的,下一步封装就是把所有相同的代码封装到Bas...
Nodejs ORM框架Sequelize快速入门
想搭建一个Vue+Node+MongoDB的项目,能跑通整个开发到部署的流程,折腾一个多星期终于可以了~看过不少教学文章,发现教你怎样搭建一个Vue开发环境的很多,搭建Express的很多,但还真没发...
他的最新文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)sequelize的使用 - CSDN博客
sequelize的使用
nodejs使用express框架 ,用sequelize实现分页
我们直接来分析代码
var utils = require('../lib/utils');
app.post('/articleList',function(req,res){
var page, pageSize = '';
if(req.param('page')&&utils.trim(req.param('page'))!=""){
page=parseInt(utils.trim(req.param("page")));
if(req.param('rows')&&utils.trim(req.param('rows'))!=""){
pageSize=parseInt(utils.trim(req.param("rows")));
findAndCountAll({
offset:(page - 1) * pageSize,
limit:pageSize
}).then(function(article){
if(article.length!=0){
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
console.log(article);
res.send(res,{rows:article.rows,total:article.count});
utils.send(res,{
err:'没有找到文章,请先创建'
本文已收录于以下专栏:
相关文章推荐
Sequelize Node.js的ORM 框架
Sequelize.js 提供对 MySQL,MariaDB,SQLite 和 PostgreSQL 数据库的简单访问,通过映射数据库条目到对象,或者...
sequelizequery与ProjectfindAll对比
sequelizequery
参数绑定介绍
在nodejs中使用sequlize库来查询m...
Sequelize 常用操作demo链接
var Sequelize = require('sequelize');
var sequelize = new Sequelize('nodejs', '...
npm install sequelize   //ORM 主框架
npm install -g sequelize-auto // 已有数据库表生成modle工具
nodejs如果采用mysql数据库作为对象持久存储,可以使用sequelize这个库来做orm,同时,这个库可以将模型同步到数据库,无需我们手动创建表,这点可以和hibernate媲美。
1.起因我们有个需要事物的业务场景,上线之初一直运行正常,可是在晚上高峰的时候一直会有逻辑错误的问题,刚开始绝的是逻辑有问题。在阿里RDS后台发现出现大量的锁...
node.js和MySQL使用SequelizeSequelize 其实就是node.j和MySQL之间的一个桥梁,是用javascript写的object关系映射库。
1. 首先安装依赖:npm ...
Async/Await应该是目前最简单的异步方案了,这里我们将使用它进行数据库操作。先来说一下它的基本规则:
async 表示这是一个async函数,await只能用在这个函数里面。
koa2-starter.A koa2 starter by using mysql, gulp includes async/await, pm2, express-style middleware...
如果你觉得Sequelize的文档有点多、杂,不方便看,可以看看这篇。
在使用NodeJS来关系型操作数据库时,为了方便,通常都会选择一个合适的ORM(Object
Relationship M...
他的最新文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)}

我要回帖

更多关于 sequelizejs mongodb 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信