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. Docker 更改镜像存储位置

    在使用 Docker 的过程中,如果我们一切都以默认的设置进行操作的话,在使用一段时间之后你应该会发现系统的根目录空间越来越少,直到有一天发现空间都被占满了,最后发现原来是被 Docker 占用了.所 ...

  2. 浅谈脚本化css(三)之方块运动函数

    我们可以写一个让小方块运动的函数: div#demo { width: 100px; ; position: absolute; ; ; } var div = document.getElement ...

  3. 实用的Javascript获取网页屏幕可见区域高度

    本文转载原地址:这里 document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 docu ...

  4. ubuntu将python3设为默认后再安装支持python3.x的包

    简介: ubuntu默认python2.7版本,如果想要装python3.x版本,请记住python2.7版本一定不能卸载!!!但是即使我 python3.x版本安装成功,当运行python脚本时,系 ...

  5. cocos2d-x学习笔记--第一天记录

    1.环境安装 http://www.cocos2d-x.org/ ---下载2.2.3--解压 https://www.python.org/ ---2.7.6 系统环境变量 设置安装目录 2创建一个 ...

  6. LeetCode 545----Boundary of Binary Tree

    Given a binary tree, return the values of its boundary in anti-clockwise direction starting from roo ...

  7. Postman Postman接口测试工具使用简介

    Postman接口测试工具使用简介 by:授客 QQ:1033553122 本文主要是对Postman这个接口测试工具的使用做个简单的介绍,仅供参考. 插件安装 1)下载并安装chrome浏览器 2) ...

  8. python汉字转拼音

    上代码: #!/usr/bin/env python # -*- coding:utf-8 -*- """ Author:cleverdeng E-mail:clverd ...

  9. 实验二:klee处理未建模函数和处理error的方式

    首先,能够分析klee源码固然重要.但是目前尚未到那个地步.我按照我的过程,记录和分析我所做的实验. 结论性内容是: 1.klee处理printf传入符号值的情形时,报为error,不会将符号值具体化 ...

  10. AWS CSAA -- 02 AWS - 10000 Feet Overview

    004 The History Of AWS So Far 005 AWS - 10000 Foot Overview 006 AWS - 10000 Foot Overview 007 AWS - ...