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 ...
随机推荐
- Zookeeper学习文章目录1
目录:参考文章如下 1.ZooKeeper学习第一期---Zookeeper简单介绍 2. ZooKeeper学习第二期--ZooKeeper安装配置 3. ZooKeeper学习第三期---Zook ...
- Django之WSGI浅谈
一.什么是Web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统. 浏览器与服务器之间发起HTTP请求: 1.浏览器发送一 ...
- DOMINO的JDBC和ODBC连接方法
利用ODBC实现Domino和关系数据库的互操作 Lotus Domino是当今办公自动化系统的主流开发平台之一,Domino自带一个非关系型数据库–文档型数据库,而目前大部分企业的信息都储存在 ...
- web项目启动时,自动执行代码的几种方式
在项目开发过程中,往往需要一些功能随着项目启动而优先启动,下面我总结几种方式(非spring boot) spring boot的参考 spring boot 学习之路9 (项目启动后就执行特定方法) ...
- top,job,user,file,alias
1.系统进程 2.系统资源管理 3.作业管理 4.用户管理 5.文件权限 6.别名定义 一.系统进程 1.进程的定义 进程是操作系统的概念,每当我们执行一个程序时,对于操作系统来讲就创建了 ...
- Android组件系列----Activity的生命周期
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- Hibernate的increment主键生成机制带来的问题
最近给学校做的系统,总出现主键插入冲突的问题.主键是通过hibernate自动生成的,设置increment属性,总出现Duplicate entry的错误.搜到解决方案如下: 在网站运行在apach ...
- 在 Azure VM 上安装 LAMP Web 服务器
本文逐步讲解如何在 Azure 中的 Ubuntu VM 上部署 Apache Web 服务器.MySQL 和 PHP(LAMP 堆栈). 如果想要部署 NGINX Web 服务器,请参阅 LEMP ...
- Windows server 安装
运行管理员CMD --先切换到安装环境目录cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 --安装服务 InstallUtil.exe D:\绝对路劲 ...
- Java 两个日期间的天数计算
在Java中计算两个日期间的天数,大致有2种方法:一是使用原生JDK进行计算,在JDK8中提供了更为直接和完善的方法:二是使用第三方库. 1.使用原生的JDK private static long ...