@fluidware-it/mysql2-client 0.6.0 → 0.6.2
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.
- package/build/esm/config.d.ts +1 -1
- package/build/esm/config.js.map +1 -1
- package/build/esm/dbClient.d.ts +2 -2
- package/build/esm/dbClient.js.map +1 -1
- package/build/esnext/config.d.ts +1 -1
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/dbClient.d.ts +2 -2
- package/build/esnext/dbClient.js.map +1 -1
- package/build/src/config.d.ts +1 -1
- package/build/src/config.js.map +1 -1
- package/build/src/dbClient.d.ts +2 -2
- package/build/src/dbClient.js.map +1 -1
- package/package.json +2 -2
package/build/esm/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConnectionOptions } from 'mysql2';
|
|
1
|
+
import type { ConnectionOptions } from 'mysql2';
|
|
2
2
|
export declare const USE_READ_COMMITTED_ISOLATION: boolean;
|
|
3
3
|
export declare function getMysqlConnectionOptions(prefix?: string): ConnectionOptions;
|
|
4
4
|
export declare function setMysqlConnectionOptions(prefix: string, options: ConnectionOptions): void;
|
package/build/esm/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AAEzB,MAAM,CAAC,IAAM,4BAA4B,GAAG,QAAQ,CAAC,OAAO,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;AAE1G,SAAS,aAAa,CAAC,gBAAwB,EAAE,WAAmB;IAClE,IAAI,gBAAgB,EAAE;QACpB,OAAO,E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AAC;QACF,IAAI,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;YAC3C,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;SAC3C;QACD,eAAe,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACrC;IACD,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,MAAc,EAAE,OAA0B;IAClF,eAAe,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;AACpC,CAAC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { ConnectionOptions } from 'mysql2';\nimport { EnvParse } from '@fluidware-it/saddlebag';\nimport * as fs from 'fs';\n\nexport const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('FW_DB_USE_READ_COMMITTED_ISOLATION', false);\n\nfunction getDbPassword(DB_PASSWORD_FILE: string, DB_PASSWORD: string): string {\n if (DB_PASSWORD_FILE) {\n return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');\n }\n return DB_PASSWORD;\n}\n\nconst memoizedOptions: { [prefix: string]: ConnectionOptions } = {};\n\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\n if (!memoizedOptions[prefix]) {\n const DB_PASSWORD = EnvParse.envString(`FW_${prefix}DB_PASSWORD`, '');\n const DB_PASSWORD_FILE = EnvParse.envString(`FW_${prefix}DB_PASSWORD_FILE`, '');\n\n if (!DB_PASSWORD && !DB_PASSWORD_FILE) {\n throw new Error('FW_DB_PASSWORD or FW_DB_PASSWORD_FILE env is required');\n }\n\n const DB_USER = EnvParse.envStringRequired(`FW_${prefix}DB_USER`);\n const DB_HOST = EnvParse.envString(`FW_${prefix}DB_HOST`, 'localhost');\n const DB_PORT = EnvParse.envInt(`FW_${prefix}DB_PORT`, 3306);\n const DB_NAME = EnvParse.envString(`FW_${prefix}DB_NAME`, DB_USER);\n // FW_${prefix}DB_CONN_OPTIONS: JSON string with connection options See https://github.com/mysqljs/mysql#connection-options for all possible options\n const DB_CONN_OPTIONS = EnvParse.envJSON(`FW_${prefix}DB_CONN_OPTIONS`, {});\n\n const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);\n const dbOptions = {\n host: DB_HOST,\n port: DB_PORT,\n user: DB_USER,\n password: dbPassword,\n database: DB_NAME\n };\n if (Object.keys(DB_CONN_OPTIONS).length > 0) {\n Object.assign(dbOptions, DB_CONN_OPTIONS);\n }\n memoizedOptions[prefix] = dbOptions;\n }\n return memoizedOptions[prefix];\n}\n\nexport function setMysqlConnectionOptions(prefix: string, options: ConnectionOptions) {\n memoizedOptions[prefix] = options;\n}\n"]}
|
package/build/esm/dbClient.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { ConnectionOptions } from 'mysql2';
|
|
1
2
|
import { Connection, RowDataPacket, ResultSetHeader, ProcedureCallPacket, FieldPacket } from 'mysql2/promise';
|
|
2
|
-
import { ConnectionOptions } from 'mysql2';
|
|
3
3
|
interface ConnectionWrap extends Connection {
|
|
4
4
|
run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(sql: string, phs?: (string | number | boolean | null)[]): Promise<{
|
|
5
5
|
rows: T;
|
|
@@ -21,7 +21,7 @@ export declare class DbClient {
|
|
|
21
21
|
insert(sql: string, phs?: (string | number | boolean | null)[]): Promise<number>;
|
|
22
22
|
update(sql: string, phs?: (string | number | boolean | null)[], returnChangedRows?: boolean): Promise<number>;
|
|
23
23
|
delete(sql: string, phs?: (string | number | boolean | null)[]): Promise<number>;
|
|
24
|
-
run(sql: string, phs?: (string | number | boolean | null)[]): Promise<[import("mysql2
|
|
24
|
+
run(sql: string, phs?: (string | number | boolean | null)[]): Promise<[import("mysql2").QueryResult, FieldPacket[]]>;
|
|
25
25
|
}
|
|
26
26
|
export {};
|
|
27
27
|
//# sourceMappingURL=dbClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGH,OAAO,EACL,gBAAgB,EAMjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAYnF;IAIE,kBAAY,yBAAsD;QAChE,IAAI,yBAAyB,EAAE;YAC7B,IAAI,OAAO,yBAAyB,KAAK,QAAQ,EAAE;gBACjD,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC,yBAAyB,CAAC,CAAC;aAC/E;iBAAM;gBACL,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC;aACpD;SACF;aAAM;YACL,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC,EAAE,CAAC,CAAC;SACxD;IACH,CAAC;IAEK,uBAAI,GAAV;;;;;;;wBACE,KAAA,IAAI,CAAA;wBAAe,qBAAM,gBAAgB,CAAC,IAAI,CAAC,iBAAiB,CAAC,EAAA;;wBAAjE,GAAK,UAAU,GAAG,CAAC,SAA8C,CAAmB,CAAC;wBACrF,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;wBAChE,sBAAO,GAAG,CAAC,IAAI,CAAC,YAAY,EAAC;;;;KAC9B;IAEK,sBAAG,GAAT,UAAU,GAAW,EAAE,GAA0C;;;gBAC/D,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;oBACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;iBAC5C;gBACD,sBAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,EAAC;;;KAC1C;IACH,eAAC;AAAD,CAAC,AA5HD,IA4HC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { ConnectionOptions } from 'mysql2';\nimport {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean | null)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private readonly connectionOptions: ConnectionOptions;\n\n constructor(connectionOptionsOrPrefix?: ConnectionOptions | string) {\n if (connectionOptionsOrPrefix) {\n if (typeof connectionOptionsOrPrefix === 'string') {\n this.connectionOptions = getMysqlConnectionOptions(connectionOptionsOrPrefix);\n } else {\n this.connectionOptions = connectionOptionsOrPrefix;\n }\n } else {\n this.connectionOptions = getMysqlConnectionOptions('');\n }\n }\n\n async open() {\n this.connection = (await createConnection(this.connectionOptions)) as ConnectionWrap;\n this.connection.run = async <\n T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket\n >(\n sql: string,\n phs?: (string | number | boolean | null)[]\n ): Promise<{ rows: T; cols: FieldPacket[] }> => {\n if (!this.connection) {\n throw new Error('run() called but no connection available');\n }\n const [rows, cols] = await this.connection.execute<T>(sql, phs);\n return {\n rows,\n cols\n };\n };\n }\n\n getConnection(): ConnectionWrap | undefined {\n return this.connection;\n }\n\n async close() {\n if (this.connection) {\n await this.connection.end();\n }\n }\n\n async startTransaction() {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n if (USE_READ_COMMITTED_ISOLATION) {\n await this.connection.execute(\"SET SESSION tx_isolation='read-committed'\");\n }\n await this.connection.execute('SET AUTOCOMMIT=0');\n }\n\n async commit(closeTransaction = true) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n await this.connection.execute('COMMIT');\n if (closeTransaction) {\n await this.connection.execute('SET AUTOCOMMIT=1');\n }\n }\n\n async rollback(closeTransaction = true) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n\n await this.connection.execute('ROLLBACK');\n if (closeTransaction) {\n await this.connection.execute('SET AUTOCOMMIT=1');\n }\n }\n\n async all(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<RowDataPacket[]>(sql, phs);\n return res.rows;\n }\n\n async get(sql: string, phs?: (string | number | boolean | null)[]): Promise<RowDataPacket | null> {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<RowDataPacket[]>(sql, phs);\n if (res.rows.length > 1) throw new Error('get() returned more than one row');\n if (res.rows.length === 1) return res.rows[0];\n return null;\n }\n\n async insert(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return res.rows.insertId || res.rows.affectedRows;\n }\n\n async update(sql: string, phs?: (string | number | boolean | null)[], returnChangedRows = false) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return returnChangedRows ? res.rows.changedRows : res.rows.affectedRows;\n }\n\n async delete(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return res.rows.affectedRows;\n }\n\n async run(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n return this.connection.execute(sql, phs);\n }\n}\n"]}
|
package/build/esnext/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConnectionOptions } from 'mysql2';
|
|
1
|
+
import type { ConnectionOptions } from 'mysql2';
|
|
2
2
|
export declare const USE_READ_COMMITTED_ISOLATION: boolean;
|
|
3
3
|
export declare function getMysqlConnectionOptions(prefix?: string): ConnectionOptions;
|
|
4
4
|
export declare function setMysqlConnectionOptions(prefix: string, options: ConnectionOptions): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AAEzB,MAAM,CAAC,MAAM,4BAA4B,GAAG,QAAQ,CAAC,OAAO,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;AAE1G,SAAS,aAAa,CAAC,gBAAwB,EAAE,WAAmB;IAClE,IAAI,gBAAgB,EAAE;QACpB,OAAO,EAAE,CAAC,YAAY,CAAC,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C,eAAe,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;YAC3C,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;SAC3C;QACD,eAAe,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACrC;IACD,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,MAAc,EAAE,OAA0B;IAClF,eAAe,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;AACpC,CAAC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { ConnectionOptions } from 'mysql2';\nimport { EnvParse } from '@fluidware-it/saddlebag';\nimport * as fs from 'fs';\n\nexport const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('FW_DB_USE_READ_COMMITTED_ISOLATION', false);\n\nfunction getDbPassword(DB_PASSWORD_FILE: string, DB_PASSWORD: string): string {\n if (DB_PASSWORD_FILE) {\n return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');\n }\n return DB_PASSWORD;\n}\n\nconst memoizedOptions: { [prefix: string]: ConnectionOptions } = {};\n\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\n if (!memoizedOptions[prefix]) {\n const DB_PASSWORD = EnvParse.envString(`FW_${prefix}DB_PASSWORD`, '');\n const DB_PASSWORD_FILE = EnvParse.envString(`FW_${prefix}DB_PASSWORD_FILE`, '');\n\n if (!DB_PASSWORD && !DB_PASSWORD_FILE) {\n throw new Error('FW_DB_PASSWORD or FW_DB_PASSWORD_FILE env is required');\n }\n\n const DB_USER = EnvParse.envStringRequired(`FW_${prefix}DB_USER`);\n const DB_HOST = EnvParse.envString(`FW_${prefix}DB_HOST`, 'localhost');\n const DB_PORT = EnvParse.envInt(`FW_${prefix}DB_PORT`, 3306);\n const DB_NAME = EnvParse.envString(`FW_${prefix}DB_NAME`, DB_USER);\n // FW_${prefix}DB_CONN_OPTIONS: JSON string with connection options See https://github.com/mysqljs/mysql#connection-options for all possible options\n const DB_CONN_OPTIONS = EnvParse.envJSON(`FW_${prefix}DB_CONN_OPTIONS`, {});\n\n const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);\n const dbOptions = {\n host: DB_HOST,\n port: DB_PORT,\n user: DB_USER,\n password: dbPassword,\n database: DB_NAME\n };\n if (Object.keys(DB_CONN_OPTIONS).length > 0) {\n Object.assign(dbOptions, DB_CONN_OPTIONS);\n }\n memoizedOptions[prefix] = dbOptions;\n }\n return memoizedOptions[prefix];\n}\n\nexport function setMysqlConnectionOptions(prefix: string, options: ConnectionOptions) {\n memoizedOptions[prefix] = options;\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { ConnectionOptions } from 'mysql2';
|
|
1
2
|
import { Connection, RowDataPacket, ResultSetHeader, ProcedureCallPacket, FieldPacket } from 'mysql2/promise';
|
|
2
|
-
import { ConnectionOptions } from 'mysql2';
|
|
3
3
|
interface ConnectionWrap extends Connection {
|
|
4
4
|
run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(sql: string, phs?: (string | number | boolean | null)[]): Promise<{
|
|
5
5
|
rows: T;
|
|
@@ -21,7 +21,7 @@ export declare class DbClient {
|
|
|
21
21
|
insert(sql: string, phs?: (string | number | boolean | null)[]): Promise<number>;
|
|
22
22
|
update(sql: string, phs?: (string | number | boolean | null)[], returnChangedRows?: boolean): Promise<number>;
|
|
23
23
|
delete(sql: string, phs?: (string | number | boolean | null)[]): Promise<number>;
|
|
24
|
-
run(sql: string, phs?: (string | number | boolean | null)[]): Promise<[import("mysql2
|
|
24
|
+
run(sql: string, phs?: (string | number | boolean | null)[]): Promise<[import("mysql2").QueryResult, FieldPacket[]]>;
|
|
25
25
|
}
|
|
26
26
|
export {};
|
|
27
27
|
//# sourceMappingURL=dbClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;
|
|
1
|
+
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EACL,gBAAgB,EAMjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAYnF,MAAM,OAAO,QAAQ;IAInB,YAAY,yBAAsD;QAChE,IAAI,yBAAyB,EAAE;YAC7B,IAAI,OAAO,yBAAyB,KAAK,QAAQ,EAAE;gBACjD,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC,yBAAyB,CAAC,CAAC;aAC/E;iBAAM;gBACL,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC;aACpD;SACF;aAAM;YACL,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC,EAAE,CAAC,CAAC;SACxD;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,UAAU,GAAG,CAAC,MAAM,gBAAgB,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAmB,CAAC;QACrF,IAAI,CAAC,UAAU,CAAC,GAAG,GAAG,KAAK,EAGzB,GAAW,EACX,GAA0C,EACC,EAAE;YAC7C,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;gBACpB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;aAC7D;YACD,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,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,GAAW,EAAE,GAA0C,EAAE,iBAAiB,GAAG,KAAK;QAC7F,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;SAC5C;QACD,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAkB,GAAG,EAAE,GAAG,CAAC,CAAC;QACjE,OAAO,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC;IAC1E,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAW,EAAE,GAA0C;QAClE,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;SAC5C;QACD,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAkB,GAAG,EAAE,GAAG,CAAC,CAAC;QACjE,OAAO,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,GAA0C;QAC/D,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;SAC5C;QACD,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC3C,CAAC;CACF","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { ConnectionOptions } from 'mysql2';\nimport {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean | null)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private readonly connectionOptions: ConnectionOptions;\n\n constructor(connectionOptionsOrPrefix?: ConnectionOptions | string) {\n if (connectionOptionsOrPrefix) {\n if (typeof connectionOptionsOrPrefix === 'string') {\n this.connectionOptions = getMysqlConnectionOptions(connectionOptionsOrPrefix);\n } else {\n this.connectionOptions = connectionOptionsOrPrefix;\n }\n } else {\n this.connectionOptions = getMysqlConnectionOptions('');\n }\n }\n\n async open() {\n this.connection = (await createConnection(this.connectionOptions)) as ConnectionWrap;\n this.connection.run = async <\n T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket\n >(\n sql: string,\n phs?: (string | number | boolean | null)[]\n ): Promise<{ rows: T; cols: FieldPacket[] }> => {\n if (!this.connection) {\n throw new Error('run() called but no connection available');\n }\n const [rows, cols] = await this.connection.execute<T>(sql, phs);\n return {\n rows,\n cols\n };\n };\n }\n\n getConnection(): ConnectionWrap | undefined {\n return this.connection;\n }\n\n async close() {\n if (this.connection) {\n await this.connection.end();\n }\n }\n\n async startTransaction() {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n if (USE_READ_COMMITTED_ISOLATION) {\n await this.connection.execute(\"SET SESSION tx_isolation='read-committed'\");\n }\n await this.connection.execute('SET AUTOCOMMIT=0');\n }\n\n async commit(closeTransaction = true) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n await this.connection.execute('COMMIT');\n if (closeTransaction) {\n await this.connection.execute('SET AUTOCOMMIT=1');\n }\n }\n\n async rollback(closeTransaction = true) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n\n await this.connection.execute('ROLLBACK');\n if (closeTransaction) {\n await this.connection.execute('SET AUTOCOMMIT=1');\n }\n }\n\n async all(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<RowDataPacket[]>(sql, phs);\n return res.rows;\n }\n\n async get(sql: string, phs?: (string | number | boolean | null)[]): Promise<RowDataPacket | null> {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<RowDataPacket[]>(sql, phs);\n if (res.rows.length > 1) throw new Error('get() returned more than one row');\n if (res.rows.length === 1) return res.rows[0];\n return null;\n }\n\n async insert(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return res.rows.insertId || res.rows.affectedRows;\n }\n\n async update(sql: string, phs?: (string | number | boolean | null)[], returnChangedRows = false) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return returnChangedRows ? res.rows.changedRows : res.rows.affectedRows;\n }\n\n async delete(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return res.rows.affectedRows;\n }\n\n async run(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n return this.connection.execute(sql, phs);\n }\n}\n"]}
|
package/build/src/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConnectionOptions } from 'mysql2';
|
|
1
|
+
import type { ConnectionOptions } from 'mysql2';
|
|
2
2
|
export declare const USE_READ_COMMITTED_ISOLATION: boolean;
|
|
3
3
|
export declare function getMysqlConnectionOptions(prefix?: string): ConnectionOptions;
|
|
4
4
|
export declare function setMysqlConnectionOptions(prefix: string, options: ConnectionOptions): void;
|
package/build/src/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAGH,uDAAmD;AACnD,yBAAyB;AAEZ,QAAA,4BAA4B,GAAG,oBAAQ,CAAC,OAAO,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;AAE1G,SAAS,aAAa,CAAC,gBAAwB,EAAE,WAAmB;IAClE,IAAI,gBAAgB,EAAE;QACpB,OAAO,EAAE,CAAC,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;KAClD;IACD,OAAO,WAAW,CAAC;AACrB,CAAC;AAED,MAAM,eAAe,GAA4C,EAAE,CAAC;AAEpE,SAAgB,yBAAyB,CAAC,MAAM,GAAG,EAAE;IACnD,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,EAAE;QAC5B,MAAM,WAAW,GAAG,oBAAQ,CAAC,SAAS,CAAC,MAAM,MAAM,aAAa,EAAE,EAAE,CAAC,CAAC;QACtE,MAAM,gBAAgB,GAAG,oBAAQ,CAAC,SAAS,CAAC,MAAM,MAAM,kBAAkB,EAAE,EAAE,CAAC,CAAC;QAEhF,IAAI,CAAC,WAAW,IAAI,CAAC,gBAAgB,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;SAC1E;QAED,MAAM,OAAO,GAAG,oBAAQ,CAAC,iBAAiB,CAAC,MAAM,MAAM,SAAS,CAAC,CAAC;QAClE,MAAM,OAAO,GAAG,oBAAQ,CAAC,SAAS,CAAC,MAAM,MAAM,SAAS,EAAE,WAAW,CAAC,CAAC;QACvE,MAAM,OAAO,GAAG,oBAAQ,CAAC,MAAM,CAAC,MAAM,MAAM,SAAS,EAAE,IAAI,CAAC,CAAC;QAC7D,MAAM,OAAO,GAAG,oBAAQ,CAAC,SAAS,CAAC,MAAM,MAAM,SAAS,EAAE,OAAO,CAAC,CAAC;QACnE,oJAAoJ;QACpJ,MAAM,eAAe,GAAG,oBAAQ,CAAC,OAAO,CAAC,MAAM,MAAM,iBAAiB,EAAE,EAAE,CAAC,CAAC;QAE5E,MAAM,UAAU,GAAG,aAAa,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;QAChE,MAAM,SAAS,GAAG;YAChB,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,OAAO;YACb,QAAQ,EAAE,UAAU;YACpB,QAAQ,EAAE,OAAO;SAClB,CAAC;QACF,IAAI,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;YAC3C,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;SAC3C;QACD,eAAe,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACrC;IACD,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC;AA9BD,8DA8BC;AAED,SAAgB,yBAAyB,CAAC,MAAc,EAAE,OAA0B;IAClF,eAAe,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;AACpC,CAAC;AAFD,8DAEC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { ConnectionOptions } from 'mysql2';\nimport { EnvParse } from '@fluidware-it/saddlebag';\nimport * as fs from 'fs';\n\nexport const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('FW_DB_USE_READ_COMMITTED_ISOLATION', false);\n\nfunction getDbPassword(DB_PASSWORD_FILE: string, DB_PASSWORD: string): string {\n if (DB_PASSWORD_FILE) {\n return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');\n }\n return DB_PASSWORD;\n}\n\nconst memoizedOptions: { [prefix: string]: ConnectionOptions } = {};\n\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\n if (!memoizedOptions[prefix]) {\n const DB_PASSWORD = EnvParse.envString(`FW_${prefix}DB_PASSWORD`, '');\n const DB_PASSWORD_FILE = EnvParse.envString(`FW_${prefix}DB_PASSWORD_FILE`, '');\n\n if (!DB_PASSWORD && !DB_PASSWORD_FILE) {\n throw new Error('FW_DB_PASSWORD or FW_DB_PASSWORD_FILE env is required');\n }\n\n const DB_USER = EnvParse.envStringRequired(`FW_${prefix}DB_USER`);\n const DB_HOST = EnvParse.envString(`FW_${prefix}DB_HOST`, 'localhost');\n const DB_PORT = EnvParse.envInt(`FW_${prefix}DB_PORT`, 3306);\n const DB_NAME = EnvParse.envString(`FW_${prefix}DB_NAME`, DB_USER);\n // FW_${prefix}DB_CONN_OPTIONS: JSON string with connection options See https://github.com/mysqljs/mysql#connection-options for all possible options\n const DB_CONN_OPTIONS = EnvParse.envJSON(`FW_${prefix}DB_CONN_OPTIONS`, {});\n\n const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);\n const dbOptions = {\n host: DB_HOST,\n port: DB_PORT,\n user: DB_USER,\n password: dbPassword,\n database: DB_NAME\n };\n if (Object.keys(DB_CONN_OPTIONS).length > 0) {\n Object.assign(dbOptions, DB_CONN_OPTIONS);\n }\n memoizedOptions[prefix] = dbOptions;\n }\n return memoizedOptions[prefix];\n}\n\nexport function setMysqlConnectionOptions(prefix: string, options: ConnectionOptions) {\n memoizedOptions[prefix] = options;\n}\n"]}
|
package/build/src/dbClient.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { ConnectionOptions } from 'mysql2';
|
|
1
2
|
import { Connection, RowDataPacket, ResultSetHeader, ProcedureCallPacket, FieldPacket } from 'mysql2/promise';
|
|
2
|
-
import { ConnectionOptions } from 'mysql2';
|
|
3
3
|
interface ConnectionWrap extends Connection {
|
|
4
4
|
run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(sql: string, phs?: (string | number | boolean | null)[]): Promise<{
|
|
5
5
|
rows: T;
|
|
@@ -21,7 +21,7 @@ export declare class DbClient {
|
|
|
21
21
|
insert(sql: string, phs?: (string | number | boolean | null)[]): Promise<number>;
|
|
22
22
|
update(sql: string, phs?: (string | number | boolean | null)[], returnChangedRows?: boolean): Promise<number>;
|
|
23
23
|
delete(sql: string, phs?: (string | number | boolean | null)[]): Promise<number>;
|
|
24
|
-
run(sql: string, phs?: (string | number | boolean | null)[]): Promise<[import("mysql2
|
|
24
|
+
run(sql: string, phs?: (string | number | boolean | null)[]): Promise<[import("mysql2").QueryResult, FieldPacket[]]>;
|
|
25
25
|
}
|
|
26
26
|
export {};
|
|
27
27
|
//# sourceMappingURL=dbClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;
|
|
1
|
+
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAGH,4CAOwB;AACxB,qCAAmF;AAYnF,MAAa,QAAQ;IAInB,YAAY,yBAAsD;QAChE,IAAI,yBAAyB,EAAE;YAC7B,IAAI,OAAO,yBAAyB,KAAK,QAAQ,EAAE;gBACjD,IAAI,CAAC,iBAAiB,GAAG,IAAA,kCAAyB,EAAC,yBAAyB,CAAC,CA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C,GAAW,EAAE,GAA0C;QAC/D,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;SAC5C;QACD,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC3C,CAAC;CACF;AA5HD,4BA4HC","sourcesContent":["/*\n * Copyright Fluidware srl\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { ConnectionOptions } from 'mysql2';\nimport {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean | null)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private readonly connectionOptions: ConnectionOptions;\n\n constructor(connectionOptionsOrPrefix?: ConnectionOptions | string) {\n if (connectionOptionsOrPrefix) {\n if (typeof connectionOptionsOrPrefix === 'string') {\n this.connectionOptions = getMysqlConnectionOptions(connectionOptionsOrPrefix);\n } else {\n this.connectionOptions = connectionOptionsOrPrefix;\n }\n } else {\n this.connectionOptions = getMysqlConnectionOptions('');\n }\n }\n\n async open() {\n this.connection = (await createConnection(this.connectionOptions)) as ConnectionWrap;\n this.connection.run = async <\n T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket\n >(\n sql: string,\n phs?: (string | number | boolean | null)[]\n ): Promise<{ rows: T; cols: FieldPacket[] }> => {\n if (!this.connection) {\n throw new Error('run() called but no connection available');\n }\n const [rows, cols] = await this.connection.execute<T>(sql, phs);\n return {\n rows,\n cols\n };\n };\n }\n\n getConnection(): ConnectionWrap | undefined {\n return this.connection;\n }\n\n async close() {\n if (this.connection) {\n await this.connection.end();\n }\n }\n\n async startTransaction() {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n if (USE_READ_COMMITTED_ISOLATION) {\n await this.connection.execute(\"SET SESSION tx_isolation='read-committed'\");\n }\n await this.connection.execute('SET AUTOCOMMIT=0');\n }\n\n async commit(closeTransaction = true) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n await this.connection.execute('COMMIT');\n if (closeTransaction) {\n await this.connection.execute('SET AUTOCOMMIT=1');\n }\n }\n\n async rollback(closeTransaction = true) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n\n await this.connection.execute('ROLLBACK');\n if (closeTransaction) {\n await this.connection.execute('SET AUTOCOMMIT=1');\n }\n }\n\n async all(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<RowDataPacket[]>(sql, phs);\n return res.rows;\n }\n\n async get(sql: string, phs?: (string | number | boolean | null)[]): Promise<RowDataPacket | null> {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<RowDataPacket[]>(sql, phs);\n if (res.rows.length > 1) throw new Error('get() returned more than one row');\n if (res.rows.length === 1) return res.rows[0];\n return null;\n }\n\n async insert(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return res.rows.insertId || res.rows.affectedRows;\n }\n\n async update(sql: string, phs?: (string | number | boolean | null)[], returnChangedRows = false) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return returnChangedRows ? res.rows.changedRows : res.rows.affectedRows;\n }\n\n async delete(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n const res = await this.connection.run<ResultSetHeader>(sql, phs);\n return res.rows.affectedRows;\n }\n\n async run(sql: string, phs?: (string | number | boolean | null)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n return this.connection.execute(sql, phs);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidware-it/mysql2-client",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"main": "build/src/index.js",
|
|
5
5
|
"module": "build/esm/index.js",
|
|
6
6
|
"esnext": "build/esnext/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@fluidware-it/saddlebag": "^0.1.1",
|
|
27
|
-
"mysql2": "^3.
|
|
27
|
+
"mysql2": "^3.14.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/jest": "^29.5.6",
|