Sequelize is a promise-based ORM for Node.js v4 and up. It supports the dialects PostgreSQL, MySQL, SQLite and MSSQL and features solid transaction support, relations, read replication and more.

Sequelize是一种基于promise的ORM(对象关系映射),支持nodejs版本4及以上。它支持PostgreSQL、MySQL、SQLite和MSSQL语言,并具有可靠的事务支持、关系、读复制等特性。

安装:

// Using NPM
$ npm install --save sequelize
$ npm install --save mysql2

我使用的是mysql,所以安装的是上面两个模块

⚠️要安装mysql2,否则报错:

Error: Please install mysql2 package manually

相应的其他对应安装:

$ npm install --save pg pg-hstore
$ npm install --save mysql2
$ npm install --save sqlite3
$ npm install --save tedious // MSSQL

Setting up a connection

Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database if you're connecting to the DB from a single process. If you're connecting to the DB from multiple processes, you'll have to create one instance per process, but each instance should have a maximum connection pool size of "max connection pool size divided by number of instances". So, if you wanted a max connection pool size of 90 and you had 3 worker processes, each process's instance should have a max connection pool size of 30.

Sequelize将在初始化时设置一个连接池,因此理想情况下,如果从单个进程连接到数据库,那么每个数据库应该只创建一个实例。如果要从多个进程连接到数据库,必须为每个进程创建一个实例,但是每个实例的最大连接池大小应该是“最大连接池大小除以实例数量”。因此,如果您希望最大连接池大小为90,并且您有3个工作进程,那么每个进程的实例的最大连接池大小应该为30。

const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mysql'|'sqlite'|'postgres'|'mssql',
operatorsAliases: false, pool: {
max: ,
min: ,
acquire: ,
idle:
}, // SQLite only
storage: 'path/to/database.sqlite'
}); // Or you can simply use a connection uri
const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');

Test the connection

You can use the .authenticate() function like this to test the connection.

sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});

Promises (如果不了解什么是promise,可以看本博客js同步-异步-回调)

Sequelize uses Bluebird promises to control async control-flow.

Note: Sequelize use independent copy of Bluebird instance. You can access it using Sequelize.Promise if you want to set any Bluebird specific options

If you are unfamiliar with how promises work, don't worry, you can read up on them here.

Basically, a promise represents a value which will be present at some point - "I promise you I will give you a result or an error at some point". This means that

// DON'T DO THIS
user = User.findOne()
console.log(user.get('firstName'));

will never work! This is because user is a promise object, not a data row from the DB. The right way to do it is:

User.findOne().then(user => {
console.log(user.get('firstName'));
});

When your environment or transpiler supports async/await this will work but only in the body of an asyncfunction:

user = await User.findOne()
console.log(user.get('firstName'));

Once you've got the hang of what promises are and how they work, use the bluebird API reference as your go-to tool. In particular, you'll probably be using .all a lot.


 

Sequelize-nodejs-1-getting started的更多相关文章

  1. 【前端】nodejs的ORM框架sequelize的工厂化

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/sequelize_factory.html 一.什么是sequelize nodejs的后台在操作数据库的时候,需 ...

  2. nodejs项目mysql使用sequelize支持存储emoji

    nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...

  3. koa2+log4js+sequelize搭建的nodejs服务

    主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...

  4. 从 moment -> nodejs -> sequelize -> postgres,你都得设置好时区

    背景 最近在做报表统计,因为 sequelize 的时区配置没加导致了统计数字对不上的问题. 问:大家都知道时区,但是你清楚 UTC 和 GMT 的区别吗? 答:UTC 是我们现在用的时间标准,GMT ...

  5. nodejs sequelize 对应数据库操作符的定义

    const Op = Sequelize.Op [Op.and]: {a: } // 且 (a = 5) [Op.or]: [{a: }, {a: }] // (a = 5 或 a = 6) [Op. ...

  6. postgresql on centos (sequelize+pg+nodejs):Failed to find PostgresSQL server.Pleast double check your settings

    公司的一个项目,使用的nodejs做服务端,数据库是postgresql,在本地时一切ok,放在centos时,postgresql配置ok,可以远程访问,但是nodejs在centos启动时,就会报 ...

  7. 项目总结,彻底掌握NodeJS中如何使用Sequelize

    前言 sequelize是什么? sequelize是基于NodeJs的ORM框架,它适用于不同的数据库,如:Postgres.MySQL.SQLite.MariaDB,我们可以通过sequelize ...

  8. 基于Nodejs的sequelize操纵数据库

    ## 使用基于ORM架构的sequelize操纵数据库 ### 1.技术背景 ```Sequelize是一个基于promise的关系型数据库ORM框架,*********************技术文 ...

  9. Nodejs ORM框架Sequelize快速入门

    Nodejs ORM框架Sequelize快速入门 什么是ORM? 简单的讲就是对SQL查询语句的封装,让我们可以用OOP的方式操作数据库,优雅的生成安全.可维护的SQL代码.直观上,是一种Model ...

  10. nodejs+sequelize操作mysql数据库

    前言: 本人对mysql不是很熟悉,只会命令行的简单增删改查.有些观点可能不到位请谅解. sequelize是针对node.js和io.js开发的基于ORM的框架,它支持的数据库包括:PostgreS ...

随机推荐

  1. pipenv虚拟环境和依赖管理工具

    一.pipenv用来干嘛 每门编程语言发展到现在,都需要一个工具,能够管理代码版本和控制生产环境和测试环境依赖一致的,这样减少不可代码上线之后不可控的问题出现.Php有Composer.Nodejs有 ...

  2. CSS 画一个心

    效果图: 实现原理: 可以把这个心分为两部分,两个长方形,分别设置 border-radius,transform: rotate() . 设置属性之后 再次添加一个,设置相反的 rotate 设置其 ...

  3. 带你从零学ReactNative开发跨平台App开发(四)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  4. Week8——hashcode()和equals()方法

    equals()方法  Object类中的equals方法和“==”是一样的,没有区别,即俩个对象的比较是比较他们的栈内存中存储的内存地址.而String类,Integer类等等一些类,是重写了equ ...

  5. ubuntu密码正确,却不能登录图形界面

    传统的方法是修改.Xauthority文件权限,不过我试了没有用. 后来发现我的问题是因为安装了NVIDIA cuda驱动而导致的. 所以先卸载nvidia驱动,再更新,就可以正常进入了. 命令: s ...

  6. 团队项目个人进展——Day05

    一.昨天工作总结 冲刺第五天,学习了官方文档说明,学习并但是并未实现地图的放大与缩小 二.遇到的问题 对一些框架和API不太熟悉 未实现地图的放大与缩小 三.今日工作规划 深入学习有关视频与文档说明

  7. [CENTOS7] 修改机器名:hostnamectl set-hostname host.domain

    # hostnamectl set-hostname host.domain

  8. 【 PostgreSQL】后台周期执行函数实例(shell+crontab)

    工作中常见函数后台周期执行的情况,Oracle有job实现,gp数据库可以通过shell+crontab实现.流程如下: gpadmin用户下创建函数sh脚本. 将sh挂在crontab任务上 ### ...

  9. Javascript---add to favorite | Set Homepage

    <!-- 设为首页 --> <li><a href="javascript:setHome(this,window.location)"> &l ...

  10. keyWindow与delegate中Window的区别

    keyWindow与delegate中Window的区别 源码: // // ViewController.m // UIWindowRelated // // Created by YouXianM ...