@fluidware-it/mysql2-client 0.0.2 → 0.1.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.
- package/build/esm/{Config.d.ts → config.d.ts} +1 -1
- package/build/esm/{Config.js → config.js} +20 -25
- package/build/esm/config.js.map +1 -0
- package/build/{src/DbClient.d.ts → esm/dbClient.d.ts} +2 -1
- package/build/esm/{DbClient.js → dbClient.js} +12 -2
- package/build/esm/dbClient.js.map +1 -0
- package/build/esm/index.d.ts +2 -2
- package/build/esm/index.js +2 -2
- package/build/esm/index.js.map +1 -1
- package/build/esnext/{Config.d.ts → config.d.ts} +1 -1
- package/build/esnext/{Config.js → config.js} +20 -25
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/{DbClient.d.ts → dbClient.d.ts} +2 -1
- package/build/esnext/{DbClient.js → dbClient.js} +8 -2
- package/build/{esm/DbClient.js.map → esnext/dbClient.js.map} +1 -1
- package/build/esnext/index.d.ts +2 -2
- package/build/esnext/index.js +2 -2
- package/build/esnext/index.js.map +1 -1
- package/build/src/{Config.d.ts → config.d.ts} +1 -1
- package/build/src/{Config.js → config.js} +20 -25
- package/build/src/config.js.map +1 -0
- package/build/{esm/DbClient.d.ts → src/dbClient.d.ts} +2 -1
- package/build/src/{DbClient.js → dbClient.js} +11 -5
- package/build/src/dbClient.js.map +1 -0
- package/build/src/index.d.ts +2 -2
- package/build/src/index.js +2 -2
- package/build/src/index.js.map +1 -1
- package/package.json +2 -2
- package/build/esm/Config.js.map +0 -1
- package/build/esnext/Config.js.map +0 -1
- package/build/esnext/DbClient.js.map +0 -1
- package/build/src/Config.js.map +0 -1
- package/build/src/DbClient.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ConnectionOptions } from 'mysql2';
|
|
2
2
|
export declare const USE_READ_COMMITTED_ISOLATION: boolean;
|
|
3
3
|
export declare function getMysqlConnectionOptions(prefix?: string): ConnectionOptions;
|
|
4
|
-
//# sourceMappingURL=
|
|
4
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -13,49 +13,44 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { EnvParse } from '@fluidware-it/
|
|
16
|
+
import { EnvParse } from '@fluidware-it/saddlebag';
|
|
17
17
|
import * as fs from 'fs';
|
|
18
|
-
export var USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('
|
|
18
|
+
export var USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('FW_DB_USE_READ_COMMITTED_ISOLATION', false);
|
|
19
19
|
function getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD) {
|
|
20
20
|
if (DB_PASSWORD_FILE) {
|
|
21
21
|
return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
return DB_PASSWORD;
|
|
25
|
-
}
|
|
23
|
+
return DB_PASSWORD;
|
|
26
24
|
}
|
|
27
|
-
var
|
|
25
|
+
var memoizedOptions = {};
|
|
28
26
|
export function getMysqlConnectionOptions(prefix) {
|
|
29
27
|
if (prefix === void 0) { prefix = ''; }
|
|
30
|
-
if (!
|
|
31
|
-
var
|
|
32
|
-
var
|
|
33
|
-
var DB_USER = EnvParse.envStringRequired("".concat(prefix, "DB_USER"));
|
|
34
|
-
var DB_NAME = EnvParse.envString("".concat(prefix, "DB_NAME"), DB_USER);
|
|
35
|
-
var DB_CONN_OPTIONS = EnvParse.envJSON("".concat(prefix, "DB_NAME"), {});
|
|
36
|
-
var DB_PASSWORD = EnvParse.envString("".concat(prefix, "DB_PASSWORD"), '');
|
|
37
|
-
var DB_PASSWORD_FILE = EnvParse.envString("".concat(prefix, "DB_PASSWORD_FILE"), '');
|
|
28
|
+
if (!memoizedOptions[prefix]) {
|
|
29
|
+
var DB_PASSWORD = EnvParse.envString("FW_".concat(prefix, "DB_PASSWORD"), '');
|
|
30
|
+
var DB_PASSWORD_FILE = EnvParse.envString("FW_".concat(prefix, "DB_PASSWORD_FILE"), '');
|
|
38
31
|
if (!DB_PASSWORD && !DB_PASSWORD_FILE) {
|
|
39
|
-
throw new Error('
|
|
32
|
+
throw new Error('FW_DB_PASSWORD or FW_DB_PASSWORD_FILE env is required');
|
|
40
33
|
}
|
|
34
|
+
var DB_USER = EnvParse.envStringRequired("FW_".concat(prefix, "DB_USER"));
|
|
35
|
+
var DB_HOST = EnvParse.envString("FW_".concat(prefix, "DB_HOST"), 'localhost');
|
|
36
|
+
var DB_PORT = EnvParse.envInt("FW_".concat(prefix, "DB_PORT"), 3306);
|
|
37
|
+
var DB_NAME = EnvParse.envString("FW_".concat(prefix, "DB_NAME"), DB_USER);
|
|
38
|
+
var DB_CONN_OPTIONS = EnvParse.envJSON("FW_".concat(prefix, "DB_NAME"), {});
|
|
41
39
|
var dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);
|
|
42
|
-
var dbName = DB_NAME || DB_USER;
|
|
43
|
-
var dbHost = DB_HOST || '127.0.0.1';
|
|
44
|
-
var dbPort = Number(DB_PORT) || 3306;
|
|
45
40
|
var dbOptions = {
|
|
46
|
-
host:
|
|
47
|
-
port:
|
|
41
|
+
host: DB_HOST,
|
|
42
|
+
port: DB_PORT,
|
|
48
43
|
user: DB_USER,
|
|
49
44
|
password: dbPassword,
|
|
50
|
-
database:
|
|
45
|
+
database: DB_NAME
|
|
51
46
|
};
|
|
52
|
-
if (DB_CONN_OPTIONS) {
|
|
47
|
+
if (Object.keys(DB_CONN_OPTIONS).length > 0) {
|
|
53
48
|
// See https://github.com/mysqljs/mysql#connection-options for all possible options
|
|
54
49
|
var customOptions = JSON.parse(DB_CONN_OPTIONS);
|
|
55
50
|
Object.assign(dbOptions, customOptions);
|
|
56
51
|
}
|
|
57
|
-
|
|
52
|
+
memoizedOptions[prefix] = dbOptions;
|
|
58
53
|
}
|
|
59
|
-
return
|
|
54
|
+
return memoizedOptions[prefix];
|
|
60
55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AAM,MAAM,YAAS,EAAE,IAAI,CAAC,CAAC;QAC7D,IAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,aAAM,MAAM,YAAS,EAAE,OAAO,CAAC,CAAC;QACnE,IAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,aAAM,MAAM,YAAS,EAAE,EAAE,CAAC,CAAC;QAEpE,IAAM,UAAU,GAAG,aAAa,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;QAChE,IAAM,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,mFAAmF;YACnF,IAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;SACzC;QACD,eAAe,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACrC;IACD,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AACjC,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 { EnvParse } from '@fluidware-it/saddlebag';\nimport * as fs from 'fs';\nimport { ConnectionOptions } from 'mysql2';\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 const DB_CONN_OPTIONS = EnvParse.envJSON(`FW_${prefix}DB_NAME`, {});\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 // See https://github.com/mysqljs/mysql#connection-options for all possible options\n const customOptions = JSON.parse(DB_CONN_OPTIONS);\n Object.assign(dbOptions, customOptions);\n }\n memoizedOptions[prefix] = dbOptions;\n }\n return memoizedOptions[prefix];\n}\n"]}
|
|
@@ -21,6 +21,7 @@ export declare class DbClient {
|
|
|
21
21
|
insert(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
22
22
|
update(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
23
23
|
delete(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
24
|
+
run(sql: string, phs?: (string | number | boolean)[]): Promise<[import("mysql2/typings/mysql/lib/protocol/packets/OkPacket").OkPacket | RowDataPacket[] | ProcedureCallPacket | ResultSetHeader[] | RowDataPacket[][] | import("mysql2/typings/mysql/lib/protocol/packets/OkPacket").OkPacket[], FieldPacket[]]>;
|
|
24
25
|
}
|
|
25
26
|
export {};
|
|
26
|
-
//# sourceMappingURL=
|
|
27
|
+
//# sourceMappingURL=dbClient.d.ts.map
|
|
@@ -66,7 +66,7 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
66
66
|
return ar;
|
|
67
67
|
};
|
|
68
68
|
import { createConnection } from 'mysql2/promise';
|
|
69
|
-
import { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './
|
|
69
|
+
import { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';
|
|
70
70
|
var DbClient = /** @class */ (function () {
|
|
71
71
|
function DbClient(connectionOptionsOrPrefix) {
|
|
72
72
|
if (connectionOptionsOrPrefix) {
|
|
@@ -285,7 +285,17 @@ var DbClient = /** @class */ (function () {
|
|
|
285
285
|
});
|
|
286
286
|
});
|
|
287
287
|
};
|
|
288
|
+
DbClient.prototype.run = function (sql, phs) {
|
|
289
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
290
|
+
return __generator(this, function (_a) {
|
|
291
|
+
if (!this.connection) {
|
|
292
|
+
throw new Error('no connection available');
|
|
293
|
+
}
|
|
294
|
+
return [2 /*return*/, this.connection.execute(sql, phs)];
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
};
|
|
288
298
|
return DbClient;
|
|
289
299
|
}());
|
|
290
300
|
export { DbClient };
|
|
291
|
-
//# sourceMappingURL=
|
|
301
|
+
//# sourceMappingURL=dbClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;;;;;;;;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AAyB,CAAC,CAAC;yBAC5C;wBACW,qBAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAkB,GAAG,EAAE,GAAG,CAAC,EAAA;;wBAA1D,GAAG,GAAG,SAAoD;wBAChE,sBAAO,GAAG,CAAC,IAAI,CAAC,YAAY,EAAC;;;;KAC9B;IAEK,yBAAM,GAAZ,UAAa,GAAW,EAAE,GAAmC;;;;;;wBAC3D,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;4BACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;yBAC5C;wBACW,qBAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAkB,GAAG,EAAE,GAAG,CAAC,EAAA;;wBAA1D,GAAG,GAAG,SAAoD;wBAChE,sBAAO,GAAG,CAAC,IAAI,CAAC,YAAY,EAAC;;;;KAC9B;IAEK,sBAAG,GAAT,UAAU,GAAW,EAAE,GAAmC;;;gBACxD,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,AA3HD,IA2HC","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 {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';\nimport { ConnectionOptions } from 'mysql2';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private 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)[]\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)[]) {\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)[]) {\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 return res.rows[0];\n }\n\n async insert(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\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 delete(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\n if (!this.connection) {\n throw new Error('no connection available');\n }\n return this.connection.execute(sql, phs);\n }\n}\n"]}
|
package/build/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './config';
|
|
2
|
+
export * from './dbClient';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/build/esm/index.js
CHANGED
|
@@ -13,6 +13,6 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
export * from './
|
|
17
|
-
export * from './
|
|
16
|
+
export * from './config';
|
|
17
|
+
export * from './dbClient';
|
|
18
18
|
//# sourceMappingURL=index.js.map
|
package/build/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,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\nexport * from './
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,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\nexport * from './config';\nexport * from './dbClient';\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ConnectionOptions } from 'mysql2';
|
|
2
2
|
export declare const USE_READ_COMMITTED_ISOLATION: boolean;
|
|
3
3
|
export declare function getMysqlConnectionOptions(prefix?: string): ConnectionOptions;
|
|
4
|
-
//# sourceMappingURL=
|
|
4
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -13,48 +13,43 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { EnvParse } from '@fluidware-it/
|
|
16
|
+
import { EnvParse } from '@fluidware-it/saddlebag';
|
|
17
17
|
import * as fs from 'fs';
|
|
18
|
-
export const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('
|
|
18
|
+
export const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('FW_DB_USE_READ_COMMITTED_ISOLATION', false);
|
|
19
19
|
function getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD) {
|
|
20
20
|
if (DB_PASSWORD_FILE) {
|
|
21
21
|
return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
return DB_PASSWORD;
|
|
25
|
-
}
|
|
23
|
+
return DB_PASSWORD;
|
|
26
24
|
}
|
|
27
|
-
const
|
|
25
|
+
const memoizedOptions = {};
|
|
28
26
|
export function getMysqlConnectionOptions(prefix = '') {
|
|
29
|
-
if (!
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const DB_USER = EnvParse.envStringRequired(`${prefix}DB_USER`);
|
|
33
|
-
const DB_NAME = EnvParse.envString(`${prefix}DB_NAME`, DB_USER);
|
|
34
|
-
const DB_CONN_OPTIONS = EnvParse.envJSON(`${prefix}DB_NAME`, {});
|
|
35
|
-
const DB_PASSWORD = EnvParse.envString(`${prefix}DB_PASSWORD`, '');
|
|
36
|
-
const DB_PASSWORD_FILE = EnvParse.envString(`${prefix}DB_PASSWORD_FILE`, '');
|
|
27
|
+
if (!memoizedOptions[prefix]) {
|
|
28
|
+
const DB_PASSWORD = EnvParse.envString(`FW_${prefix}DB_PASSWORD`, '');
|
|
29
|
+
const DB_PASSWORD_FILE = EnvParse.envString(`FW_${prefix}DB_PASSWORD_FILE`, '');
|
|
37
30
|
if (!DB_PASSWORD && !DB_PASSWORD_FILE) {
|
|
38
|
-
throw new Error('
|
|
31
|
+
throw new Error('FW_DB_PASSWORD or FW_DB_PASSWORD_FILE env is required');
|
|
39
32
|
}
|
|
33
|
+
const DB_USER = EnvParse.envStringRequired(`FW_${prefix}DB_USER`);
|
|
34
|
+
const DB_HOST = EnvParse.envString(`FW_${prefix}DB_HOST`, 'localhost');
|
|
35
|
+
const DB_PORT = EnvParse.envInt(`FW_${prefix}DB_PORT`, 3306);
|
|
36
|
+
const DB_NAME = EnvParse.envString(`FW_${prefix}DB_NAME`, DB_USER);
|
|
37
|
+
const DB_CONN_OPTIONS = EnvParse.envJSON(`FW_${prefix}DB_NAME`, {});
|
|
40
38
|
const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);
|
|
41
|
-
const dbName = DB_NAME || DB_USER;
|
|
42
|
-
const dbHost = DB_HOST || '127.0.0.1';
|
|
43
|
-
const dbPort = Number(DB_PORT) || 3306;
|
|
44
39
|
const dbOptions = {
|
|
45
|
-
host:
|
|
46
|
-
port:
|
|
40
|
+
host: DB_HOST,
|
|
41
|
+
port: DB_PORT,
|
|
47
42
|
user: DB_USER,
|
|
48
43
|
password: dbPassword,
|
|
49
|
-
database:
|
|
44
|
+
database: DB_NAME
|
|
50
45
|
};
|
|
51
|
-
if (DB_CONN_OPTIONS) {
|
|
46
|
+
if (Object.keys(DB_CONN_OPTIONS).length > 0) {
|
|
52
47
|
// See https://github.com/mysqljs/mysql#connection-options for all possible options
|
|
53
48
|
const customOptions = JSON.parse(DB_CONN_OPTIONS);
|
|
54
49
|
Object.assign(dbOptions, customOptions);
|
|
55
50
|
}
|
|
56
|
-
|
|
51
|
+
memoizedOptions[prefix] = dbOptions;
|
|
57
52
|
}
|
|
58
|
-
return
|
|
53
|
+
return memoizedOptions[prefix];
|
|
59
54
|
}
|
|
60
|
-
//# sourceMappingURL=
|
|
55
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,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AEpE,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,mFAAmF;YACnF,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;SACzC;QACD,eAAe,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACrC;IACD,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AACjC,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 { EnvParse } from '@fluidware-it/saddlebag';\nimport * as fs from 'fs';\nimport { ConnectionOptions } from 'mysql2';\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 const DB_CONN_OPTIONS = EnvParse.envJSON(`FW_${prefix}DB_NAME`, {});\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 // See https://github.com/mysqljs/mysql#connection-options for all possible options\n const customOptions = JSON.parse(DB_CONN_OPTIONS);\n Object.assign(dbOptions, customOptions);\n }\n memoizedOptions[prefix] = dbOptions;\n }\n return memoizedOptions[prefix];\n}\n"]}
|
|
@@ -21,6 +21,7 @@ export declare class DbClient {
|
|
|
21
21
|
insert(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
22
22
|
update(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
23
23
|
delete(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
24
|
+
run(sql: string, phs?: (string | number | boolean)[]): Promise<[import("mysql2/typings/mysql/lib/protocol/packets/OkPacket").OkPacket | RowDataPacket[] | ProcedureCallPacket | ResultSetHeader[] | RowDataPacket[][] | import("mysql2/typings/mysql/lib/protocol/packets/OkPacket").OkPacket[], FieldPacket[]]>;
|
|
24
25
|
}
|
|
25
26
|
export {};
|
|
26
|
-
//# sourceMappingURL=
|
|
27
|
+
//# sourceMappingURL=dbClient.d.ts.map
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import { createConnection } from 'mysql2/promise';
|
|
17
|
-
import { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './
|
|
17
|
+
import { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';
|
|
18
18
|
export class DbClient {
|
|
19
19
|
constructor(connectionOptionsOrPrefix) {
|
|
20
20
|
if (connectionOptionsOrPrefix) {
|
|
@@ -114,5 +114,11 @@ export class DbClient {
|
|
|
114
114
|
const res = await this.connection.run(sql, phs);
|
|
115
115
|
return res.rows.affectedRows;
|
|
116
116
|
}
|
|
117
|
+
async run(sql, phs) {
|
|
118
|
+
if (!this.connection) {
|
|
119
|
+
throw new Error('no connection available');
|
|
120
|
+
}
|
|
121
|
+
return this.connection.execute(sql, phs);
|
|
122
|
+
}
|
|
117
123
|
}
|
|
118
|
-
//# sourceMappingURL=
|
|
124
|
+
//# sourceMappingURL=dbClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,gBAAgB,EAMjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAanF,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,CAA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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 {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';\nimport { ConnectionOptions } from 'mysql2';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private 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)[]\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)[]) {\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)[]) {\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 return res.rows[0];\n }\n\n async insert(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\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 delete(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\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/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './config';
|
|
2
|
+
export * from './dbClient';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/build/esnext/index.js
CHANGED
|
@@ -13,6 +13,6 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
export * from './
|
|
17
|
-
export * from './
|
|
16
|
+
export * from './config';
|
|
17
|
+
export * from './dbClient';
|
|
18
18
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,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\nexport * from './
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,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\nexport * from './config';\nexport * from './dbClient';\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ConnectionOptions } from 'mysql2';
|
|
2
2
|
export declare const USE_READ_COMMITTED_ISOLATION: boolean;
|
|
3
3
|
export declare function getMysqlConnectionOptions(prefix?: string): ConnectionOptions;
|
|
4
|
-
//# sourceMappingURL=
|
|
4
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -16,49 +16,44 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.getMysqlConnectionOptions = exports.USE_READ_COMMITTED_ISOLATION = void 0;
|
|
19
|
-
const
|
|
19
|
+
const saddlebag_1 = require("@fluidware-it/saddlebag");
|
|
20
20
|
const fs = require("fs");
|
|
21
|
-
exports.USE_READ_COMMITTED_ISOLATION =
|
|
21
|
+
exports.USE_READ_COMMITTED_ISOLATION = saddlebag_1.EnvParse.envBool('FW_DB_USE_READ_COMMITTED_ISOLATION', false);
|
|
22
22
|
function getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD) {
|
|
23
23
|
if (DB_PASSWORD_FILE) {
|
|
24
24
|
return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
return DB_PASSWORD;
|
|
28
|
-
}
|
|
26
|
+
return DB_PASSWORD;
|
|
29
27
|
}
|
|
30
|
-
const
|
|
28
|
+
const memoizedOptions = {};
|
|
31
29
|
function getMysqlConnectionOptions(prefix = '') {
|
|
32
|
-
if (!
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const DB_USER = node_utils_1.EnvParse.envStringRequired(`${prefix}DB_USER`);
|
|
36
|
-
const DB_NAME = node_utils_1.EnvParse.envString(`${prefix}DB_NAME`, DB_USER);
|
|
37
|
-
const DB_CONN_OPTIONS = node_utils_1.EnvParse.envJSON(`${prefix}DB_NAME`, {});
|
|
38
|
-
const DB_PASSWORD = node_utils_1.EnvParse.envString(`${prefix}DB_PASSWORD`, '');
|
|
39
|
-
const DB_PASSWORD_FILE = node_utils_1.EnvParse.envString(`${prefix}DB_PASSWORD_FILE`, '');
|
|
30
|
+
if (!memoizedOptions[prefix]) {
|
|
31
|
+
const DB_PASSWORD = saddlebag_1.EnvParse.envString(`FW_${prefix}DB_PASSWORD`, '');
|
|
32
|
+
const DB_PASSWORD_FILE = saddlebag_1.EnvParse.envString(`FW_${prefix}DB_PASSWORD_FILE`, '');
|
|
40
33
|
if (!DB_PASSWORD && !DB_PASSWORD_FILE) {
|
|
41
|
-
throw new Error('
|
|
34
|
+
throw new Error('FW_DB_PASSWORD or FW_DB_PASSWORD_FILE env is required');
|
|
42
35
|
}
|
|
36
|
+
const DB_USER = saddlebag_1.EnvParse.envStringRequired(`FW_${prefix}DB_USER`);
|
|
37
|
+
const DB_HOST = saddlebag_1.EnvParse.envString(`FW_${prefix}DB_HOST`, 'localhost');
|
|
38
|
+
const DB_PORT = saddlebag_1.EnvParse.envInt(`FW_${prefix}DB_PORT`, 3306);
|
|
39
|
+
const DB_NAME = saddlebag_1.EnvParse.envString(`FW_${prefix}DB_NAME`, DB_USER);
|
|
40
|
+
const DB_CONN_OPTIONS = saddlebag_1.EnvParse.envJSON(`FW_${prefix}DB_NAME`, {});
|
|
43
41
|
const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);
|
|
44
|
-
const dbName = DB_NAME || DB_USER;
|
|
45
|
-
const dbHost = DB_HOST || '127.0.0.1';
|
|
46
|
-
const dbPort = Number(DB_PORT) || 3306;
|
|
47
42
|
const dbOptions = {
|
|
48
|
-
host:
|
|
49
|
-
port:
|
|
43
|
+
host: DB_HOST,
|
|
44
|
+
port: DB_PORT,
|
|
50
45
|
user: DB_USER,
|
|
51
46
|
password: dbPassword,
|
|
52
|
-
database:
|
|
47
|
+
database: DB_NAME
|
|
53
48
|
};
|
|
54
|
-
if (DB_CONN_OPTIONS) {
|
|
49
|
+
if (Object.keys(DB_CONN_OPTIONS).length > 0) {
|
|
55
50
|
// See https://github.com/mysqljs/mysql#connection-options for all possible options
|
|
56
51
|
const customOptions = JSON.parse(DB_CONN_OPTIONS);
|
|
57
52
|
Object.assign(dbOptions, customOptions);
|
|
58
53
|
}
|
|
59
|
-
|
|
54
|
+
memoizedOptions[prefix] = dbOptions;
|
|
60
55
|
}
|
|
61
|
-
return
|
|
56
|
+
return memoizedOptions[prefix];
|
|
62
57
|
}
|
|
63
58
|
exports.getMysqlConnectionOptions = getMysqlConnectionOptions;
|
|
64
|
-
//# sourceMappingURL=
|
|
59
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,uDAAmD;AACnD,yBAAyB;AAGZ,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,MAAM,eAAe,GAAG,oBAAQ,CAAC,OAAO,CAAC,MAAM,MAAM,SAAS,EAAE,EAAE,CAAC,CAAC;QAEpE,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,mFAAmF;YACnF,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;SACzC;QACD,eAAe,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACrC;IACD,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC;AA/BD,8DA+BC","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 { EnvParse } from '@fluidware-it/saddlebag';\nimport * as fs from 'fs';\nimport { ConnectionOptions } from 'mysql2';\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 const DB_CONN_OPTIONS = EnvParse.envJSON(`FW_${prefix}DB_NAME`, {});\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 // See https://github.com/mysqljs/mysql#connection-options for all possible options\n const customOptions = JSON.parse(DB_CONN_OPTIONS);\n Object.assign(dbOptions, customOptions);\n }\n memoizedOptions[prefix] = dbOptions;\n }\n return memoizedOptions[prefix];\n}\n"]}
|
|
@@ -21,6 +21,7 @@ export declare class DbClient {
|
|
|
21
21
|
insert(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
22
22
|
update(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
23
23
|
delete(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
24
|
+
run(sql: string, phs?: (string | number | boolean)[]): Promise<[import("mysql2/typings/mysql/lib/protocol/packets/OkPacket").OkPacket | RowDataPacket[] | ProcedureCallPacket | ResultSetHeader[] | RowDataPacket[][] | import("mysql2/typings/mysql/lib/protocol/packets/OkPacket").OkPacket[], FieldPacket[]]>;
|
|
24
25
|
}
|
|
25
26
|
export {};
|
|
26
|
-
//# sourceMappingURL=
|
|
27
|
+
//# sourceMappingURL=dbClient.d.ts.map
|
|
@@ -17,19 +17,19 @@
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.DbClient = void 0;
|
|
19
19
|
const promise_1 = require("mysql2/promise");
|
|
20
|
-
const
|
|
20
|
+
const config_1 = require("./config");
|
|
21
21
|
class DbClient {
|
|
22
22
|
constructor(connectionOptionsOrPrefix) {
|
|
23
23
|
if (connectionOptionsOrPrefix) {
|
|
24
24
|
if (typeof connectionOptionsOrPrefix === 'string') {
|
|
25
|
-
this.connectionOptions = (0,
|
|
25
|
+
this.connectionOptions = (0, config_1.getMysqlConnectionOptions)(connectionOptionsOrPrefix);
|
|
26
26
|
}
|
|
27
27
|
else {
|
|
28
28
|
this.connectionOptions = connectionOptionsOrPrefix;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
32
|
-
this.connectionOptions = (0,
|
|
32
|
+
this.connectionOptions = (0, config_1.getMysqlConnectionOptions)('');
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
async open() {
|
|
@@ -57,7 +57,7 @@ class DbClient {
|
|
|
57
57
|
if (!this.connection) {
|
|
58
58
|
throw new Error('no connection available');
|
|
59
59
|
}
|
|
60
|
-
if (
|
|
60
|
+
if (config_1.USE_READ_COMMITTED_ISOLATION) {
|
|
61
61
|
await this.connection.execute("SET SESSION tx_isolation='read-committed'");
|
|
62
62
|
}
|
|
63
63
|
await this.connection.execute('SET AUTOCOMMIT=0');
|
|
@@ -117,6 +117,12 @@ class DbClient {
|
|
|
117
117
|
const res = await this.connection.run(sql, phs);
|
|
118
118
|
return res.rows.affectedRows;
|
|
119
119
|
}
|
|
120
|
+
async run(sql, phs) {
|
|
121
|
+
if (!this.connection) {
|
|
122
|
+
throw new Error('no connection available');
|
|
123
|
+
}
|
|
124
|
+
return this.connection.execute(sql, phs);
|
|
125
|
+
}
|
|
120
126
|
}
|
|
121
127
|
exports.DbClient = DbClient;
|
|
122
|
-
//# sourceMappingURL=
|
|
128
|
+
//# sourceMappingURL=dbClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dbClient.js","sourceRoot":"","sources":["../../src/dbClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4CAOwB;AACxB,qCAAmF;AAanF,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,CAAC;aAC/E;iBAAM;gBACL,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC;aACpD;SACF;aAAM;YACL,IAAI,CAAC,iBAAiB,GAAG,IAAA,kCAAyB,EAAC,EAAE,CAAC,CAAC;SACxD;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,UAAU,GAAG,CAAC,MAAM,IAAA,0BAAgB,EAAC,IAAI,CAAC,iBAAiB,CAAC,CAAmB,CAAC;QACrF,IAAI,CAAC,UAAU,CAAC,GAAG,GAAG,KAAK,EAG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I,CAAC,QAAQ,IAAI,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC;IACpD,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAW,EAAE,GAAmC;QAC3D,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,MAAM,CAAC,GAAW,EAAE,GAAmC;QAC3D,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,GAAmC;QACxD,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;AA3HD,4BA2HC","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 {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './config';\nimport { ConnectionOptions } from 'mysql2';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private 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)[]\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)[]) {\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)[]) {\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 return res.rows[0];\n }\n\n async insert(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\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 delete(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\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/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './config';
|
|
2
|
+
export * from './dbClient';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/build/src/index.js
CHANGED
|
@@ -29,6 +29,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
29
29
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
30
30
|
};
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
__exportStar(require("./
|
|
33
|
-
__exportStar(require("./
|
|
32
|
+
__exportStar(require("./config"), exports);
|
|
33
|
+
__exportStar(require("./dbClient"), exports);
|
|
34
34
|
//# sourceMappingURL=index.js.map
|
package/build/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;;;;;AAEH,2CAAyB;AACzB,6CAA2B","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\nexport * from './
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;;;;;AAEH,2CAAyB;AACzB,6CAA2B","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\nexport * from './config';\nexport * from './dbClient';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidware-it/mysql2-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"main": "build/src/index.js",
|
|
5
5
|
"module": "build/esm/index.js",
|
|
6
6
|
"esnext": "build/esnext/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"node": ">=18.4"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@fluidware-it/
|
|
25
|
+
"@fluidware-it/saddlebag": "^0.1.1",
|
|
26
26
|
"mysql2": "^3.6.2"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
package/build/esm/Config.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Config.js","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AAGzB,MAAM,CAAC,IAAM,4BAA4B,GAAG,QAAQ,CAAC,OAAO,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;AAEpG,SAAS,aAAa,CAAC,gBAAwB,EAAE,WAAmB;IAClE,IAAI,gBAAgB,EAAE;QACpB,OAAO,EAAE,CAAC,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;KAClD;SAAM;QACL,OAAO,WAAW,CAAC;KACpB;AACH,CAAC;AAED,IAAM,cAAc,GAA4C,EAAE,CAAC;AAEnE,MAAM,UAAU,yBAAyB,CAAC,MAAW;IAAX,uBAAA,EAAA,WAAW;IACnD,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE;QAC3B,IAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,UAAG,MAAM,YAAS,EAAE,WAAW,CAAC,CAAC;QACpE,IAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,UAAG,MAA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AAM,CAAC,CAAC;AAChC,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 { EnvParse } from '@fluidware-it/node-utils';\nimport * as fs from 'fs';\nimport { ConnectionOptions } from 'mysql2';\n\nexport const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('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 } else {\n return DB_PASSWORD;\n }\n}\n\nconst memoizeOptions: { [prefix: string]: ConnectionOptions } = {};\n\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\n if (!memoizeOptions[prefix]) {\n const DB_HOST = EnvParse.envString(`${prefix}DB_HOST`, 'localhost');\n const DB_PORT = EnvParse.envInt(`${prefix}DB_PORT`, 3306);\n const DB_USER = EnvParse.envStringRequired(`${prefix}DB_USER`);\n const DB_NAME = EnvParse.envString(`${prefix}DB_NAME`, DB_USER);\n const DB_CONN_OPTIONS = EnvParse.envJSON(`${prefix}DB_NAME`, {});\n const DB_PASSWORD = EnvParse.envString(`${prefix}DB_PASSWORD`, '');\n const DB_PASSWORD_FILE = EnvParse.envString(`${prefix}DB_PASSWORD_FILE`, '');\n\n if (!DB_PASSWORD && !DB_PASSWORD_FILE) {\n throw new Error('DB_PASSWORD or DB_PASSWORD_FILE env is required');\n }\n const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);\n const dbName = DB_NAME || DB_USER;\n const dbHost = DB_HOST || '127.0.0.1';\n const dbPort = Number(DB_PORT) || 3306;\n\n const dbOptions = {\n host: dbHost,\n port: dbPort,\n user: DB_USER,\n password: dbPassword,\n database: dbName\n };\n if (DB_CONN_OPTIONS) {\n // See https://github.com/mysqljs/mysql#connection-options for all possible options\n const customOptions = JSON.parse(DB_CONN_OPTIONS);\n Object.assign(dbOptions, customOptions);\n }\n memoizeOptions[prefix] = dbOptions;\n }\n return memoizeOptions[prefix];\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Config.js","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AAGzB,MAAM,CAAC,MAAM,4BAA4B,GAAG,QAAQ,CAAC,OAAO,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;AAEpG,SAAS,aAAa,CAAC,gBAAwB,EAAE,WAAmB;IAClE,IAAI,gBAAgB,EAAE;QACpB,OAAO,EAAE,CAAC,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;KAClD;SAAM;QACL,OAAO,WAAW,CAAC;KACpB;AACH,CAAC;AAED,MAAM,cAAc,GAA4C,EAAE,CAAC;AAEnE,MAAM,UAAU,yBAAyB,CAAC,MAAM,GAAG,EAAE;IACnD,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE;QAC3B,MAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,SAAS,EAAE,WAAW,CAAC,CAAC;QACpE,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,SAAS,EAAE,IAAI,CAAC,CAAC;QAC1D,MAAM,OAAO,GAAG,QAAQ,CAAC,iBAAiB,CAAC,GAAG,MAAM,SAAS,CAAC,CAAC;QAC/D,MAAM,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,SAAS,EAAE,OAAO,CAAC,CAAC;QAChE,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,MAAM,SAAS,EAAE,EAAE,CAAC,CAAC;QACjE,MAAM,WAAW,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,aAAa,EAAE,EAAE,CAAC,CAAC;QACnE,MAAM,gBAAgB,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,kBAAkB,EAAE,EAAE,CAAC,CAAC;QAE7E,IAAI,CAAC,WAAW,IAAI,CAAC,gBAAgB,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;SACpE;QACD,MAAM,UAAU,GAAG,aAAa,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;QAChE,MAAM,MAAM,GAAG,OAAO,IAAI,OAAO,CAAC;QAClC,MAAM,MAAM,GAAG,OAAO,IAAI,WAAW,CAAC;QACtC,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;QAEvC,MAAM,SAAS,GAAG;YAChB,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,OAAO;YACb,QAAQ,EAAE,UAAU;YACpB,QAAQ,EAAE,MAAM;SACjB,CAAC;QACF,IAAI,eAAe,EAAE;YACnB,mFAAmF;YACnF,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;SACzC;QACD,cAAc,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACpC;IACD,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;AAChC,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 { EnvParse } from '@fluidware-it/node-utils';\nimport * as fs from 'fs';\nimport { ConnectionOptions } from 'mysql2';\n\nexport const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('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 } else {\n return DB_PASSWORD;\n }\n}\n\nconst memoizeOptions: { [prefix: string]: ConnectionOptions } = {};\n\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\n if (!memoizeOptions[prefix]) {\n const DB_HOST = EnvParse.envString(`${prefix}DB_HOST`, 'localhost');\n const DB_PORT = EnvParse.envInt(`${prefix}DB_PORT`, 3306);\n const DB_USER = EnvParse.envStringRequired(`${prefix}DB_USER`);\n const DB_NAME = EnvParse.envString(`${prefix}DB_NAME`, DB_USER);\n const DB_CONN_OPTIONS = EnvParse.envJSON(`${prefix}DB_NAME`, {});\n const DB_PASSWORD = EnvParse.envString(`${prefix}DB_PASSWORD`, '');\n const DB_PASSWORD_FILE = EnvParse.envString(`${prefix}DB_PASSWORD_FILE`, '');\n\n if (!DB_PASSWORD && !DB_PASSWORD_FILE) {\n throw new Error('DB_PASSWORD or DB_PASSWORD_FILE env is required');\n }\n const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);\n const dbName = DB_NAME || DB_USER;\n const dbHost = DB_HOST || '127.0.0.1';\n const dbPort = Number(DB_PORT) || 3306;\n\n const dbOptions = {\n host: dbHost,\n port: dbPort,\n user: DB_USER,\n password: dbPassword,\n database: dbName\n };\n if (DB_CONN_OPTIONS) {\n // See https://github.com/mysqljs/mysql#connection-options for all possible options\n const customOptions = JSON.parse(DB_CONN_OPTIONS);\n Object.assign(dbOptions, customOptions);\n }\n memoizeOptions[prefix] = dbOptions;\n }\n return memoizeOptions[prefix];\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DbClient.js","sourceRoot":"","sources":["../../src/DbClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,gBAAgB,EAMjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAanF,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,GAAmC,EACQ,EAAE;YAC7C,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;gBACpB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;aAC7D;YACD,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,CAAI,GAAG,EAAE,GAAG,CAAC,CAAC;YAChE,OAAO;gBACL,IAAI;gBACJ,IAAI;aACL,CAAC;QACJ,CAAC,CAAC;IACJ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,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAW,EAAE,GAAmC;QAC3D,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;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 {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './Config';\nimport { ConnectionOptions } from 'mysql2';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private 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)[]\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)[]) {\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)[]) {\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 return res.rows[0];\n }\n\n async insert(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\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 delete(sql: string, phs?: (string | number | boolean)[]) {\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"]}
|
package/build/src/Config.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Config.js","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,yDAAoD;AACpD,yBAAyB;AAGZ,QAAA,4BAA4B,GAAG,qBAAQ,CAAC,OAAO,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;AAEpG,SAAS,aAAa,CAAC,gBAAwB,EAAE,WAAmB;IAClE,IAAI,gBAAgB,EAAE;QACpB,OAAO,EAAE,CAAC,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;KAClD;SAAM;QACL,OAAO,WAAW,CAAC;KACpB;AACH,CAAC;AAED,MAAM,cAAc,GAA4C,EAAE,CAAC;AAEnE,SAAgB,yBAAyB,CAAC,MAAM,GAAG,EAAE;IACnD,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE;QAC3B,MAAM,OAAO,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,SAAS,EAAE,WAAW,CAAC,CAAC;QACpE,MAAM,OAAO,GAAG,qBAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,SAAS,EAAE,IAAI,CAAC,CAAC;QAC1D,MAAM,OAAO,GAAG,qBAAQ,CAAC,iBAAiB,CAAC,GAAG,MAAM,SAAS,CAAC,CAAC;QAC/D,MAAM,OAAO,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,SAAS,EAAE,OAAO,CAAC,CAAC;QAChE,MAAM,eAAe,GAAG,qBAAQ,CAAC,OAAO,CAAC,GAAG,MAAM,SAAS,EAAE,EAAE,CAAC,CAAC;QACjE,MAAM,WAAW,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,aAAa,EAAE,EAAE,CAAC,CAAC;QACnE,MAAM,gBAAgB,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,kBAAkB,EAAE,EAAE,CAAC,CAAC;QAE7E,IAAI,CAAC,WAAW,IAAI,CAAC,gBAAgB,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;SACpE;QACD,MAAM,UAAU,GAAG,aAAa,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;QAChE,MAAM,MAAM,GAAG,OAAO,IAAI,OAAO,CAAC;QAClC,MAAM,MAAM,GAAG,OAAO,IAAI,WAAW,CAAC;QACtC,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;QAEvC,MAAM,SAAS,GAAG;YAChB,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,MAAM;YACZ,IAAI,EAAE,OAAO;YACb,QAAQ,EAAE,UAAU;YACpB,QAAQ,EAAE,MAAM;SACjB,CAAC;QACF,IAAI,eAAe,EAAE;YACnB,mFAAmF;YACnF,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;SACzC;QACD,cAAc,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;KACpC;IACD,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC;AAjCD,8DAiCC","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 { EnvParse } from '@fluidware-it/node-utils';\nimport * as fs from 'fs';\nimport { ConnectionOptions } from 'mysql2';\n\nexport const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('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 } else {\n return DB_PASSWORD;\n }\n}\n\nconst memoizeOptions: { [prefix: string]: ConnectionOptions } = {};\n\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\n if (!memoizeOptions[prefix]) {\n const DB_HOST = EnvParse.envString(`${prefix}DB_HOST`, 'localhost');\n const DB_PORT = EnvParse.envInt(`${prefix}DB_PORT`, 3306);\n const DB_USER = EnvParse.envStringRequired(`${prefix}DB_USER`);\n const DB_NAME = EnvParse.envString(`${prefix}DB_NAME`, DB_USER);\n const DB_CONN_OPTIONS = EnvParse.envJSON(`${prefix}DB_NAME`, {});\n const DB_PASSWORD = EnvParse.envString(`${prefix}DB_PASSWORD`, '');\n const DB_PASSWORD_FILE = EnvParse.envString(`${prefix}DB_PASSWORD_FILE`, '');\n\n if (!DB_PASSWORD && !DB_PASSWORD_FILE) {\n throw new Error('DB_PASSWORD or DB_PASSWORD_FILE env is required');\n }\n const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);\n const dbName = DB_NAME || DB_USER;\n const dbHost = DB_HOST || '127.0.0.1';\n const dbPort = Number(DB_PORT) || 3306;\n\n const dbOptions = {\n host: dbHost,\n port: dbPort,\n user: DB_USER,\n password: dbPassword,\n database: dbName\n };\n if (DB_CONN_OPTIONS) {\n // See https://github.com/mysqljs/mysql#connection-options for all possible options\n const customOptions = JSON.parse(DB_CONN_OPTIONS);\n Object.assign(dbOptions, customOptions);\n }\n memoizeOptions[prefix] = dbOptions;\n }\n return memoizeOptions[prefix];\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DbClient.js","sourceRoot":"","sources":["../../src/DbClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4CAOwB;AACxB,qCAAmF;AAanF,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,CAAC;aAC/E;iBAAM;gBACL,IAAI,CAAC,iBAAiB,GAAG,yBAAyB,CAAC;aACpD;SACF;aAAM;YACL,IAAI,CAAC,iBAAiB,GAAG,IAAA,kCAAyB,EAAC,EAAE,CAAC,CAAC;SACxD;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,UAAU,GAAG,CAAC,MAAM,IAAA,0BAAgB,EAAC,IAAI,CAAC,iBAAiB,CAAC,CAAmB,CAAC;QACrF,IAAI,CAAC,UAAU,CAAC,GAAG,GAAG,KAAK,EAGzB,GAAW,EACX,GAAmC,EACQ,EAAE;YAC7C,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;gBACpB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;aAC7D;YACD,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,CAAI,GAAG,EAAE,GAAG,CAAC,CAAC;YAChE,OAAO;gBACL,IAAI;gBACJ,IAAI;aACL,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,IAAI,CAAC,UAAU,EAAE;YACnB,M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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;CACF;AApHD,4BAoHC","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 {\n createConnection,\n Connection,\n RowDataPacket,\n ResultSetHeader,\n ProcedureCallPacket,\n FieldPacket\n} from 'mysql2/promise';\nimport { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './Config';\nimport { ConnectionOptions } from 'mysql2';\n\ninterface ConnectionWrap extends Connection {\n run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(\n sql: string,\n phs?: (string | number | boolean)[]\n ): Promise<{\n rows: T;\n cols: FieldPacket[];\n }>;\n}\n\nexport class DbClient {\n private connection?: ConnectionWrap;\n private 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)[]\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)[]) {\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)[]) {\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 return res.rows[0];\n }\n\n async insert(sql: string, phs?: (string | number | boolean)[]) {\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)[]) {\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 delete(sql: string, phs?: (string | number | boolean)[]) {\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"]}
|