@ssense/mysql
Advanced tools
Comparing version 1.0.3 to 1.0.4
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Connection = void 0; | ||
const mysql = require("mysql"); | ||
const mysql_1 = require("mysql"); | ||
class ConnectionWrapper { | ||
@@ -41,9 +41,11 @@ constructor(connection, releaseConnection) { | ||
} | ||
else if (options.connectionLimit && (typeof options.connectionLimit !== 'number' || options.connectionLimit <= 0)) { | ||
else if (options.connectionLimit && | ||
(typeof options.connectionLimit !== 'number' || options.connectionLimit <= 0)) { | ||
throw new Error('options.connectionLimit is invalid'); | ||
} | ||
else if (options.hasOwnProperty('timezone') && (typeof options.timezone !== 'string' || options.timezone.trim() === '')) { | ||
else if (options.hasOwnProperty('timezone') && | ||
(typeof options.timezone !== 'string' || options.timezone.trim() === '')) { | ||
throw new Error('options.timezone is invalid'); | ||
} | ||
this.pool = mysql.createPool(options); | ||
this.pool = (0, mysql_1.createPool)(options); | ||
} | ||
@@ -95,3 +97,3 @@ getPoolConnection() { | ||
transactionStarted = true; | ||
await wrapper.query(`LOCK TABLES ${locks.map(l => `${l.name} ${l.mode}`).join(', ')};`); | ||
await wrapper.query(`LOCK TABLES ${locks.map((l) => `${l.name} ${l.mode}`).join(', ')};`); | ||
tablesLocked = true; | ||
@@ -98,0 +100,0 @@ const result = await Promise.resolve(callback(wrapper)); |
{ | ||
"name": "@ssense/mysql", | ||
"version": "1.0.3", | ||
"description": "Helpers for accessing and sending queries to MySQL or MariaDB", | ||
"keywords": [ | ||
"MysqlConnection", | ||
"MySQL", | ||
"MariaDB", | ||
"Typescript" | ||
], | ||
"author": "Sirius Squad <sirius.squad@ssense.com>", | ||
"homepage": "https://github.com/Groupe-Atallah/pkg-node-utils#readme", | ||
"license": "ISC", | ||
"main": "lib/index.js", | ||
"directories": { | ||
"lib": "lib", | ||
"test": "__tests__" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Groupe-Atallah/pkg-node-utils.git" | ||
}, | ||
"scripts": { | ||
"tsc": "rm -rf lib/* && tsc", | ||
"test": "jest", | ||
"lint": "tslint -t verbose --project tsconfig-linter.json -c tslint.json ./src/{,**/}*.ts ./__tests__/{,**/}*.ts" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Groupe-Atallah/pkg-node-utils/issues" | ||
}, | ||
"types": "lib/index.d.ts", | ||
"devDependencies": { | ||
"typescript": "^4.7.4" | ||
}, | ||
"dependencies": { | ||
"mysql": "^2.18.1" | ||
} | ||
"name": "@ssense/mysql", | ||
"version": "1.0.4", | ||
"description": "Helpers for accessing and sending queries to MySQL or MariaDB", | ||
"keywords": [ | ||
"MysqlConnection", | ||
"MySQL", | ||
"MariaDB", | ||
"Typescript" | ||
], | ||
"author": "Sirius Squad <sirius.squad@ssense.com>", | ||
"homepage": "https://github.com/Groupe-Atallah/pkg-node-utils/tree/master/packages/mysql#readme", | ||
"license": "ISC", | ||
"main": "lib/index.js", | ||
"directories": { | ||
"lib": "lib", | ||
"test": "__tests__" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Groupe-Atallah/pkg-node-utils.git", | ||
"directory": "packages/mysql" | ||
}, | ||
"scripts": { | ||
"tsc": "rm -rf lib/* && tsc", | ||
"test": "jest", | ||
"lint": "eslint . --ext .ts --max-warnings=0", | ||
"format": "prettier --write .", | ||
"format:check": "prettier --check ." | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Groupe-Atallah/pkg-node-utils/issues" | ||
}, | ||
"types": "lib/index.d.ts", | ||
"dependencies": { | ||
"mysql": "^2.18.1" | ||
}, | ||
"devDependencies": { | ||
"@types/mysql": "^2.15.21" | ||
}, | ||
"eslintConfig": { | ||
"extends": "../../.eslintrc" | ||
} | ||
} |
@@ -11,9 +11,9 @@ [🡐 Go to main README page](../README.md) | ||
| Method | Returns | Description | | ||
|----------------|-----------------|--------------------| | ||
|[constructor](#constructoroptions-connectionoptions)(options: `ConnectionOptions`)|`Connection`|Creates a new instance of Connection| | ||
|[query](#querysql-string-params-any)(sql: `string`, params?: `any[]`)|`Promise<any>`|Sends a query to MySQL server and return a result| | ||
|[runInTransaction](#runintransactioncallback-TransactionFunction)(callback: `TransactionFunction`)|`Promise<any>`|Executes a list of statements in a MySQL transactional way, managing the transaction (begin, commit, rollback) automatically| | ||
|[runWithLockTables](#runwithlocktableslocks-locktableoption-callback-TransactionFunction)(locks: `LockTableOption[]`, callback: `TransactionFunction`)|`Promise<any>`|Same as `runInTransaction()` method, except it explicitly locks tables before running the transaction (calling `LOCK TABLES` instead of `START TRANSACTION`)| | ||
|[close](#close)()|`Promise<void>`|Closes all opened connections to the database and prevent new connections to be created| | ||
| Method | Returns | Description | | ||
| ------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| [constructor](#constructoroptions-connectionoptions)(options: `ConnectionOptions`) | `Connection` | Creates a new instance of Connection | | ||
| [query](#querysql-string-params-any)(sql: `string`, params?: `any[]`) | `Promise<any>` | Sends a query to MySQL server and return a result | | ||
| [runInTransaction](#runintransactioncallback-TransactionFunction)(callback: `TransactionFunction`) | `Promise<any>` | Executes a list of statements in a MySQL transactional way, managing the transaction (begin, commit, rollback) automatically | | ||
| [runWithLockTables](#runwithlocktableslocks-locktableoption-callback-TransactionFunction)(locks: `LockTableOption[]`, callback: `TransactionFunction`) | `Promise<any>` | Same as `runInTransaction()` method, except it explicitly locks tables before running the transaction (calling `LOCK TABLES` instead of `START TRANSACTION`) | | ||
| [close](#close)() | `Promise<void>` | Closes all opened connections to the database and prevent new connections to be created | | ||
@@ -28,5 +28,5 @@ ## Details | ||
|Name|Type|Required|Description| | ||
|---|---|:---:|---| | ||
|options|`ConnectionOptions`|Yes|The parameters used to connect to the MySQL server| | ||
| Name | Type | Required | Description | | ||
| ------- | ------------------- | :------: | -------------------------------------------------- | | ||
| options | `ConnectionOptions` | Yes | The parameters used to connect to the MySQL server | | ||
@@ -37,11 +37,11 @@ **ConnectionOptions** properties | ||
|Name|Type|Required|Description| | ||
|---|---|:---:|---| | ||
|host|`string`|Yes|MySQL server hostname or IP address| | ||
|database|`string`|Yes|Name of database to use| | ||
|port|`number`|No|MySQL port (default: `3306`)| | ||
|user|`string`|No|MySQL username (default: `null`)| | ||
|password|`string`|No|MySQL password (default: `null`)| | ||
|connectionLimit|`number`|No|Maximum number of parallel connections in internal MySQL connection pool (default: `10`)| | ||
|timezone|`string`|No|The timezone configured on the MySQL server. This is used to type cast server date/time values to JavaScript Date object and vice versa. (default: `'local'`)| | ||
| Name | Type | Required | Description | | ||
| --------------- | -------- | :------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| host | `string` | Yes | MySQL server hostname or IP address | | ||
| database | `string` | Yes | Name of database to use | | ||
| port | `number` | No | MySQL port (default: `3306`) | | ||
| user | `string` | No | MySQL username (default: `null`) | | ||
| password | `string` | No | MySQL password (default: `null`) | | ||
| connectionLimit | `number` | No | Maximum number of parallel connections in internal MySQL connection pool (default: `10`) | | ||
| timezone | `string` | No | The timezone configured on the MySQL server. This is used to type cast server date/time values to JavaScript Date object and vice versa. (default: `'local'`) | | ||
@@ -54,12 +54,12 @@ ### **`query(sql: string, params?: any[])`** | ||
|Name|Type|Required|Description| | ||
|---|---|:---:|---| | ||
|sql|`string`|Yes|SQL query| | ||
|params|`any[]`|No|SQL query params for a query with parameters (will be protected against SQL injections, see [mysql npm module](https://www.npmjs.com/package/mysql#escaping-query-values) for more detail)| | ||
| Name | Type | Required | Description | | ||
| ------ | -------- | :------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| sql | `string` | Yes | SQL query | | ||
| params | `any[]` | No | SQL query params for a query with parameters (will be protected against SQL injections, see [mysql npm module](https://www.npmjs.com/package/mysql#escaping-query-values) for more detail) | | ||
#### Return value | ||
|Type|Description| | ||
|---|---| | ||
|`Promise<any>`|Result of the executed query| | ||
| Type | Description | | ||
| -------------- | ---------------------------- | | ||
| `Promise<any>` | Result of the executed query | | ||
@@ -72,5 +72,5 @@ ### **`runInTransaction(callback: TransactionFunction)`** | ||
|Name|Type|Required|Description| | ||
|---|---|:---:|---| | ||
|callback|`TransactionFunction`|Yes|Function in which all the MySQL statements can be executed (will be run in a MySQL transaction)| | ||
| Name | Type | Required | Description | | ||
| -------- | --------------------- | :------: | ----------------------------------------------------------------------------------------------- | | ||
| callback | `TransactionFunction` | Yes | Function in which all the MySQL statements can be executed (will be run in a MySQL transaction) | | ||
@@ -84,5 +84,5 @@ **TransactionFunction** definition | ||
|Type|Description| | ||
|---|---| | ||
|`Promise<any>`|Result of the executed transaction| | ||
| Type | Description | | ||
| -------------- | ---------------------------------- | | ||
| `Promise<any>` | Result of the executed transaction | | ||
@@ -95,13 +95,13 @@ ### **`runWithLockTables(locks: LockTableOption[], callback: TransactionFunction)`** | ||
|Name|Type|Required|Description| | ||
|---|---|:---:|---| | ||
|locks|`LockTableOption[]`|Yes|Array of LockTableOption (tables to lock with lock mode)| | ||
|callback|`TransactionFunction`|Yes|Function in which all the MySQL statements can be executed (will be run in a MySQL transaction)| | ||
| Name | Type | Required | Description | | ||
| -------- | --------------------- | :------: | ----------------------------------------------------------------------------------------------- | | ||
| locks | `LockTableOption[]` | Yes | Array of LockTableOption (tables to lock with lock mode) | | ||
| callback | `TransactionFunction` | Yes | Function in which all the MySQL statements can be executed (will be run in a MySQL transaction) | | ||
**LockTableOption** properties | ||
|Name|Type|Required|Description| | ||
|---|---|:---:|---| | ||
|name|`string`|Yes|Name of the table to lock| | ||
|mode|`'READ'`\|`'WRITE'`|Yes|Lock mode to use, must be one of `'READ'` or `'WRITE'`| | ||
| Name | Type | Required | Description | | ||
| ---- | ------------------- | :------: | ------------------------------------------------------ | | ||
| name | `string` | Yes | Name of the table to lock | | ||
| mode | `'READ'`\|`'WRITE'` | Yes | Lock mode to use, must be one of `'READ'` or `'WRITE'` | | ||
@@ -114,5 +114,5 @@ **TransactionFunction** definition | ||
|Type|Description| | ||
|---|---| | ||
|`Promise<any>`|Result of the executed transaction| | ||
| Type | Description | | ||
| -------------- | ---------------------------------- | | ||
| `Promise<any>` | Result of the executed transaction | | ||
@@ -131,3 +131,3 @@ ### **`close()`** | ||
// Create connection | ||
const connection = new Connection({... params}); | ||
const connection = new Connection({ ...params }); | ||
@@ -154,7 +154,10 @@ // Run multiple MySQL commands inside a managed transaction | ||
// Create connection | ||
const connection = new Connection({... params}); | ||
const connection = new Connection({ ...params }); | ||
// Run multiple MySQL commands inside a managed transaction | ||
const result = await connection.runWithLockTables( | ||
[{name: 'users', mode: 'WRITE'}, {name: 'accounts', mode: 'WRITE'}], | ||
[ | ||
{ name: 'users', mode: 'WRITE' }, | ||
{ name: 'accounts', mode: 'WRITE' }, | ||
], | ||
async (transaction) => { | ||
@@ -168,3 +171,3 @@ // When reaching this part of the code, both "users" and "accounts" tables will be locked, even if we don't perfom any query on the "accounts" table | ||
return users[0]; | ||
} | ||
}, | ||
); | ||
@@ -171,0 +174,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
22727
161
167