@stashfin/mysql2 1.2.16 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ import mysql, { RowDataPacket, PoolOptions, ResultSetHeader } from 'mysql2/promise';
2
+ export interface MysqlConnectorOptions extends PoolOptions {
3
+ }
4
+ export declare class MysqlConnector {
5
+ private pool;
6
+ private credentials;
7
+ constructor(credentials: PoolOptions);
8
+ /** A random method to simulate a step before to get the class methods */
9
+ getConnection(): Promise<mysql.PoolConnection>;
10
+ queryRow<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T | undefined>;
11
+ queryRows<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
12
+ queryArray<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
13
+ executeResult(sql: string, values?: unknown[]): Promise<ResultSetHeader>;
14
+ insertData(tableName: string, data: Record<string, string | number>): Promise<ResultSetHeader>;
15
+ updateData(tableName: string, data: Record<string, any>, condition: string, conditionValues?: Array<string | number>): Promise<ResultSetHeader>;
16
+ close(): Promise<void>;
17
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MysqlConnector = void 0;
7
+ const promise_1 = __importDefault(require("mysql2/promise"));
8
+ //define class
9
+ class MysqlConnector {
10
+ pool;
11
+ credentials;
12
+ constructor(credentials) {
13
+ this.credentials = credentials;
14
+ this.pool = promise_1.default.createPool(this.credentials);
15
+ }
16
+ /** A random method to simulate a step before to get the class methods */
17
+ getConnection() {
18
+ if (!this?.pool)
19
+ this.pool = promise_1.default.createPool(this.credentials);
20
+ return this.pool.getConnection();
21
+ }
22
+ async queryRow(sql, values) {
23
+ const connection = await this.getConnection();
24
+ const [rows] = await connection.query(sql, values);
25
+ await connection.release();
26
+ return rows[0] || undefined;
27
+ }
28
+ async queryRows(sql, values) {
29
+ const connection = await this.getConnection();
30
+ const [rows] = await connection.query(sql, values);
31
+ await connection.release();
32
+ return rows;
33
+ }
34
+ async queryArray(sql, values) {
35
+ const connection = await this.getConnection();
36
+ const [rows] = await connection.query({
37
+ sql,
38
+ values,
39
+ rowsAsArray: true,
40
+ });
41
+ await connection.release();
42
+ return rows;
43
+ }
44
+ async executeResult(sql, values) {
45
+ const connection = await this.pool.getConnection();
46
+ const [rows] = await connection.query(sql, values);
47
+ await connection.release();
48
+ return rows;
49
+ }
50
+ async insertData(tableName, data) {
51
+ const keys = Object.keys(data);
52
+ const values = Object.values(data);
53
+ const sql = `INSERT INTO ${tableName} (${keys.join(', ')}) VALUES (${keys
54
+ .map(() => '?')
55
+ .join(', ')})`;
56
+ return this.executeResult(sql, values);
57
+ }
58
+ async updateData(tableName,
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ data, condition, conditionValues) {
61
+ const keys = Object.keys(data);
62
+ const values = Object.values(data);
63
+ const sql = `UPDATE ${tableName} SET ${keys
64
+ .map((key) => `${key} = ?`)
65
+ .join(', ')} WHERE ${condition}`;
66
+ const finalValues = [...values, ...(conditionValues || [])];
67
+ return this.executeResult(sql, finalValues);
68
+ }
69
+ async close() {
70
+ await this.pool.end();
71
+ }
72
+ }
73
+ exports.MysqlConnector = MysqlConnector;
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import mysql, { RowDataPacket, PoolOptions, ResultSetHeader } from 'mysql2/promise';
1
+ import mysql, { RowDataPacket, PoolOptions, ResultSetHeader } from 'mysql2';
2
2
  export interface MysqlConnectorOptions extends PoolOptions {
3
3
  }
4
4
  export declare class MysqlConnector {
@@ -6,7 +6,7 @@ export declare class MysqlConnector {
6
6
  private credentials;
7
7
  constructor(credentials: PoolOptions);
8
8
  /** A random method to simulate a step before to get the class methods */
9
- getConnection(): Promise<mysql.PoolConnection>;
9
+ getConnection(): mysql.Pool;
10
10
  queryRow<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T | undefined>;
11
11
  queryRows<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
12
12
  queryArray<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
package/index.js CHANGED
@@ -4,48 +4,68 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.MysqlConnector = void 0;
7
- const promise_1 = __importDefault(require("mysql2/promise"));
7
+ const mysql2_1 = __importDefault(require("mysql2"));
8
8
  //define class
9
9
  class MysqlConnector {
10
10
  pool;
11
11
  credentials;
12
12
  constructor(credentials) {
13
13
  this.credentials = credentials;
14
- this.pool = promise_1.default.createPool(this.credentials);
14
+ this.pool = mysql2_1.default.createPool(this.credentials);
15
15
  }
16
16
  /** A random method to simulate a step before to get the class methods */
17
17
  getConnection() {
18
18
  if (!this?.pool)
19
- this.pool = promise_1.default.createPool(this.credentials);
20
- return this.pool.getConnection();
19
+ this.pool = mysql2_1.default.createPool(this.credentials);
20
+ return this.pool;
21
21
  }
22
22
  async queryRow(sql, values) {
23
- const connection = await this.getConnection();
24
- const [rows] = await connection.query(sql, values);
25
- await connection.release();
26
- return rows[0] || undefined;
23
+ const pool = this.getConnection();
24
+ return new Promise((resolve, reject) => {
25
+ pool.query(sql, values, function (err, results, fields) {
26
+ if (err)
27
+ reject(err);
28
+ // Connection is automatically released when query resolves
29
+ else
30
+ resolve(results[0] || undefined);
31
+ });
32
+ });
27
33
  }
28
34
  async queryRows(sql, values) {
29
- const connection = await this.getConnection();
30
- const [rows] = await connection.query(sql, values);
31
- await connection.release();
32
- return rows;
35
+ const pool = this.getConnection();
36
+ return new Promise((resolve, reject) => {
37
+ pool.query(sql, values, function (err, results, fields) {
38
+ // Connection is automatically released when query resolves
39
+ if (err)
40
+ reject(err);
41
+ else
42
+ resolve(results);
43
+ });
44
+ });
33
45
  }
34
46
  async queryArray(sql, values) {
35
- const connection = await this.getConnection();
36
- const [rows] = await connection.query({
37
- sql,
38
- values,
39
- rowsAsArray: true,
47
+ const pool = this.getConnection();
48
+ return new Promise((resolve, reject) => {
49
+ pool.query({ sql, values, rowsAsArray: true }, function (err, results, fields) {
50
+ // Connection is automatically released when query resolves
51
+ if (err)
52
+ reject(err);
53
+ else
54
+ resolve(results);
55
+ });
40
56
  });
41
- await connection.release();
42
- return rows;
43
57
  }
44
58
  async executeResult(sql, values) {
45
- const connection = await this.pool.getConnection();
46
- const [rows] = await connection.query(sql, values);
47
- await connection.release();
48
- return rows;
59
+ const pool = this.getConnection();
60
+ return new Promise((resolve, reject) => {
61
+ pool.query(sql, values, function (err, results, fields) {
62
+ // Connection is automatically released when query resolves
63
+ if (err)
64
+ reject(err);
65
+ else
66
+ resolve(results || undefined);
67
+ });
68
+ });
49
69
  }
50
70
  async insertData(tableName, data) {
51
71
  const keys = Object.keys(data);
@@ -0,0 +1,17 @@
1
+ import mysql, { RowDataPacket, PoolOptions, ResultSetHeader } from 'mysql2/promise';
2
+ export interface MysqlConnectorOptions extends PoolOptions {
3
+ }
4
+ export declare class MysqlConnector {
5
+ private pool;
6
+ private credentials;
7
+ constructor(credentials: PoolOptions);
8
+ /** A random method to simulate a step before to get the class methods */
9
+ getConnection(): Promise<mysql.PoolConnection>;
10
+ queryRow<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T | undefined>;
11
+ queryRows<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
12
+ queryArray<T extends RowDataPacket>(sql: string, values?: Array<string | number>): Promise<T[]>;
13
+ executeResult(sql: string, values?: unknown[]): Promise<ResultSetHeader>;
14
+ insertData(tableName: string, data: Record<string, string | number>): Promise<ResultSetHeader>;
15
+ updateData(tableName: string, data: Record<string, any>, condition: string, conditionValues?: Array<string | number>): Promise<ResultSetHeader>;
16
+ close(): Promise<void>;
17
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MysqlConnector = void 0;
7
+ const promise_1 = __importDefault(require("mysql2/promise"));
8
+ //define class
9
+ class MysqlConnector {
10
+ pool;
11
+ credentials;
12
+ constructor(credentials) {
13
+ this.credentials = credentials;
14
+ this.pool = promise_1.default.createPool(this.credentials);
15
+ }
16
+ /** A random method to simulate a step before to get the class methods */
17
+ getConnection() {
18
+ if (!this?.pool)
19
+ this.pool = promise_1.default.createPool(this.credentials);
20
+ return this.pool.getConnection();
21
+ }
22
+ async queryRow(sql, values) {
23
+ const connection = await this.getConnection();
24
+ const [rows] = await connection.query(sql, values);
25
+ await connection.release();
26
+ return rows[0] || undefined;
27
+ }
28
+ async queryRows(sql, values) {
29
+ const connection = await this.getConnection();
30
+ const [rows] = await connection.query(sql, values);
31
+ await connection.release();
32
+ return rows;
33
+ }
34
+ async queryArray(sql, values) {
35
+ const connection = await this.getConnection();
36
+ const [rows] = await connection.query({
37
+ sql,
38
+ values,
39
+ rowsAsArray: true,
40
+ });
41
+ await connection.release();
42
+ return rows;
43
+ }
44
+ async executeResult(sql, values) {
45
+ const connection = await this.pool.getConnection();
46
+ const [rows] = await connection.query(sql, values);
47
+ await connection.release();
48
+ return rows;
49
+ }
50
+ async insertData(tableName, data) {
51
+ const keys = Object.keys(data);
52
+ const values = Object.values(data);
53
+ const sql = `INSERT INTO ${tableName} (${keys.join(', ')}) VALUES (${keys
54
+ .map(() => '?')
55
+ .join(', ')})`;
56
+ return this.executeResult(sql, values);
57
+ }
58
+ async updateData(tableName,
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ data, condition, conditionValues) {
61
+ const keys = Object.keys(data);
62
+ const values = Object.values(data);
63
+ const sql = `UPDATE ${tableName} SET ${keys
64
+ .map((key) => `${key} = ?`)
65
+ .join(', ')} WHERE ${condition}`;
66
+ const finalValues = [...values, ...(conditionValues || [])];
67
+ return this.executeResult(sql, finalValues);
68
+ }
69
+ async close() {
70
+ await this.pool.end();
71
+ }
72
+ }
73
+ exports.MysqlConnector = MysqlConnector;
package/models/Loans.d.ts CHANGED
@@ -25,7 +25,7 @@ export interface LoansTable {
25
25
  source?: 'app' | 'thirdparty' | null;
26
26
  ip?: string | null;
27
27
  approval_date?: Date | null;
28
- disbursal_date?: Date | null;
28
+ disbursal_date?: string | null;
29
29
  closure_date?: Date | null;
30
30
  is_auto_processing?: number | null;
31
31
  remarks?: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stashfin/mysql2",
3
- "version": "1.2.16",
3
+ "version": "1.3.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",