@fluidware-it/mysql2-client 0.0.0
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/LICENSE +201 -0
- package/README.md +3 -0
- package/build/esm/Config.d.ts +4 -0
- package/build/esm/Config.js +57 -0
- package/build/esm/Config.js.map +1 -0
- package/build/esm/DbClient.d.ts +26 -0
- package/build/esm/DbClient.js +291 -0
- package/build/esm/DbClient.js.map +1 -0
- package/build/esm/index.d.ts +1 -0
- package/build/esm/index.js +17 -0
- package/build/esm/index.js.map +1 -0
- package/build/esnext/Config.d.ts +4 -0
- package/build/esnext/Config.js +56 -0
- package/build/esnext/Config.js.map +1 -0
- package/build/esnext/DbClient.d.ts +26 -0
- package/build/esnext/DbClient.js +118 -0
- package/build/esnext/DbClient.js.map +1 -0
- package/build/esnext/index.d.ts +1 -0
- package/build/esnext/index.js +17 -0
- package/build/esnext/index.js.map +1 -0
- package/build/src/Config.d.ts +4 -0
- package/build/src/Config.js +60 -0
- package/build/src/Config.js.map +1 -0
- package/build/src/DbClient.d.ts +26 -0
- package/build/src/DbClient.js +122 -0
- package/build/src/DbClient.js.map +1 -0
- package/build/src/index.d.ts +1 -0
- package/build/src/index.js +17 -0
- package/build/src/index.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fluidware srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { EnvParse } from '@fluidware-it/node-utils';
|
|
17
|
+
import * as fs from 'fs';
|
|
18
|
+
export var USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('USE_READ_COMMITTED_ISOLATION', false);
|
|
19
|
+
function getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD) {
|
|
20
|
+
if (DB_PASSWORD_FILE) {
|
|
21
|
+
return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return DB_PASSWORD;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function getMysqlConnectionOptions(prefix) {
|
|
28
|
+
if (prefix === void 0) { prefix = ''; }
|
|
29
|
+
var DB_HOST = EnvParse.envString("".concat(prefix, "DB_HOST"), 'localhost');
|
|
30
|
+
var DB_PORT = EnvParse.envInt("".concat(prefix, "DB_PORT"), 3306);
|
|
31
|
+
var DB_USER = EnvParse.envStringRequired("".concat(prefix, "DB_USER"));
|
|
32
|
+
var DB_NAME = EnvParse.envString("".concat(prefix, "DB_NAME"), DB_USER);
|
|
33
|
+
var DB_CONN_OPTIONS = EnvParse.envJSON("".concat(prefix, "DB_NAME"), {});
|
|
34
|
+
var DB_PASSWORD = EnvParse.envString("".concat(prefix, "DB_PASSWORD"), '');
|
|
35
|
+
var DB_PASSWORD_FILE = EnvParse.envString("".concat(prefix, "DB_PASSWORD_FILE"), '');
|
|
36
|
+
if (!DB_PASSWORD && !DB_PASSWORD_FILE) {
|
|
37
|
+
throw new Error('DB_PASSWORD or DB_PASSWORD_FILE env is required');
|
|
38
|
+
}
|
|
39
|
+
var dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);
|
|
40
|
+
var dbName = DB_NAME || DB_USER;
|
|
41
|
+
var dbHost = DB_HOST || '127.0.0.1';
|
|
42
|
+
var dbPort = Number(DB_PORT) || 3306;
|
|
43
|
+
var dbOptions = {
|
|
44
|
+
host: dbHost,
|
|
45
|
+
port: dbPort,
|
|
46
|
+
user: DB_USER,
|
|
47
|
+
password: dbPassword,
|
|
48
|
+
database: dbName
|
|
49
|
+
};
|
|
50
|
+
if (DB_CONN_OPTIONS) {
|
|
51
|
+
// See https://github.com/mysqljs/mysql#connection-options for all possible options
|
|
52
|
+
var customOptions = JSON.parse(DB_CONN_OPTIONS);
|
|
53
|
+
Object.assign(dbOptions, customOptions);
|
|
54
|
+
}
|
|
55
|
+
return dbOptions;
|
|
56
|
+
}
|
|
57
|
+
//# 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,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,MAAM,UAAU,yBAAyB,CAAC,MAA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AAC,KAAK,CAAC,eAAe,CAAC,CAAC;QAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;KACzC;IACD,OAAO,SAAS,CAAC;AACnB,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\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\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\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 return dbOptions;\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Connection, RowDataPacket, ResultSetHeader, ProcedureCallPacket, FieldPacket } from 'mysql2/promise';
|
|
2
|
+
import { ConnectionOptions } from 'mysql2';
|
|
3
|
+
interface ConnectionWrap extends Connection {
|
|
4
|
+
run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(sql: string, phs?: (string | number | boolean)[]): Promise<{
|
|
5
|
+
rows: T;
|
|
6
|
+
cols: FieldPacket[];
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
export declare class DbClient {
|
|
10
|
+
private connection?;
|
|
11
|
+
private connectionOptions;
|
|
12
|
+
constructor(connectionOptionsOrPrefix?: ConnectionOptions | string);
|
|
13
|
+
open(): Promise<void>;
|
|
14
|
+
getConnection(): ConnectionWrap | undefined;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
startTransaction(): Promise<void>;
|
|
17
|
+
commit(closeTransaction?: boolean): Promise<void>;
|
|
18
|
+
rollback(closeTransaction?: boolean): Promise<void>;
|
|
19
|
+
all(sql: string, phs?: (string | number | boolean)[]): Promise<RowDataPacket[]>;
|
|
20
|
+
get(sql: string, phs?: (string | number | boolean)[]): Promise<RowDataPacket>;
|
|
21
|
+
insert(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
22
|
+
update(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
23
|
+
delete(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=DbClient.d.ts.map
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fluidware srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
26
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
27
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
28
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
29
|
+
function step(op) {
|
|
30
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
31
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
32
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
33
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
34
|
+
switch (op[0]) {
|
|
35
|
+
case 0: case 1: t = op; break;
|
|
36
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
37
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
38
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
39
|
+
default:
|
|
40
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
41
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
42
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
43
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
44
|
+
if (t[2]) _.ops.pop();
|
|
45
|
+
_.trys.pop(); continue;
|
|
46
|
+
}
|
|
47
|
+
op = body.call(thisArg, _);
|
|
48
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
49
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
53
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
54
|
+
if (!m) return o;
|
|
55
|
+
var i = m.call(o), r, ar = [], e;
|
|
56
|
+
try {
|
|
57
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
58
|
+
}
|
|
59
|
+
catch (error) { e = { error: error }; }
|
|
60
|
+
finally {
|
|
61
|
+
try {
|
|
62
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
63
|
+
}
|
|
64
|
+
finally { if (e) throw e.error; }
|
|
65
|
+
}
|
|
66
|
+
return ar;
|
|
67
|
+
};
|
|
68
|
+
import { createConnection } from 'mysql2/promise';
|
|
69
|
+
import { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './Config';
|
|
70
|
+
var DbClient = /** @class */ (function () {
|
|
71
|
+
function DbClient(connectionOptionsOrPrefix) {
|
|
72
|
+
if (connectionOptionsOrPrefix) {
|
|
73
|
+
if (typeof connectionOptionsOrPrefix === 'string') {
|
|
74
|
+
this.connectionOptions = getMysqlConnectionOptions(connectionOptionsOrPrefix);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.connectionOptions = connectionOptionsOrPrefix;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
this.connectionOptions = getMysqlConnectionOptions('');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
DbClient.prototype.open = function () {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
86
|
+
var _a;
|
|
87
|
+
var _this = this;
|
|
88
|
+
return __generator(this, function (_b) {
|
|
89
|
+
switch (_b.label) {
|
|
90
|
+
case 0:
|
|
91
|
+
_a = this;
|
|
92
|
+
return [4 /*yield*/, createConnection(this.connectionOptions)];
|
|
93
|
+
case 1:
|
|
94
|
+
_a.connection = (_b.sent());
|
|
95
|
+
this.connection.run = function (sql, phs) { return __awaiter(_this, void 0, void 0, function () {
|
|
96
|
+
var _a, rows, cols;
|
|
97
|
+
return __generator(this, function (_b) {
|
|
98
|
+
switch (_b.label) {
|
|
99
|
+
case 0:
|
|
100
|
+
if (!this.connection) {
|
|
101
|
+
throw new Error('run() called but no connection available');
|
|
102
|
+
}
|
|
103
|
+
return [4 /*yield*/, this.connection.execute(sql, phs)];
|
|
104
|
+
case 1:
|
|
105
|
+
_a = __read.apply(void 0, [_b.sent(), 2]), rows = _a[0], cols = _a[1];
|
|
106
|
+
return [2 /*return*/, {
|
|
107
|
+
rows: rows,
|
|
108
|
+
cols: cols
|
|
109
|
+
}];
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}); };
|
|
113
|
+
return [2 /*return*/];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
DbClient.prototype.getConnection = function () {
|
|
119
|
+
return this.connection;
|
|
120
|
+
};
|
|
121
|
+
DbClient.prototype.close = function () {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
123
|
+
return __generator(this, function (_a) {
|
|
124
|
+
switch (_a.label) {
|
|
125
|
+
case 0:
|
|
126
|
+
if (!this.connection) return [3 /*break*/, 2];
|
|
127
|
+
return [4 /*yield*/, this.connection.end()];
|
|
128
|
+
case 1:
|
|
129
|
+
_a.sent();
|
|
130
|
+
_a.label = 2;
|
|
131
|
+
case 2: return [2 /*return*/];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
DbClient.prototype.startTransaction = function () {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
138
|
+
return __generator(this, function (_a) {
|
|
139
|
+
switch (_a.label) {
|
|
140
|
+
case 0:
|
|
141
|
+
if (!this.connection) {
|
|
142
|
+
throw new Error('no connection available');
|
|
143
|
+
}
|
|
144
|
+
if (!USE_READ_COMMITTED_ISOLATION) return [3 /*break*/, 2];
|
|
145
|
+
return [4 /*yield*/, this.connection.execute("SET SESSION tx_isolation='read-committed'")];
|
|
146
|
+
case 1:
|
|
147
|
+
_a.sent();
|
|
148
|
+
_a.label = 2;
|
|
149
|
+
case 2: return [4 /*yield*/, this.connection.execute('SET AUTOCOMMIT=0')];
|
|
150
|
+
case 3:
|
|
151
|
+
_a.sent();
|
|
152
|
+
return [2 /*return*/];
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
DbClient.prototype.commit = function (closeTransaction) {
|
|
158
|
+
if (closeTransaction === void 0) { closeTransaction = true; }
|
|
159
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
160
|
+
return __generator(this, function (_a) {
|
|
161
|
+
switch (_a.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
if (!this.connection) {
|
|
164
|
+
throw new Error('no connection available');
|
|
165
|
+
}
|
|
166
|
+
return [4 /*yield*/, this.connection.execute('COMMIT')];
|
|
167
|
+
case 1:
|
|
168
|
+
_a.sent();
|
|
169
|
+
if (!closeTransaction) return [3 /*break*/, 3];
|
|
170
|
+
return [4 /*yield*/, this.connection.execute('SET AUTOCOMMIT=1')];
|
|
171
|
+
case 2:
|
|
172
|
+
_a.sent();
|
|
173
|
+
_a.label = 3;
|
|
174
|
+
case 3: return [2 /*return*/];
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
DbClient.prototype.rollback = function (closeTransaction) {
|
|
180
|
+
if (closeTransaction === void 0) { closeTransaction = true; }
|
|
181
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
182
|
+
return __generator(this, function (_a) {
|
|
183
|
+
switch (_a.label) {
|
|
184
|
+
case 0:
|
|
185
|
+
if (!this.connection) {
|
|
186
|
+
throw new Error('no connection available');
|
|
187
|
+
}
|
|
188
|
+
return [4 /*yield*/, this.connection.execute('ROLLBACK')];
|
|
189
|
+
case 1:
|
|
190
|
+
_a.sent();
|
|
191
|
+
if (!closeTransaction) return [3 /*break*/, 3];
|
|
192
|
+
return [4 /*yield*/, this.connection.execute('SET AUTOCOMMIT=1')];
|
|
193
|
+
case 2:
|
|
194
|
+
_a.sent();
|
|
195
|
+
_a.label = 3;
|
|
196
|
+
case 3: return [2 /*return*/];
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
DbClient.prototype.all = function (sql, phs) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
203
|
+
var res;
|
|
204
|
+
return __generator(this, function (_a) {
|
|
205
|
+
switch (_a.label) {
|
|
206
|
+
case 0:
|
|
207
|
+
if (!this.connection) {
|
|
208
|
+
throw new Error('no connection available');
|
|
209
|
+
}
|
|
210
|
+
return [4 /*yield*/, this.connection.run(sql, phs)];
|
|
211
|
+
case 1:
|
|
212
|
+
res = _a.sent();
|
|
213
|
+
return [2 /*return*/, res.rows];
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
DbClient.prototype.get = function (sql, phs) {
|
|
219
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
220
|
+
var res;
|
|
221
|
+
return __generator(this, function (_a) {
|
|
222
|
+
switch (_a.label) {
|
|
223
|
+
case 0:
|
|
224
|
+
if (!this.connection) {
|
|
225
|
+
throw new Error('no connection available');
|
|
226
|
+
}
|
|
227
|
+
return [4 /*yield*/, this.connection.run(sql, phs)];
|
|
228
|
+
case 1:
|
|
229
|
+
res = _a.sent();
|
|
230
|
+
if (res.rows.length > 1)
|
|
231
|
+
throw new Error('get returned more than one row');
|
|
232
|
+
return [2 /*return*/, res.rows[0]];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
DbClient.prototype.insert = function (sql, phs) {
|
|
238
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
239
|
+
var res;
|
|
240
|
+
return __generator(this, function (_a) {
|
|
241
|
+
switch (_a.label) {
|
|
242
|
+
case 0:
|
|
243
|
+
if (!this.connection) {
|
|
244
|
+
throw new Error('no connection available');
|
|
245
|
+
}
|
|
246
|
+
return [4 /*yield*/, this.connection.run(sql, phs)];
|
|
247
|
+
case 1:
|
|
248
|
+
res = _a.sent();
|
|
249
|
+
return [2 /*return*/, res.rows.insertId || res.rows.affectedRows];
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
DbClient.prototype.update = function (sql, phs) {
|
|
255
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
256
|
+
var res;
|
|
257
|
+
return __generator(this, function (_a) {
|
|
258
|
+
switch (_a.label) {
|
|
259
|
+
case 0:
|
|
260
|
+
if (!this.connection) {
|
|
261
|
+
throw new Error('no connection available');
|
|
262
|
+
}
|
|
263
|
+
return [4 /*yield*/, this.connection.run(sql, phs)];
|
|
264
|
+
case 1:
|
|
265
|
+
res = _a.sent();
|
|
266
|
+
return [2 /*return*/, res.rows.affectedRows];
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
DbClient.prototype.delete = function (sql, phs) {
|
|
272
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
273
|
+
var res;
|
|
274
|
+
return __generator(this, function (_a) {
|
|
275
|
+
switch (_a.label) {
|
|
276
|
+
case 0:
|
|
277
|
+
if (!this.connection) {
|
|
278
|
+
throw new Error('no connection available');
|
|
279
|
+
}
|
|
280
|
+
return [4 /*yield*/, this.connection.run(sql, phs)];
|
|
281
|
+
case 1:
|
|
282
|
+
res = _a.sent();
|
|
283
|
+
return [2 /*return*/, res.rows.affectedRows];
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
};
|
|
288
|
+
return DbClient;
|
|
289
|
+
}());
|
|
290
|
+
export { DbClient };
|
|
291
|
+
//# sourceMappingURL=DbClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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;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AC,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;IACH,eAAC;AAAD,CAAC,AApHD,IAoHC","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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Fluidware srl
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG","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"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fluidware srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { EnvParse } from '@fluidware-it/node-utils';
|
|
17
|
+
import * as fs from 'fs';
|
|
18
|
+
export const USE_READ_COMMITTED_ISOLATION = EnvParse.envBool('USE_READ_COMMITTED_ISOLATION', false);
|
|
19
|
+
function getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD) {
|
|
20
|
+
if (DB_PASSWORD_FILE) {
|
|
21
|
+
return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return DB_PASSWORD;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function getMysqlConnectionOptions(prefix = '') {
|
|
28
|
+
const DB_HOST = EnvParse.envString(`${prefix}DB_HOST`, 'localhost');
|
|
29
|
+
const DB_PORT = EnvParse.envInt(`${prefix}DB_PORT`, 3306);
|
|
30
|
+
const DB_USER = EnvParse.envStringRequired(`${prefix}DB_USER`);
|
|
31
|
+
const DB_NAME = EnvParse.envString(`${prefix}DB_NAME`, DB_USER);
|
|
32
|
+
const DB_CONN_OPTIONS = EnvParse.envJSON(`${prefix}DB_NAME`, {});
|
|
33
|
+
const DB_PASSWORD = EnvParse.envString(`${prefix}DB_PASSWORD`, '');
|
|
34
|
+
const DB_PASSWORD_FILE = EnvParse.envString(`${prefix}DB_PASSWORD_FILE`, '');
|
|
35
|
+
if (!DB_PASSWORD && !DB_PASSWORD_FILE) {
|
|
36
|
+
throw new Error('DB_PASSWORD or DB_PASSWORD_FILE env is required');
|
|
37
|
+
}
|
|
38
|
+
const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);
|
|
39
|
+
const dbName = DB_NAME || DB_USER;
|
|
40
|
+
const dbHost = DB_HOST || '127.0.0.1';
|
|
41
|
+
const dbPort = Number(DB_PORT) || 3306;
|
|
42
|
+
const dbOptions = {
|
|
43
|
+
host: dbHost,
|
|
44
|
+
port: dbPort,
|
|
45
|
+
user: DB_USER,
|
|
46
|
+
password: dbPassword,
|
|
47
|
+
database: dbName
|
|
48
|
+
};
|
|
49
|
+
if (DB_CONN_OPTIONS) {
|
|
50
|
+
// See https://github.com/mysqljs/mysql#connection-options for all possible options
|
|
51
|
+
const customOptions = JSON.parse(DB_CONN_OPTIONS);
|
|
52
|
+
Object.assign(dbOptions, customOptions);
|
|
53
|
+
}
|
|
54
|
+
return dbOptions;
|
|
55
|
+
}
|
|
56
|
+
//# 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,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,gBA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,QAAQ,EAAE,MAAM;KACjB,CAAC;IACF,IAAI,eAAe,EAAE;QACnB,mFAAmF;QACnF,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;QAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;KACzC;IACD,OAAO,SAAS,CAAC;AACnB,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\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\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\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 return dbOptions;\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Connection, RowDataPacket, ResultSetHeader, ProcedureCallPacket, FieldPacket } from 'mysql2/promise';
|
|
2
|
+
import { ConnectionOptions } from 'mysql2';
|
|
3
|
+
interface ConnectionWrap extends Connection {
|
|
4
|
+
run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(sql: string, phs?: (string | number | boolean)[]): Promise<{
|
|
5
|
+
rows: T;
|
|
6
|
+
cols: FieldPacket[];
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
export declare class DbClient {
|
|
10
|
+
private connection?;
|
|
11
|
+
private connectionOptions;
|
|
12
|
+
constructor(connectionOptionsOrPrefix?: ConnectionOptions | string);
|
|
13
|
+
open(): Promise<void>;
|
|
14
|
+
getConnection(): ConnectionWrap | undefined;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
startTransaction(): Promise<void>;
|
|
17
|
+
commit(closeTransaction?: boolean): Promise<void>;
|
|
18
|
+
rollback(closeTransaction?: boolean): Promise<void>;
|
|
19
|
+
all(sql: string, phs?: (string | number | boolean)[]): Promise<RowDataPacket[]>;
|
|
20
|
+
get(sql: string, phs?: (string | number | boolean)[]): Promise<RowDataPacket>;
|
|
21
|
+
insert(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
22
|
+
update(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
23
|
+
delete(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=DbClient.d.ts.map
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fluidware srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { createConnection } from 'mysql2/promise';
|
|
17
|
+
import { getMysqlConnectionOptions, USE_READ_COMMITTED_ISOLATION } from './Config';
|
|
18
|
+
export class DbClient {
|
|
19
|
+
constructor(connectionOptionsOrPrefix) {
|
|
20
|
+
if (connectionOptionsOrPrefix) {
|
|
21
|
+
if (typeof connectionOptionsOrPrefix === 'string') {
|
|
22
|
+
this.connectionOptions = getMysqlConnectionOptions(connectionOptionsOrPrefix);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.connectionOptions = connectionOptionsOrPrefix;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.connectionOptions = getMysqlConnectionOptions('');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async open() {
|
|
33
|
+
this.connection = (await createConnection(this.connectionOptions));
|
|
34
|
+
this.connection.run = async (sql, phs) => {
|
|
35
|
+
if (!this.connection) {
|
|
36
|
+
throw new Error('run() called but no connection available');
|
|
37
|
+
}
|
|
38
|
+
const [rows, cols] = await this.connection.execute(sql, phs);
|
|
39
|
+
return {
|
|
40
|
+
rows,
|
|
41
|
+
cols
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
getConnection() {
|
|
46
|
+
return this.connection;
|
|
47
|
+
}
|
|
48
|
+
async close() {
|
|
49
|
+
if (this.connection) {
|
|
50
|
+
await this.connection.end();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async startTransaction() {
|
|
54
|
+
if (!this.connection) {
|
|
55
|
+
throw new Error('no connection available');
|
|
56
|
+
}
|
|
57
|
+
if (USE_READ_COMMITTED_ISOLATION) {
|
|
58
|
+
await this.connection.execute("SET SESSION tx_isolation='read-committed'");
|
|
59
|
+
}
|
|
60
|
+
await this.connection.execute('SET AUTOCOMMIT=0');
|
|
61
|
+
}
|
|
62
|
+
async commit(closeTransaction = true) {
|
|
63
|
+
if (!this.connection) {
|
|
64
|
+
throw new Error('no connection available');
|
|
65
|
+
}
|
|
66
|
+
await this.connection.execute('COMMIT');
|
|
67
|
+
if (closeTransaction) {
|
|
68
|
+
await this.connection.execute('SET AUTOCOMMIT=1');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async rollback(closeTransaction = true) {
|
|
72
|
+
if (!this.connection) {
|
|
73
|
+
throw new Error('no connection available');
|
|
74
|
+
}
|
|
75
|
+
await this.connection.execute('ROLLBACK');
|
|
76
|
+
if (closeTransaction) {
|
|
77
|
+
await this.connection.execute('SET AUTOCOMMIT=1');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async all(sql, phs) {
|
|
81
|
+
if (!this.connection) {
|
|
82
|
+
throw new Error('no connection available');
|
|
83
|
+
}
|
|
84
|
+
const res = await this.connection.run(sql, phs);
|
|
85
|
+
return res.rows;
|
|
86
|
+
}
|
|
87
|
+
async get(sql, phs) {
|
|
88
|
+
if (!this.connection) {
|
|
89
|
+
throw new Error('no connection available');
|
|
90
|
+
}
|
|
91
|
+
const res = await this.connection.run(sql, phs);
|
|
92
|
+
if (res.rows.length > 1)
|
|
93
|
+
throw new Error('get returned more than one row');
|
|
94
|
+
return res.rows[0];
|
|
95
|
+
}
|
|
96
|
+
async insert(sql, phs) {
|
|
97
|
+
if (!this.connection) {
|
|
98
|
+
throw new Error('no connection available');
|
|
99
|
+
}
|
|
100
|
+
const res = await this.connection.run(sql, phs);
|
|
101
|
+
return res.rows.insertId || res.rows.affectedRows;
|
|
102
|
+
}
|
|
103
|
+
async update(sql, phs) {
|
|
104
|
+
if (!this.connection) {
|
|
105
|
+
throw new Error('no connection available');
|
|
106
|
+
}
|
|
107
|
+
const res = await this.connection.run(sql, phs);
|
|
108
|
+
return res.rows.affectedRows;
|
|
109
|
+
}
|
|
110
|
+
async delete(sql, phs) {
|
|
111
|
+
if (!this.connection) {
|
|
112
|
+
throw new Error('no connection available');
|
|
113
|
+
}
|
|
114
|
+
const res = await this.connection.run(sql, phs);
|
|
115
|
+
return res.rows.affectedRows;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=DbClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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;S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AC,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,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;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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Fluidware srl
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG","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"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Fluidware srl
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.getMysqlConnectionOptions = exports.USE_READ_COMMITTED_ISOLATION = void 0;
|
|
19
|
+
const node_utils_1 = require("@fluidware-it/node-utils");
|
|
20
|
+
const fs = require("fs");
|
|
21
|
+
exports.USE_READ_COMMITTED_ISOLATION = node_utils_1.EnvParse.envBool('USE_READ_COMMITTED_ISOLATION', false);
|
|
22
|
+
function getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD) {
|
|
23
|
+
if (DB_PASSWORD_FILE) {
|
|
24
|
+
return fs.readFileSync(DB_PASSWORD_FILE, 'utf8');
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return DB_PASSWORD;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function getMysqlConnectionOptions(prefix = '') {
|
|
31
|
+
const DB_HOST = node_utils_1.EnvParse.envString(`${prefix}DB_HOST`, 'localhost');
|
|
32
|
+
const DB_PORT = node_utils_1.EnvParse.envInt(`${prefix}DB_PORT`, 3306);
|
|
33
|
+
const DB_USER = node_utils_1.EnvParse.envStringRequired(`${prefix}DB_USER`);
|
|
34
|
+
const DB_NAME = node_utils_1.EnvParse.envString(`${prefix}DB_NAME`, DB_USER);
|
|
35
|
+
const DB_CONN_OPTIONS = node_utils_1.EnvParse.envJSON(`${prefix}DB_NAME`, {});
|
|
36
|
+
const DB_PASSWORD = node_utils_1.EnvParse.envString(`${prefix}DB_PASSWORD`, '');
|
|
37
|
+
const DB_PASSWORD_FILE = node_utils_1.EnvParse.envString(`${prefix}DB_PASSWORD_FILE`, '');
|
|
38
|
+
if (!DB_PASSWORD && !DB_PASSWORD_FILE) {
|
|
39
|
+
throw new Error('DB_PASSWORD or DB_PASSWORD_FILE env is required');
|
|
40
|
+
}
|
|
41
|
+
const dbPassword = getDbPassword(DB_PASSWORD_FILE, DB_PASSWORD);
|
|
42
|
+
const dbName = DB_NAME || DB_USER;
|
|
43
|
+
const dbHost = DB_HOST || '127.0.0.1';
|
|
44
|
+
const dbPort = Number(DB_PORT) || 3306;
|
|
45
|
+
const dbOptions = {
|
|
46
|
+
host: dbHost,
|
|
47
|
+
port: dbPort,
|
|
48
|
+
user: DB_USER,
|
|
49
|
+
password: dbPassword,
|
|
50
|
+
database: dbName
|
|
51
|
+
};
|
|
52
|
+
if (DB_CONN_OPTIONS) {
|
|
53
|
+
// See https://github.com/mysqljs/mysql#connection-options for all possible options
|
|
54
|
+
const customOptions = JSON.parse(DB_CONN_OPTIONS);
|
|
55
|
+
Object.assign(dbOptions, customOptions);
|
|
56
|
+
}
|
|
57
|
+
return dbOptions;
|
|
58
|
+
}
|
|
59
|
+
exports.getMysqlConnectionOptions = getMysqlConnectionOptions;
|
|
60
|
+
//# sourceMappingURL=Config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,SAAgB,yBAAyB,CAAC,MAAM,GAAG,EAAE;IACnD,MAAM,OAAO,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,SAAS,EAAE,WAAW,CAAC,CAAC;IACpE,MAAM,OAAO,GAAG,qBAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,SAAS,EAAE,IAAI,CAAC,CAAC;IAC1D,MAAM,OAAO,GAAG,qBAAQ,CAAC,iBAAiB,CAAC,GAAG,MAAM,SAAS,CAAC,CAAC;IAC/D,MAAM,OAAO,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,SAAS,EAAE,OAAO,CAAC,CAAC;IAChE,MAAM,eAAe,GAAG,qBAAQ,CAAC,OAAO,CAAC,GAAG,MAAM,SAAS,EAAE,EAAE,CAAC,CAAC;IACjE,MAAM,WAAW,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,aAAa,EAAE,EAAE,CAAC,CAAC;IACnE,MAAM,gBAAgB,GAAG,qBAAQ,CAAC,SAAS,CAAC,GAAG,MAAM,kBAAkB,EAAE,EAAE,CAAC,CAAC;IAE7E,IAAI,CAAC,WAAW,IAAI,CAAC,gBAAgB,EAAE;QACrC,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;KACpE;IAED,MAAM,UAAU,GAAG,aAAa,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;IAChE,MAAM,MAAM,GAAG,OAAO,IAAI,OAAO,CAAC;IAClC,MAAM,MAAM,GAAG,OAAO,IAAI,WAAW,CAAC;IACtC,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;IAEvC,MAAM,SAAS,GAAG;QAChB,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,OAAO;QACb,QAAQ,EAAE,UAAU;QACpB,QAAQ,EAAE,MAAM;KACjB,CAAC;IACF,IAAI,eAAe,EAAE;QACnB,mFAAmF;QACnF,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;QAClD,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;KACzC;IACD,OAAO,SAAS,CAAC;AACnB,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/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\nexport function getMysqlConnectionOptions(prefix = ''): ConnectionOptions {\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\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 return dbOptions;\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Connection, RowDataPacket, ResultSetHeader, ProcedureCallPacket, FieldPacket } from 'mysql2/promise';
|
|
2
|
+
import { ConnectionOptions } from 'mysql2';
|
|
3
|
+
interface ConnectionWrap extends Connection {
|
|
4
|
+
run<T extends ResultSetHeader | ResultSetHeader[] | RowDataPacket[] | RowDataPacket[][] | ProcedureCallPacket>(sql: string, phs?: (string | number | boolean)[]): Promise<{
|
|
5
|
+
rows: T;
|
|
6
|
+
cols: FieldPacket[];
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
export declare class DbClient {
|
|
10
|
+
private connection?;
|
|
11
|
+
private connectionOptions;
|
|
12
|
+
constructor(connectionOptionsOrPrefix?: ConnectionOptions | string);
|
|
13
|
+
open(): Promise<void>;
|
|
14
|
+
getConnection(): ConnectionWrap | undefined;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
startTransaction(): Promise<void>;
|
|
17
|
+
commit(closeTransaction?: boolean): Promise<void>;
|
|
18
|
+
rollback(closeTransaction?: boolean): Promise<void>;
|
|
19
|
+
all(sql: string, phs?: (string | number | boolean)[]): Promise<RowDataPacket[]>;
|
|
20
|
+
get(sql: string, phs?: (string | number | boolean)[]): Promise<RowDataPacket>;
|
|
21
|
+
insert(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
22
|
+
update(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
23
|
+
delete(sql: string, phs?: (string | number | boolean)[]): Promise<number>;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=DbClient.d.ts.map
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Fluidware srl
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.DbClient = void 0;
|
|
19
|
+
const promise_1 = require("mysql2/promise");
|
|
20
|
+
const Config_1 = require("./Config");
|
|
21
|
+
class DbClient {
|
|
22
|
+
constructor(connectionOptionsOrPrefix) {
|
|
23
|
+
if (connectionOptionsOrPrefix) {
|
|
24
|
+
if (typeof connectionOptionsOrPrefix === 'string') {
|
|
25
|
+
this.connectionOptions = (0, Config_1.getMysqlConnectionOptions)(connectionOptionsOrPrefix);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.connectionOptions = connectionOptionsOrPrefix;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.connectionOptions = (0, Config_1.getMysqlConnectionOptions)('');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async open() {
|
|
36
|
+
this.connection = (await (0, promise_1.createConnection)(this.connectionOptions));
|
|
37
|
+
this.connection.run = async (sql, phs) => {
|
|
38
|
+
if (!this.connection) {
|
|
39
|
+
throw new Error('run() called but no connection available');
|
|
40
|
+
}
|
|
41
|
+
const [rows, cols] = await this.connection.execute(sql, phs);
|
|
42
|
+
return {
|
|
43
|
+
rows,
|
|
44
|
+
cols
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
getConnection() {
|
|
49
|
+
return this.connection;
|
|
50
|
+
}
|
|
51
|
+
async close() {
|
|
52
|
+
if (this.connection) {
|
|
53
|
+
await this.connection.end();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async startTransaction() {
|
|
57
|
+
if (!this.connection) {
|
|
58
|
+
throw new Error('no connection available');
|
|
59
|
+
}
|
|
60
|
+
if (Config_1.USE_READ_COMMITTED_ISOLATION) {
|
|
61
|
+
await this.connection.execute("SET SESSION tx_isolation='read-committed'");
|
|
62
|
+
}
|
|
63
|
+
await this.connection.execute('SET AUTOCOMMIT=0');
|
|
64
|
+
}
|
|
65
|
+
async commit(closeTransaction = true) {
|
|
66
|
+
if (!this.connection) {
|
|
67
|
+
throw new Error('no connection available');
|
|
68
|
+
}
|
|
69
|
+
await this.connection.execute('COMMIT');
|
|
70
|
+
if (closeTransaction) {
|
|
71
|
+
await this.connection.execute('SET AUTOCOMMIT=1');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async rollback(closeTransaction = true) {
|
|
75
|
+
if (!this.connection) {
|
|
76
|
+
throw new Error('no connection available');
|
|
77
|
+
}
|
|
78
|
+
await this.connection.execute('ROLLBACK');
|
|
79
|
+
if (closeTransaction) {
|
|
80
|
+
await this.connection.execute('SET AUTOCOMMIT=1');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async all(sql, phs) {
|
|
84
|
+
if (!this.connection) {
|
|
85
|
+
throw new Error('no connection available');
|
|
86
|
+
}
|
|
87
|
+
const res = await this.connection.run(sql, phs);
|
|
88
|
+
return res.rows;
|
|
89
|
+
}
|
|
90
|
+
async get(sql, phs) {
|
|
91
|
+
if (!this.connection) {
|
|
92
|
+
throw new Error('no connection available');
|
|
93
|
+
}
|
|
94
|
+
const res = await this.connection.run(sql, phs);
|
|
95
|
+
if (res.rows.length > 1)
|
|
96
|
+
throw new Error('get returned more than one row');
|
|
97
|
+
return res.rows[0];
|
|
98
|
+
}
|
|
99
|
+
async insert(sql, phs) {
|
|
100
|
+
if (!this.connection) {
|
|
101
|
+
throw new Error('no connection available');
|
|
102
|
+
}
|
|
103
|
+
const res = await this.connection.run(sql, phs);
|
|
104
|
+
return res.rows.insertId || res.rows.affectedRows;
|
|
105
|
+
}
|
|
106
|
+
async update(sql, phs) {
|
|
107
|
+
if (!this.connection) {
|
|
108
|
+
throw new Error('no connection available');
|
|
109
|
+
}
|
|
110
|
+
const res = await this.connection.run(sql, phs);
|
|
111
|
+
return res.rows.affectedRows;
|
|
112
|
+
}
|
|
113
|
+
async delete(sql, phs) {
|
|
114
|
+
if (!this.connection) {
|
|
115
|
+
throw new Error('no connection available');
|
|
116
|
+
}
|
|
117
|
+
const res = await this.connection.run(sql, phs);
|
|
118
|
+
return res.rows.affectedRows;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.DbClient = DbClient;
|
|
122
|
+
//# 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,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,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AyB,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;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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Fluidware srl
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG","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"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fluidware-it/mysql2-client",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"main": "build/src/index.js",
|
|
5
|
+
"module": "build/esm/index.js",
|
|
6
|
+
"esnext": "build/esnext/index.js",
|
|
7
|
+
"types": "build/src/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"author": "Fluidware srl",
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"prepack": "npm run compile",
|
|
15
|
+
"compile": "npm run clean && tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
|
|
16
|
+
"clean": "tsc --build --clean tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
|
|
17
|
+
"lint": "eslint src",
|
|
18
|
+
"lint:fix": "eslint --fix src",
|
|
19
|
+
"test": "jest"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18.4"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@fluidware-it/node-utils": "^0.0.0",
|
|
26
|
+
"mysql2": "^3.6.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/jest": "^29.5.6",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^6.8.0",
|
|
31
|
+
"@typescript-eslint/parser": "^6.8.0",
|
|
32
|
+
"eslint": "^8.51.0",
|
|
33
|
+
"eslint-config-prettier": "^9.0.0",
|
|
34
|
+
"eslint-plugin-node": "^11.1.0",
|
|
35
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"prettier": "^3.0.3",
|
|
38
|
+
"ts-jest": "^29.1.1",
|
|
39
|
+
"typescript": "^5.2.2"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"build/esm/**/*.js",
|
|
43
|
+
"build/esm/**/*.js.map",
|
|
44
|
+
"build/esm/**/*.d.ts",
|
|
45
|
+
"build/esnext/**/*.js",
|
|
46
|
+
"build/esnext/**/*.js.map",
|
|
47
|
+
"build/esnext/**/*.d.ts",
|
|
48
|
+
"build/src/**/*.js",
|
|
49
|
+
"build/src/**/*.js.map",
|
|
50
|
+
"build/src/**/*.d.ts",
|
|
51
|
+
"LICENSE",
|
|
52
|
+
"README.md"
|
|
53
|
+
]
|
|
54
|
+
}
|