mysql database node connected to three methods ------ Notes

A, mysql database

Documentation: https://github.com/mysqljs/mysql

There are three ways to create a connection mysql

1.createConnection

The need to create a connection and disconnection to manage the use of

2.createPool

Create a resource pool, do not need to create a connection, disconnection manage each use one call release to be released connect to a resource pool use, as to whether the connection is lost to the resource pool to manage. Every time build very resource-consuming connections, affects performance, create a rational management of connections, will help improve performance.

3.createPoolCluster

Create a connection pool cluster, allowing connections to multiple host

Two, sequelize library

Chinese document: https://github.com/demopark/sequelize-docs-Zh-CN

This library dependencies mysql2

Compared with the mysql, sql statement does not need to write, change check deletions are encapsulated into corresponding method.
mysql database entry is relatively simple, conducive to learning sql statement
sequelize encapsulates some simple sql statement, master packaging methods and the corresponding parameters can be, but the cost is slightly higher learning, you need to create mode, mode requires the database tables correspondence . In the actual project development process, the use of higher sequelize development efficiency, the code can be more brief. There query methods, support the use of the sql statement.

sequelize provides a method sequelize.sync ({force: true}) ; mandatory table database table defined synchronization pattern and, if the database table defined in the present mode of the same name, this table will be deleted, redefined. If the mode is undefined table exists in the database, it will not be operating.
You may be provided a single mode forced synchronization
such as:

// Note: If the table exists, using the `force: true` deletes the table 
User.sync ({Force: to true }). The then (() => {
   // now database table corresponding to the model of` users` defined 
  return User.create ({ 
    firstName: 'John' , 
    lastName: 'Hancock' 
  }); 
});

 


Genlock also at risk points:
1. For the project initialization, you need to create data tables when using
2. If you already have part of the data in the database, when the service is restarted, the database schema definition table is deleted, the data will be lost, so not suitable for use in the non-initialization, in use need to determine whether the initialization of



Author: lion Ai Zhama end of
the link: https: //www.jianshu.com/p/e3bc48b4a050
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/lguow/p/11934687.html