Sequelize-nodejs-1-getting started
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的更多相关文章
- 【前端】nodejs的ORM框架sequelize的工厂化
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/sequelize_factory.html 一.什么是sequelize nodejs的后台在操作数据库的时候,需 ...
- nodejs项目mysql使用sequelize支持存储emoji
nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...
- koa2+log4js+sequelize搭建的nodejs服务
主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...
- 从 moment -> nodejs -> sequelize -> postgres,你都得设置好时区
背景 最近在做报表统计,因为 sequelize 的时区配置没加导致了统计数字对不上的问题. 问:大家都知道时区,但是你清楚 UTC 和 GMT 的区别吗? 答:UTC 是我们现在用的时间标准,GMT ...
- nodejs sequelize 对应数据库操作符的定义
const Op = Sequelize.Op [Op.and]: {a: } // 且 (a = 5) [Op.or]: [{a: }, {a: }] // (a = 5 或 a = 6) [Op. ...
- postgresql on centos (sequelize+pg+nodejs):Failed to find PostgresSQL server.Pleast double check your settings
公司的一个项目,使用的nodejs做服务端,数据库是postgresql,在本地时一切ok,放在centos时,postgresql配置ok,可以远程访问,但是nodejs在centos启动时,就会报 ...
- 项目总结,彻底掌握NodeJS中如何使用Sequelize
前言 sequelize是什么? sequelize是基于NodeJs的ORM框架,它适用于不同的数据库,如:Postgres.MySQL.SQLite.MariaDB,我们可以通过sequelize ...
- 基于Nodejs的sequelize操纵数据库
## 使用基于ORM架构的sequelize操纵数据库 ### 1.技术背景 ```Sequelize是一个基于promise的关系型数据库ORM框架,*********************技术文 ...
- Nodejs ORM框架Sequelize快速入门
Nodejs ORM框架Sequelize快速入门 什么是ORM? 简单的讲就是对SQL查询语句的封装,让我们可以用OOP的方式操作数据库,优雅的生成安全.可维护的SQL代码.直观上,是一种Model ...
- nodejs+sequelize操作mysql数据库
前言: 本人对mysql不是很熟悉,只会命令行的简单增删改查.有些观点可能不到位请谅解. sequelize是针对node.js和io.js开发的基于ORM的框架,它支持的数据库包括:PostgreS ...
随机推荐
- js程序中美元符号$是什么(转)
$符号在php中是表示变量的特征字符, 在js中它也有很多作用, 一般我们用来命名一个函数名称,获取id的1.首先可以用来表示变量, 比如变量 var s='asdsd'或var $s='asdasd ...
- CSS预编译器:Sass(进阶),更快的前端开发
1.@if @if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块 在 Sass 中除了 @if 之,还 ...
- 爬虫必备—scrapy-redis(分布式爬虫)
转载自:http://www.cnblogs.com/wupeiqi/articles/6912807.html scrapy-redis是一个基于redis的scrapy组件,通过它可以快速实现简单 ...
- CentOS7系列--1.3CentOS7用户管理
CentOS7用户管理 1. 添加用户 [root@centos7 ~]# useradd jack [root@centos7 ~]# passwd jack Changing password f ...
- C#+SharpMap的相关代码
//放大的代码: private void MapZoomIn(NameValueCollection queryString) { SharpMap.Map map = Session[" ...
- textarea显示默认值
点击不显示默认值,鼠标离开如果没有内容就显示默认值,如果有内容就显示内容. <textarea class="area" onfocus="if(value=='请 ...
- 通过html导出PDF如何分页
每页一个DIV,加上样式page-break-inside:avoid; 即可分页了 .pdfpage{page-break-inside:avoid;} <div class="pd ...
- 团队项目个人进展——Day04
一.昨天工作总结 冲刺第四天,昨天继续忙着整理数据结构与算法的知识,在项目上看了看有关视频,学习了有关视图的地方和文档说明 二.遇到的问题 无 三.今日工作规划 学习并实现地图的放大与缩小
- Array类型
Array类型 Array也是ECMAScript中常用类型之一,其特点是数组中的每一项都可以保存任何类型的数据,数组的大小可以动态调整. 创建数组 方式1:使用Array构造函数 var books ...
- git工具的使用
Git工具的出现降低了软件版本维护的成本,极大的提高了工作效率,在这里列出了一些使用方法,方便以后查看. 1.Git的初始化->创建一个Git仓库:git init 2.创建信息:git con ...