MYSQL-NODE-CLIENT
A Node.js mysql client implementation, using the following mysql driver: https://www.npmjs.com/package/mysql
Highlights
Install & Kickstart
Installation is done using the npm install
command
$ npm install mysql-node-client
In order to create an instance of the mysqlClient, you must do the following:
const createMysqlClient = const CONNECTION_DATA = host: 'localhost' port: '3307' user: 'myUser' password: 'asd123' database: 'test_db' const test = async { try //create an instance of the client const mysqlClient = //call a method exposed by the client const data = await mysqlClient //use the response data console //destroy the connection pool when it's not needed anymore mysqlClient catch err console }
Connection Management
All the connections are pooled in a connection pool. The default connectionLimit is 100 and the default acquireTimeout is 10 seconds. If all the connections in the pool are used, there's a default queue of 100 slots which can hold requests. All the default options can be overriden by passing a second object to the createMysqlClient method.
Methods exposed by the client
DDLs
createDatabase
- The connection must not be using a database name
const data = await mysqlClient
listDatabases
- The connection must not be using a database name
const data = await mysqlClient
createTable
const TABLE_SCHEMA = name: 'id' type: 'INT AUTO_INCREMENT PRIMARY KEY' name: 'name' type: 'VARCHAR(255) NOT NULL' const data = await mysqlClient
listTables
const data = await mysqlClient
describeTable
const data = await mysqlClient
DMLs
store
const data = await mysqlClient
update
const data = await mysqlClient
destroy
const data = await mysqlClient
DQLs
list
- Supports columns projection
const data = await mysqlClient
fetch
- Supports columns projection and filtering
const data = await mysqlClient
Other
getConnection
- Used for retrieving a connection from the connection pool, or creating a new one. That connection is locked and can be used until it is released.
//create an instance of the clientconst mysqlClient = //retrieve or create a connection to the dbconst connection = await mysqlClient//use any method from the connection object, as provided by the mysql driverconnection
destroyConnectionPool
- Destroy the connection pool when it's not needed anymorequery
- A wrapper over the base query method. Extras: connection management (retrieval and release from/to the connection pool), logging system (connection id and sql query executed).
const data = await mysqlClient
Notes
For more information, check the mysql driver documentation (https://www.npmjs.com/package/mysql). This implementation should be treated as a wrapper of the base driver.