@e-mc/db 0.8.7 → 0.9.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 +10 -10
- package/README.md +133 -9
- package/index.d.ts +4 -4
- package/index.js +12 -11
- package/package.json +4 -4
- package/pool.d.ts +4 -4
- package/pool.js +9 -16
- package/util.d.ts +1 -0
- package/util.js +0 -1
package/LICENSE
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Copyright 2024 An Pham
|
|
2
|
-
|
|
3
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
-
|
|
5
|
-
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
-
|
|
7
|
-
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
-
|
|
9
|
-
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
-
|
|
1
|
+
Copyright 2024 An Pham
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
11
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @e-mc/db
|
|
2
2
|
|
|
3
|
-
* NodeJS 14
|
|
3
|
+
* NodeJS 14/16
|
|
4
4
|
* ES2020
|
|
5
5
|
|
|
6
6
|
## General Usage
|
|
@@ -9,15 +9,16 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.9.0/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { DbDataSource } from "./squared";
|
|
16
16
|
|
|
17
17
|
import type { IHost } from "./index";
|
|
18
18
|
import type { ClientDbConstructor, IClientDb } from "./core";
|
|
19
|
-
import type {
|
|
20
|
-
import type {
|
|
19
|
+
import type { BatchQueryResult, DB_TYPE, ErrorQueryCallback, ExecuteBatchQueryOptions, ExecuteQueryOptions, HandleFailOptions, ProcessRowsOptions, QueryResult, SQL_COMMAND } from "./db";
|
|
20
|
+
import type { AuthValue } from "./http";
|
|
21
|
+
import type { DbCoerceSettings, DbModule, DbSourceOptions, PoolConfig } from "./settings";
|
|
21
22
|
|
|
22
23
|
import type { SecureContextOptions } from "tls";
|
|
23
24
|
|
|
@@ -26,8 +27,10 @@ interface IDb extends IClientDb<IHost, DbModule, DbDataSource, DbSourceOptions,
|
|
|
26
27
|
getCredential(item: DbDataSource): Record<string | number | symbol, unknown>;
|
|
27
28
|
hasSource(source: string, ...type: number[]): boolean;
|
|
28
29
|
applyCommand(...items: DbDataSource[]): void;
|
|
30
|
+
executeQuery(item: V, callback: ErrorQueryCallback): Promise<QueryResult>;
|
|
29
31
|
executeQuery(item: DbDataSource, sessionKey: string): Promise<QueryResult>;
|
|
30
32
|
executeQuery(item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
|
|
33
|
+
executeBatchQuery(batch: DbDataSource[], callback: ErrorQueryCallback, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
31
34
|
executeBatchQuery(batch: DbDataSource[], sessionKey: string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
32
35
|
executeBatchQuery(batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
33
36
|
processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], parallel: boolean): Promise<BatchQueryResult>;
|
|
@@ -92,13 +95,134 @@ interface DbPoolConstructor {
|
|
|
92
95
|
}
|
|
93
96
|
```
|
|
94
97
|
|
|
98
|
+
## Settings
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import type { DbSourceOptions, PurgeComponent } from "./settings";
|
|
102
|
+
|
|
103
|
+
interface DbModule {
|
|
104
|
+
// handler: "@e-mc/db";
|
|
105
|
+
mariadb?: DbStoredCredentials;
|
|
106
|
+
mongodb?: DbStoredCredentials;
|
|
107
|
+
mssql?: DbStoredCredentials;
|
|
108
|
+
mysql?: DbStoredCredentials;
|
|
109
|
+
oracle?: DbStoredCredentials;
|
|
110
|
+
postgres?: DbStoredCredentials;
|
|
111
|
+
redis?: DbStoredCredentials;
|
|
112
|
+
settings?: {
|
|
113
|
+
broadcast_id?: string | string[];
|
|
114
|
+
users?: Record<string, Record<string, unknown>>;
|
|
115
|
+
cache_dir?: string;
|
|
116
|
+
session_expires?: number;
|
|
117
|
+
user_key?: Record<string, DbSourceOptions>;
|
|
118
|
+
imports?: StringMap;
|
|
119
|
+
purge?: PurgeComponent;
|
|
120
|
+
mariadb?: DbSourceOptions;
|
|
121
|
+
mongodb?: DbSourceOptions;
|
|
122
|
+
mssql?: DbSourceOptions;
|
|
123
|
+
mysql?: DbSourceOptions;
|
|
124
|
+
oracle?: DbSourceOptions;
|
|
125
|
+
postgres?: DbSourceOptions;
|
|
126
|
+
redis?: DbSourceOptions;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type DbStoredCredentials = Record<string, Record<string, unknown>>;
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Example usage
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
const Db = require("@e-mc/db"); // Using @pi-r/mongodb
|
|
137
|
+
|
|
138
|
+
const instance = new Db({
|
|
139
|
+
mongodb: {
|
|
140
|
+
main: {
|
|
141
|
+
server: "localhost:27017",
|
|
142
|
+
auth: {
|
|
143
|
+
username: "**********",
|
|
144
|
+
password: "**********"
|
|
145
|
+
},
|
|
146
|
+
authMechanism: "SCRAM-SHA-1"
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
settings: {
|
|
150
|
+
mongodb: {
|
|
151
|
+
pool: {
|
|
152
|
+
max: 10,
|
|
153
|
+
idle: 60 * 1000,
|
|
154
|
+
queue_max: 4,
|
|
155
|
+
queue_idle: 30 * 1000,
|
|
156
|
+
timeout: 10 * 1000
|
|
157
|
+
},
|
|
158
|
+
cache: {
|
|
159
|
+
timeout: "1d",
|
|
160
|
+
when_empty: false
|
|
161
|
+
},
|
|
162
|
+
coerce: {
|
|
163
|
+
credential: false,
|
|
164
|
+
options: true
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
// instance.host = new Host();
|
|
170
|
+
instance.init();
|
|
171
|
+
|
|
172
|
+
const item = {
|
|
173
|
+
source: "mongodb",
|
|
174
|
+
credential: "main",
|
|
175
|
+
table: "demo",
|
|
176
|
+
name: "nodejs",
|
|
177
|
+
query: {
|
|
178
|
+
id: {
|
|
179
|
+
"$eq": "1"
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
willAbort: true
|
|
183
|
+
};
|
|
184
|
+
await instance.setCredential(item);
|
|
185
|
+
|
|
186
|
+
const rows = await instance.executeQuery(item, (err, item) => {
|
|
187
|
+
if (err.code === "E11000") {
|
|
188
|
+
return true; // throw err;
|
|
189
|
+
}
|
|
190
|
+
return false; // return [];
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const [rows1, rows2] = await instance.executeBatchQuery([
|
|
194
|
+
{ ...item, usePool: true },
|
|
195
|
+
{ ...item, query: { id: { "$eq": "2" } } }
|
|
196
|
+
],
|
|
197
|
+
{ parallel: true, connectOnce: true }
|
|
198
|
+
);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## NodeJS 14 LTS
|
|
202
|
+
|
|
203
|
+
Any optional fail safe dependencies were removed as of `E-mc 0.9`. The code itself will still be *ES2020* and will continue to work equivalently when self-installing these dependencies:
|
|
204
|
+
|
|
205
|
+
### Under 15.4 + 16.0
|
|
206
|
+
|
|
207
|
+
```sh
|
|
208
|
+
npm i abort-controller event-target-shim
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Under 14.17 + 15.6
|
|
212
|
+
|
|
213
|
+
```sh
|
|
214
|
+
npm i uuid
|
|
215
|
+
```
|
|
216
|
+
|
|
95
217
|
## References
|
|
96
218
|
|
|
97
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
98
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
99
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
100
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
101
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
219
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/squared.d.ts
|
|
220
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/core.d.ts
|
|
221
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/db.d.ts
|
|
222
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/http.d.ts
|
|
223
|
+
- https://www.unpkg.com/@e-mc/types@0.9.0/lib/settings.d.ts
|
|
224
|
+
|
|
225
|
+
* https://www.npmjs.com/package/@types/node
|
|
102
226
|
|
|
103
227
|
## LICENSE
|
|
104
228
|
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DbConstructor } from '../types/lib';
|
|
2
|
-
|
|
3
|
-
declare const Db: DbConstructor;
|
|
4
|
-
|
|
1
|
+
import type { DbConstructor } from '../types/lib';
|
|
2
|
+
|
|
3
|
+
declare const Db: DbConstructor;
|
|
4
|
+
|
|
5
5
|
export = Db;
|
package/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
const path = require("path");
|
|
4
3
|
const types_1 = require("@e-mc/types");
|
|
5
4
|
const core_1 = require("@e-mc/core");
|
|
@@ -124,7 +123,10 @@ class Db extends core_1.ClientDb {
|
|
|
124
123
|
}
|
|
125
124
|
async executeQuery(item, options) {
|
|
126
125
|
if (this.aborted) {
|
|
127
|
-
return
|
|
126
|
+
return (0, types_1.createAbortError)(true);
|
|
127
|
+
}
|
|
128
|
+
if (typeof options === 'function') {
|
|
129
|
+
options = { errorQuery: options };
|
|
128
130
|
}
|
|
129
131
|
try {
|
|
130
132
|
return this.getClient(item.source).executeQuery.call(this, item, options);
|
|
@@ -135,7 +137,10 @@ class Db extends core_1.ClientDb {
|
|
|
135
137
|
}
|
|
136
138
|
async executeBatchQuery(batch, options, outResult) {
|
|
137
139
|
if (this.aborted) {
|
|
138
|
-
return
|
|
140
|
+
return (0, types_1.createAbortError)(true);
|
|
141
|
+
}
|
|
142
|
+
if (typeof options === 'function') {
|
|
143
|
+
options = { errorQuery: options };
|
|
139
144
|
}
|
|
140
145
|
try {
|
|
141
146
|
return this.getClient(batch[0].source).executeBatchQuery.call(this, batch, options, outResult);
|
|
@@ -211,7 +216,7 @@ class Db extends core_1.ClientDb {
|
|
|
211
216
|
}
|
|
212
217
|
async commit(items) {
|
|
213
218
|
if (this.aborted) {
|
|
214
|
-
return
|
|
219
|
+
return (0, types_1.createAbortError)(true);
|
|
215
220
|
}
|
|
216
221
|
const tasks = (items || this.pending).map(async (data) => {
|
|
217
222
|
data.ignoreCache ?? (data.ignoreCache = true);
|
|
@@ -225,11 +230,11 @@ class Db extends core_1.ClientDb {
|
|
|
225
230
|
readTLSCert(value, cache) {
|
|
226
231
|
if ((0, types_1.isString)(value)) {
|
|
227
232
|
let pathname = value.trim();
|
|
228
|
-
if (request_1.
|
|
233
|
+
if (request_1.isCert(pathname)) {
|
|
229
234
|
return pathname;
|
|
230
235
|
}
|
|
231
236
|
if (Db.isPath(pathname = path.resolve(pathname)) && this.canRead(pathname, { ownPermissionOnly: true })) {
|
|
232
|
-
return request_1.
|
|
237
|
+
return request_1.readCACert(pathname, cache);
|
|
233
238
|
}
|
|
234
239
|
}
|
|
235
240
|
return '';
|
|
@@ -311,9 +316,5 @@ Db.STORE_RESULT_PARTITION_SIZE = 16;
|
|
|
311
316
|
Db.STORE_RESULT_PARTITION_MULT = 2;
|
|
312
317
|
Object.freeze(types_1.DB_TYPE);
|
|
313
318
|
Object.freeze(util_1.SQL_COMMAND);
|
|
314
|
-
exports.default = Db;
|
|
315
319
|
|
|
316
|
-
|
|
317
|
-
module.exports = exports.default;
|
|
318
|
-
module.exports.default = exports.default;
|
|
319
|
-
}
|
|
320
|
+
module.exports = Db;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "DB modules for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "BSD 3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.
|
|
24
|
-
"@e-mc/request": "0.
|
|
25
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/core": "0.9.0",
|
|
24
|
+
"@e-mc/request": "0.9.0",
|
|
25
|
+
"@e-mc/types": "0.9.0"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/pool.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DbPoolConstructor } from '../types/lib/db';
|
|
2
|
-
|
|
3
|
-
declare const DbPool: DbPoolConstructor;
|
|
4
|
-
|
|
1
|
+
import type { DbPoolConstructor } from '../types/lib/db';
|
|
2
|
+
|
|
3
|
+
declare const DbPool: DbPoolConstructor;
|
|
4
|
+
|
|
5
5
|
export = DbPool;
|
package/pool.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a, _b, _c;
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
const types_1 = require("@e-mc/types");
|
|
5
4
|
const util_1 = require("@e-mc/db/util");
|
|
6
5
|
const kItems = Symbol('items');
|
|
@@ -30,17 +29,15 @@ class DbPool {
|
|
|
30
29
|
}
|
|
31
30
|
static validateKey(pools, username, uuidKey) {
|
|
32
31
|
if ((0, types_1.validateUUID)(uuidKey)) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
return [uuidKey, pool];
|
|
40
|
-
}
|
|
41
|
-
delete pools[key];
|
|
42
|
-
break;
|
|
32
|
+
for (const key in pools) {
|
|
33
|
+
const pool = pools[key];
|
|
34
|
+
const auth = pool.uuidKey;
|
|
35
|
+
if (auth?.username === username && auth.password === uuidKey) {
|
|
36
|
+
if (!pool.closed) {
|
|
37
|
+
return [uuidKey, pool];
|
|
43
38
|
}
|
|
39
|
+
delete pools[key];
|
|
40
|
+
break;
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
43
|
return [uuidKey, null];
|
|
@@ -139,9 +136,5 @@ class DbPool {
|
|
|
139
136
|
}
|
|
140
137
|
}
|
|
141
138
|
_a = kItems, _b = kParent, _c = kIdlePrevious;
|
|
142
|
-
exports.default = DbPool;
|
|
143
139
|
|
|
144
|
-
|
|
145
|
-
module.exports = exports.default;
|
|
146
|
-
module.exports.default = exports.default;
|
|
147
|
-
}
|
|
140
|
+
module.exports = DbPool;
|
package/util.d.ts
CHANGED
package/util.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
exports.hasBasicAuth = exports.getBasicAuth = exports.setUUIDKey = exports.checkEmpty = exports.parseConnectionString = exports.parseServerAuth = exports.IMPORTS = exports.SQL_COMMAND = void 0;
|
|
4
3
|
const types_1 = require("@e-mc/types");
|
|
5
4
|
const util_1 = require("@e-mc/request/util");
|