官术网_书友最值得收藏!

Connecting to a database with Sequelize

The first step is to initialize the connection from Sequelize to our MySQL server. To do this, we will create a new folder and file, as follows:

mkdir src/server/database
touch src/server/database/index.js

Inside of the index.js database, we will establish a connection to our database with Sequelize. Internally, Sequelize relies on the mysql2 package, but we do not use it on our own, which is very convenient:

import Sequelize from 'sequelize';

const sequelize = new Sequelize('graphbook_dev', 'devuser', 'PASSWORD', {
host: 'localhost',
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000,
},
});

export default sequelize;

As you can see, we require Sequelize from the node_modules, and then create an instance of it. The following properties are important for Sequelize:

  • We pass the database name as the first parameter, which we just created.
  • The second and third parameters are the credentials of our devuser. Replace them with the username and password that you entered for your database. The devuser has all user rights, and can read and write all of the databases in our MySQL server. This makes development a lot easier.
  • The fourth parameter is a general options object that can hold many more properties. The preceding object is an example configuration.
  • The host of our MySQL database is our local machine alias, localhost. If this is not the case, you can also specify the IP or URL of the MySQL server.
  • The dialect is, of course, mysql
  • The operatorsAliases property specifies which strings can be used as aliases by Sequelize, or whether they can be used at all. An example would look as follows:
[Op.gt]: 6 // > 6
$gt: 6 // same as using Op.gt (> 6)

This example is taken from the Sequelize documentation. Generally, it is discouraged to use operators aliases at all. This is why you should disable it, and should always sanitize user input, to avoid SQL injections.

If you want to read more about this topic and what possibilities Sequelize gives you for operator aliases, you can find more information at  http://docs.sequelizejs.com/manual/tutorial/querying.html#operators-aliases.
  • With the pool option, you tell Sequelize the configuration for every database connection. The preceding configuration allows for a minimum of zero connections, which means that Sequelize should not maintain one connection, but should create a new one whenever it is needed. The maximum number of connections is five. This option also relates to the number of replica sets that your database system has.
  • The idle field of the pool option specifies how long a connection can be unused before it gets closed and removed from the pool of active connections.
  • When trying to establish a new connection to our MySQL server, the timeout before the connection is aborted is defined by the acquire option. In cases in which a connection cannot be created, this option helps to stop your server from freezing.

Executing the preceding code will instantiate Sequelize, and will successfully create a connection to our MySQL server. Going further, we need to handle multiple databases for every environment in which our application can run, from development to production. You will see that in the next section.

主站蜘蛛池模板: 沁水县| 乡城县| 东城区| 崇信县| 顺昌县| 闸北区| 抚顺市| 邳州市| 广平县| 河北区| 新疆| 陆良县| 紫阳县| 大方县| 赣州市| 衡阳市| 平顶山市| 闵行区| 外汇| 建湖县| 无为县| 隆回县| 绥德县| 邵东县| 黄冈市| 南城县| 和田市| 盈江县| 北海市| 昌黎县| 伽师县| 屏东市| 樟树市| 富宁县| 大余县| 平江县| 白玉县| 福泉市| 石门县| 安阳市| 廉江市|