本模块是将node-mysql模块进行简单封装, 旨在更加简单直观地进行同步操作异步调用或异步操作异步调用, 本模块的相关参数设定完全遵循node-mysql模块的相关设定.
This module is a simple encapsulation of node-mysql module, aiming to make asynchronous invocation of synchronous operation or asynchronous operation and asynchronous invocation more simple and intuitive. The parameter setting of this module fully follows the relevant setting of node-mysql module.
The English API is at the back of the page!
下载:
npm install async4mysql -S
引入:
const async4mysql = ; //引入async4mysql模块 const db = option; //创建模块实例对象
new async4mysql对象
会根据参数类型
来创建一个 单一连接池 或者 连接池集群
使用:
** 单一连接池 ->**
当 option 的 option参数 是一个json 那么 option 将会创建一个 参数格式: host:'localhost' // 数据库服务器名称 user:'root' // 数据库服务器用户名 port:3306 // 数据库服务器接口 password:'123456' // 数据库服务器密码 database:'damo' // 要连接的数据库名称
>>>>> 当db对象是一个单一连接池时, db对象 可以使用以下6个方法:
.select()
,.delete()
,.update()
,.insert()
,.entire()
,.query()
1: .select()方法
.select() 同步执行指定的mysql 查询语句 ( SELECT语句 )
参数 :
.select( sql, arr )
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option = //参数option是一个json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; /* 由于参数option是一个json, async4mysql 实例将会创建一个 单一连接池 */ let sql_1 = `SELECT * FROM table`; db; let sql_2 = `SELECT * FROM ?? WHERE id = ?`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
2: .delete()方法
.delete() 同步执行指定的mysql 删除语句 ( DELETE语句 )
参数 :
.delete( sql, arr )
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option = //参数option是一个json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; /* 由于参数option是一个json, async4mysql 实例将会创建一个 单一连接池 */ let sql_1 = `DELETE * FROM table WHERE id = '123456'`; db; let sql_2 = `DELETE * FROM ?? WHERE id = ?`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
3: .update()方法
.update() 同步执行指定的mysql 更新语句 ( UPDATE语句 )
参数 :
.update( sql, arr )
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option = //参数option是一个json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; /* 由于参数option是一个json, async4mysql 实例将会创建一个 单一连接池 */ let sql_1 = `UPDATE table SET value1='value1',value2='value2' WHERE id = '123456'`; db; let sql_2 = `UPDATE ?? SET ? WHERE id = ?`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
4: .insert()方法
.insert() 同步执行指定的mysql 插入语句 ( INSERT语句 )
参数 :
.insert( sql, arr )
如果 sql 的类型是 字符串 :
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
如果 sql 的类型是 json :
sql: 要执行的mysql语句的 插入内容键值对(json) 必须
arr: 需要操作的数据表的 表名字符串 必须
基础案例 :
******在非async函数中使用: ****** const option = //参数option是一个json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; //*由于参数option是一个json, async4mysql实例将会创建一个 单一连接池 let sql_1 = `INSERT INTO table SET id = 'aaa',value1 = 'bbb'`; db; let sql_2 = `INSERT INTO ?? SET ?`; db; let json = id:'eee' value:'fff'; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
5: .entire()方法
.entire() * 可以异步执行 多个mysql操作, 并保证所有 mysql操作 都执行完毕后, 才返回相关数据
参数:
- .entire()方法的参数类型必须是 json, 可以有 大于1的任意 个 参数
sql: 要执行的mysql语句 或者 values: 防注入操作存放数据的数组 可选 或者
基础案例 :
******在非async函数中使用: ****** const option = // 参数option是一个json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; // *由于参数option是一个json, async4mysql实例将会创建一个 单一连接池 let P1 = sql:`SELECT * FROM table WHERE value = 'value1'` // values:[] // *当不需要进行防注入操作时, values属性可以省略; let P2 = sql:`INSERT INTO ?? SET id = ?, value = ?` values:'table' 'id' 'value'; let P3 = sql:id:'_id' value:'_value' // *** 当sql属性的类型为json时, 即代表该操作**一定**为 INSERT操作 values:'table' // *** 当sql属性的类型为json时, values属性不能省略, 且它的类型必须是 字符串 , 它表示INSERT操作的表名称 ; /* P3写法相当于: let P3 = { sql:`INSERT INTO ?? SET ?`, values: ['table', {id:'_id', value:'_value'}] }; */ db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
6: .query()方法
.query() 同步执行指定的 所有类型mysql语句 ( 一般用于执行除了 增、删、改、查 外的mysql语句 )
参数 :
.query( sql, arr )
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option = //参数option是一个json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; //*由于参数option是一个json, async4mysql实例将会创建一个 单一连接池 let sql_1 = `SELECT COUNT(table_name) AS total FROM information_schema.tables WHERE table_schema = '库名' AND table_name='表名称'`; db; let sql_2 = `CREATE TABLE damo ( id varchar(11) DEFAULT NULL, _varchar varchar(100) DEFAULT NULL, _int int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
---连接池集群---
当 option 的 option参数 是一个Array那么 option 则会创建一个 参数格式: connectName:'CONNECT_1' // 连接名称 option: host:'localhost' // 数据库服务器名称 user:'root_1' // 数据库服务器用户名 port:3306 // 数据库服务器接口 password:'pwd_1' // 数据库服务器密码 database:'damo_1' // 要连接的数据库名称 connectName:'CONNECT_2' // 连接名称 option: host:'localhost' // 数据库服务器名称 user:'root_1' // 数据库服务器用户名 port:3306 // 数据库服务器接口 password:'pwd_2' // 数据库服务器密码 database:'damo_2' // 要连接的数据库名称 ......
此时, new async4mysql(option)实例 会根据 参数数组 中的每一个 值(json) 的 json.option
来创建相应连接名为 json.connectName
的连接
连接池集群 可以在 同一个实例中 对 不同 的 mysql数据库
进行操作!!! (单一连接池只能对 同一个mysql数据库 进行操作)
>>>>> 当db对象是一个连接池集群时, db对象可以使用以下6个方法:
.$select()
,.$delete()
,.$update()
,.$insert()
,.$entire()
,.$query()
1: .$select()方法
.$select() 同步执行指定的mysql 查询语句 ( SELECT语句 )
参数 :
.$select( connectName, sql, arr )
connectName: 连接名 必须
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*由于参数option是一个Array, async4mysql实例将会创建一个 连接池集群 // 根据参数opt, 这个连接池集群里面将包含2个连接, 他们的名称分别是: 'CONNECT_1' 和 'CONNECT_2', 可以连接到不同的数据库进行操作 let sql_1 = `SELECT * FROM table`; db; let sql_2 = `SELECT * FROM ?? WHERE id = ?`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
2: .$delete()方法
.$delete() 同步执行指定的mysql 删除语句 ( DELETE语句 )
参数 :
.$delete( connectName, sql, arr )
connectName: 连接名 必须
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*由于参数option是一个Array, async4mysql实例将会创建一个 连接池集群 // 根据参数opt, 这个连接池集群里面将包含2个连接, 他们的名称分别是: 'CONNECT_1' 和 'CONNECT_2', 可以连接到不同的数据库进行操作 let sql_1 = `DELETE * FROM table WHERE id = '123456'`; db; let sql_2 = `DELETE * FROM ?? WHERE id = ?`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
3: .$update()方法
.$update() 同步执行指定的mysql 更新语句 ( UPDATE语句 )
参数:
.$update( connectName, sql, arr )
connectName: 连接名 必须
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*由于参数option是一个Array, async4mysql实例将会创建一个 连接池集群 // 根据参数opt, 这个连接池集群里面将包含2个连接, 他们的名称分别是: 'CONNECT_1' 和 'CONNECT_2', 可以连接到不同的数据库进行操作 let sql_1 = `UPDATE table SET value1='value1',value2='value2' WHERE id = '123456'`; db; let sql_2 = `UPDATE ?? SET ? WHERE id = ?`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
4: .$insert()方法
.$insert() 同步执行指定的mysql 插入语句 ( INSERT语句 )
参数 :
.$insert( connectName, sql, arr )
如果 sql 的类型是 字符串 :
connectName: 连接名 必须
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
如果 sql 的类型是 json :
connectName: 连接名 必须
sql: 要执行的mysql语句的 插入内容键值对(json) 必须
arr: 需要操作的数据表的 表名字符串 必须
基础案例 :
******在非async函数中使用: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*由于参数option是一个Array, async4mysql实例将会创建一个 连接池集群 // 根据参数opt, 这个连接池集群里面将包含2个连接, 他们的名称分别是: 'CONNECT_1' 和 'CONNECT_2', 可以连接到不同的数据库进行操作 let sql_1 = `INSERT INTO table SET id = 'aaa',value1 = 'bbb'`; db; let sql_2 = `INSERT INTO ?? SET ?`; db; let json = id:'eee' value:'fff'; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
5: .$entire()方法
.$entire() * 可以异步执行 多个mysql操作, 并保证所有 mysql操作 都执行完毕后, 才返回相关数据
参数:
- .$entire()方法的参数类型必须是 json, 可以有 大于1的任意 个 参数
connectName: sql: 要执行的mysql语句 或者 values: 防注入操作存放数据的数组 可选 或者
基础案例 :
******在非async函数中使用: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*由于参数option是一个Array, async4mysql实例将会创建一个 连接池集群 // 根据参数opt, 这个连接池集群里面将包含2个连接, 他们的名称分别是: 'CONNECT_1' 和 'CONNECT_2', 可以连接到不同的数据库进行操作 let P1 = connectName:'CONNECT_1' sql:`SELECT * FROM table WHERE value = 'value1'` // values:[] // *当不需要进行防注入操作时, values属性可以省略; let P2 = connectName:'CONNECT_2' sql:`INSERT INTO ?? SET id = ?, value = ?` values:'table' 'id' 'value'; let P3 = connectName:'CONNECT_1' sql:id:'_id' value:'_value' // *** 当sql属性的类型为json时, 即代表该操作**一定**为 INSERT操作 values:'table' // *** 当sql属性的类型为json时, values属性不能省略, 且它的类型必须是 字符串 , 它表示INSERT操作的表名称 ; /* P3写法相当于: let P3 = { connectName:'CONNECT_1', sql:`INSERT INTO ?? SET ?`, values: ['table', {id:'_id', value:'_value'}] }; */ db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
6: .$query()方法
.$query() 同步执行指定的 所有类型mysql语句 ( 一般用于执行除了 增、删、改、查 外的mysql语句 )
参数:
.$query( connectName, sql, arr )
connectName: 连接名 必须
sql: 要执行的mysql语句 必须
arr: 防注入操作存放数据的数组 可选
基础案例 :
******在非async函数中使用: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*由于参数option是一个Array, async4mysql实例将会创建一个 连接池集群 // 根据参数opt, 这个连接池集群里面将包含2个连接, 他们的名称分别是: 'CONNECT_1' 和 'CONNECT_2', 可以连接到不同的数据库进行操作 let sql_1 = `SELECT COUNT(table_name) AS total FROM information_schema.tables WHERE table_schema = '库名' AND table_name='表名称'`; db; let sql_2 = `CREATE TABLE damo ( id varchar(11) DEFAULT NULL, _varchar varchar(100) DEFAULT NULL, _int int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8`; db;
******在async函数中使用: ****** const express = ;const app = ;app; app;
历史版本信息
版本: 100 发布时间: 2019-1-17 版本说明: *首版发布*------------------------------------- 版本: 110 发布时间: 2019-1-30 版本说明: *删除了方法内部的绝大部分无谓的检测判断 稍微提高代码效率*
--------------------------------------------------------English API--------------------------------------------------------
Download:
npm install async4mysql -S
IMPORT:
const async4mysql = ; //import async4mysql module const db = option; //Create a module instance object
- The
new async4mysql
instance will creates a single connection pool or connection pool cluster based onthe parameter type
USE:
** Single Connection Pool ->**
When the option parameter of option is a json then the option will create a single connection Parameter Format: host:'localhost' // Database server name user:'root' // Database server user name port:3306 // Database server interface password:'123456' // Database server password database:'damo' // The name of the database to connect to
>>>>> When **db instance ** is a ** single connection pool **, **db instance ** can use the following 6 methods:
.select()
,.delete()
,.update()
,.insert()
,.entire()
,.query()
1: .select() method
.select() Executes the specified mysql SELECT statement synchronously
parameters :
.select( sql, arr )
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option = //The option parameter is a json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; /* Because option is a json, the async4mysql instance will create a single connection pool */ let sql_1 = `SELECT * FROM table`; db; let sql_2 = `SELECT * FROM ?? WHERE id = ?`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
2: .delete() method
.delete() Executes the specified mysql DELETE statement synchronously
parameters :
.delete( sql, arr )
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option = //The option parameter is a json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; /* Because option is a json, the async4mysql will create a single connection pool */ let sql_1 = `DELETE * FROM table WHERE id = '123456'`; db; let sql_2 = `DELETE * FROM ?? WHERE id = ?`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
3: .update()方法
.update() Executes the specified mysql UPDATE statement synchronously
parameters :
.update( sql, arr )
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option = //The option parameter is a json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; /* Because option is a json, the async4mysql will create a single connection pool */ let sql_1 = `UPDATE table SET value1='value1',value2='value2' WHERE id = '123456'`; db; let sql_2 = `UPDATE ?? SET ? WHERE id = ?`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
4: .insert() method
.insert() Executes the specified mysql INSERT statement synchronously
parameters :
.insert( sql, arr )
If the type of sql is string :
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
If the type of sql is json :
sql: Insert key-value pairs for the mysql statement to execute (that is, json) necessary
arr: Table name string for the data table to manipulate optional
Basal Example :
******Use in a non-async function: ****** const option = //The option parameter is a json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; //*Because option is a json, the async4mysql will create a single connection pool let sql_1 = `INSERT INTO table SET id = 'aaa',value1 = 'bbb'`; db; let sql_2 = `INSERT INTO ?? SET ?`; db; let json = id:'eee' value:'fff'; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
5: .entire() method
.entire() * The method can perform multiple mysql operations asynchronously and ensure that all mysql operations are completed before returning relevant data
parameters:
- The .entire() method must have a json parameter type and can have any parameter greater than 1
sql: Key-value pairs for mysql statements or INSERT statements to values: An array of anti - injection operations store optional or the table name string for the INSERT
Basal Example :
******Use in a non-async function: ****** const option = // The option parameter is a json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; // *Because option is a json, the async4mysql will create a single connection pool let P1 = sql:`SELECT * FROM table WHERE value = 'value1'` // values:[] // *'values' properties can be omitted when anti-injection operations are not required; let P2 = sql:`INSERT INTO ?? SET id = ?, value = ?` values:'table' 'id' 'value'; let P3 = sql:id:'_id' value:'_value' // *** When the sql attribute is of type json, that means the operation must be INSERT values:'table' // *** When the SQL property is of type json, the values property cannot be omitted, and its type must be a string, which represents the table name of the INSERT operation ; /* P3 equivalently: let P3 = { sql:`INSERT INTO ?? SET ?`, values: ['table', {id:'_id', value:'_value'}] }; */ db;
******Use in an async function: ****** const express = ;const app = ;app; app;
6: .query() method
.query() Synchronized execution of all types of mysql statements specified (generally used to execute mysql statements except add, delete, change, and check)
parameters :
.query( sql, arr )
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option = //The option parameter is a json host:'localhost' user:'root' port:3306 password:'123456' database:'damo'; const db = option; //*Because option is a json, the async4mysql will create a single connection pool let sql_1 = `SELECT COUNT(table_name) AS total FROM information_schema.tables WHERE table_schema = '库名' AND table_name='表名称'`; db; let sql_2 = `CREATE TABLE damo ( id varchar(11) DEFAULT NULL, _varchar varchar(100) DEFAULT NULL, _int int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
---Connection Pool Cluster---
When the option parameter of option is an Arrayoption then creates a cluster of connection *Parameter format* -> connectName:'CONNECT_1' // The connection name option: host:'localhost' // Database name user:'root_1' // Database user name port:3306 // Database interface password:'pwd_1' // Database password database:'damo_1' // The name of the database to connect to connectName:'CONNECT_2' // The connection name option: host:'localhost' // Database name user:'root_1' // Database user name port:3306 // Database interface password:'pwd_2' // Database password database:'damo_2' // The name of the database to connect to ......
At this point, new async4mysql(option) instance creates a connection named json.connectName
based on the json.option
of each value(json) in the parameter array
Connection pool cluster can operate on different mysql databases in the same instance!!!(a single connection pool can only operate on the same mysql database)
>>>>> When a db object is a connection pool cluster, the following 6 methods can be used for the db object:
.$select()
,.$delete()
,.$update()
,.$insert()
,.$entire()
,.$query()
1: .$select() method
.$select() Synchronously execute the specified mysql SELECT statement
parameters :
.$select( connectName, sql, arr )
connectName: The connection name necessary
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*Since the option is an Array, async4mysql instance will create a cluster of connection pools // Depending on the opt parameter, the connection pool cluster will contain two connections, named 'CONNECT_1' and 'CONNECT_2', that can be connected to different databases for operations let sql_1 = `SELECT * FROM table`; db; let sql_2 = `SELECT * FROM ?? WHERE id = ?`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
2: .$delete() method
.$delete() Synchronously execute the specified mysql DELETE statement
parameters :
.$delete( connectName, sql, arr )
connectName: The connection name necessary
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*Since the option is an Array, async4mysql instance will create a cluster of connection pools // Depending on the opt parameter, the connection pool cluster will contain two connections, named 'CONNECT_1' and 'CONNECT_2', that can be connected to different databases for operations let sql_1 = `DELETE * FROM table WHERE id = '123456'`; db; let sql_2 = `DELETE * FROM ?? WHERE id = ?`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
3: .$update() method
.$update() Synchronously execute the specified mysql UPDATE statement
parameters :
.$update( connectName, sql, arr )
connectName: The connection name necessary
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*Since the option is an Array, async4mysql instance will create a cluster of connection pools // Depending on the opt parameter, the connection pool cluster will contain two connections, named 'CONNECT_1' and 'CONNECT_2', that can be connected to different databases for operations let sql_1 = `UPDATE table SET value1='value1',value2='value2' WHERE id = '123456'`; db; let sql_2 = `UPDATE ?? SET ? WHERE id = ?`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
4: .$insert() method
.$insert() Synchronously execute the specified mysql INSERT statement
parameters :
.$insert( connectName, sql, arr )
If the type of sql is string :
connectName: The connection name necessary
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
If the type of sql is json :
connectName: The connection name necessary
sql: Insert contents of the mysql statement to be executed key value pairs(json) necessary
arr: Table name string for the data table to manipulate necessary
Basal Example :
******Use in a non-async function: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*Since the option is an Array, async4mysql instance will create a cluster of connection pools // Depending on the opt parameter, the connection pool cluster will contain two connections, named 'CONNECT_1' and 'CONNECT_2', that can be connected to different databases for operations let sql_1 = `INSERT INTO table SET id = 'aaa',value1 = 'bbb'`; db; let sql_2 = `INSERT INTO ?? SET ?`; db; let json = id:'eee' value:'fff'; db;
******Use in an async function: ****** const express = ;const app = ;app; app;
5: .$entire() method
.$entire() * You can perform multiple mysql operations asynchronously and ensure that all mysql operations are completed before returning relevant data
Parameter:
- The parameter type of the .$entire() method must be json and can have any parameter greater than 1
connectName: The connection sql: Key-value pairs for mysql statements or INSERT statements to values: Injection-proof operations store arrays of data optional or The table name string for the INSERT
Basal Example :
******Use in a non-async function: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*Since the option is an Array, async4mysql instance will create a cluster of connection pools // Depending on the opt parameter, the connection pool cluster will contain two connections, named 'CONNECT_1' and 'CONNECT_2', that can be connected to different databases for operations let P1 = connectName:'CONNECT_1' sql:`SELECT * FROM table WHERE value = 'value1'` // values:[] // *The values attribute can be omitted when anti-injection operations are not required ; let P2 = connectName:'CONNECT_2' sql:`INSERT INTO ?? SET id = ?, value = ?` values:'table' 'id' 'value'; let P3 = connectName:'CONNECT_1' sql:id:'_id' value:'_value' // *** When the sql attribute is of type json, that means the operation **must be** an INSERT operation values:'table' // *** When the sql property is of type json, the values property cannot be omitted, and its type must be a string, which represents the table name of the INSERT operation ; /* P3 equivalently: let P3 = { connectName:'CONNECT_1', sql:`INSERT INTO ?? SET ?`, values: ['table', {id:'_id', value:'_value'}] }; */ db;
******Use in an async function: ****** const express = ;const app = ;app; app;
6: .$query() method
.$query() Synchronously execute the specified all types of mysql statements ( Generally used to execute any mysql statements in addition to add, delete, change, check )
parameters :
.$query( connectName, sql, arr )
connectName: The connection name necessary
sql: mysql statement to execute necessary
arr: The array of injection prevention operation optional
Basal Example :
******Use in a non-async function: ****** const option1 = host:'localhost' user:'root_1' port:3306 password:'123456' database:'damo_1'; const option2 = host:'localhost' user:'root_2' port:3306 password:'123456' database:'damo_2'; let opt = connectName : 'CONNECT_1' option : option1 connectName : 'CONNECT_2' option : option2; const db = opt; //*Since the option is an Array, async4mysql instance will create a cluster of connection pools // Depending on the opt parameter, the connection pool cluster will contain two connections, named 'CONNECT_1' and 'CONNECT_2', that can be connected to different databases for operations let sql_1 = `SELECT COUNT(table_name) AS total FROM information_schema.tables WHERE table_schema = '库名' AND table_name='表名称'`; db; let sql_2 = `CREATE TABLE damo ( id varchar(11) DEFAULT NULL, _varchar varchar(100) DEFAULT NULL, _int int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8`; db;
******Use in an async function: ****** const express = ;const app = ;app; app;