@e-mc/db 0.10.1 → 0.10.3
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 +216 -216
- package/index.d.ts +4 -4
- package/index.js +17 -17
- package/package.json +27 -27
- package/pool.d.ts +4 -4
- package/pool.js +2 -2
- package/types/index.d.ts +13 -13
- package/util.d.ts +19 -19
- package/util.js +1 -2
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,217 +1,217 @@
|
|
|
1
|
-
# @e-mc/db
|
|
2
|
-
|
|
3
|
-
* NodeJS 16
|
|
4
|
-
* ES2020
|
|
5
|
-
|
|
6
|
-
## General Usage
|
|
7
|
-
|
|
8
|
-
* [Read the Docs](https://e-mc.readthedocs.io)
|
|
9
|
-
|
|
10
|
-
## Interface
|
|
11
|
-
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.10.
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
import type { DbDataSource } from "./squared";
|
|
16
|
-
|
|
17
|
-
import type { IHost } from "./index";
|
|
18
|
-
import type { ClientDbConstructor, IClientDb } from "./core";
|
|
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";
|
|
22
|
-
|
|
23
|
-
import type { SecureContextOptions } from "tls";
|
|
24
|
-
|
|
25
|
-
interface IDb extends IClientDb<IHost, DbModule, DbDataSource, DbSourceOptions, DbCoerceSettings> {
|
|
26
|
-
setCredential(item: DbDataSource): Promise<void>;
|
|
27
|
-
getCredential(item: DbDataSource): Record<string | number | symbol, unknown>;
|
|
28
|
-
hasSource(source: string, ...type: number[]): boolean;
|
|
29
|
-
applyCommand(...items: DbDataSource[]): void;
|
|
30
|
-
executeQuery(item: V, callback: ErrorQueryCallback): Promise<QueryResult>;
|
|
31
|
-
executeQuery(item: DbDataSource, sessionKey: string): Promise<QueryResult>;
|
|
32
|
-
executeQuery(item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
|
|
33
|
-
executeBatchQuery(batch: DbDataSource[], callback: ErrorQueryCallback, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
34
|
-
executeBatchQuery(batch: DbDataSource[], sessionKey: string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
35
|
-
executeBatchQuery(batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
36
|
-
processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], parallel: boolean): Promise<BatchQueryResult>;
|
|
37
|
-
processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], options?: ProcessRowsOptions, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
38
|
-
handleFail(err: unknown, item: DbDataSource, options?: HandleFailOptions): boolean;
|
|
39
|
-
readTLSCert(value: unknown, cache?: boolean): string;
|
|
40
|
-
readTLSConfig(options: SecureContextOptions, cache?: boolean): void;
|
|
41
|
-
settingsOf(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
|
|
42
|
-
settingsOf(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
|
|
43
|
-
settingsKey(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
|
|
44
|
-
settingsKey(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
|
|
45
|
-
getPoolConfig(source: string, uuidKey?: string): Required<PoolConfig> | undefined;
|
|
46
|
-
get sourceType(): DB_TYPE;
|
|
47
|
-
get commandType(): SQL_COMMAND;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface DbConstructor extends ClientDbConstructor<IHost> {
|
|
51
|
-
setPoolConfig(value: Record<string, PoolConfig>): void;
|
|
52
|
-
getPoolConfig(source: string): Required<PoolConfig> | undefined;
|
|
53
|
-
readonly prototype: IDb;
|
|
54
|
-
new(module?: DbModule, database?: DbDataSource[], ...args: unknown[]): IDb;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
interface IDbSourceClient {
|
|
58
|
-
DB_SOURCE_NAME: string;
|
|
59
|
-
DB_SOURCE_CLIENT: boolean;
|
|
60
|
-
DB_SOURCE_TYPE: number;
|
|
61
|
-
setCredential(this: IDb, item: DbDataSource): Promise<void>;
|
|
62
|
-
executeQuery(this: IDb, item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
|
|
63
|
-
executeBatchQuery(this: IDb, batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
64
|
-
checkTimeout?(this: IDbSourceClient, value: number, limit?: number): Promise<number>;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
interface IDbPool {
|
|
68
|
-
client: unknown;
|
|
69
|
-
lastAccessed: number;
|
|
70
|
-
success: number;
|
|
71
|
-
failed: number;
|
|
72
|
-
poolKey: string;
|
|
73
|
-
uuidKey: AuthValue | null;
|
|
74
|
-
add(item: DbDataSource, uuidKey?: string): this;
|
|
75
|
-
has(item: DbDataSource): boolean;
|
|
76
|
-
getConnection(credential?: unknown): Promise<unknown>;
|
|
77
|
-
remove(): void;
|
|
78
|
-
detach(force?: boolean): Promise<void>;
|
|
79
|
-
close(): Promise<void>;
|
|
80
|
-
isIdle(timeout: number): boolean;
|
|
81
|
-
isEmpty(): boolean;
|
|
82
|
-
set connected(value: boolean);
|
|
83
|
-
get persist(): boolean;
|
|
84
|
-
get closed(): boolean;
|
|
85
|
-
get closeable(): boolean;
|
|
86
|
-
set parent(value: Record<string, IDbPool>);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
interface DbPoolConstructor {
|
|
90
|
-
CACHE_UNUSED: readonly string[];
|
|
91
|
-
asString(credential: unknown): string;
|
|
92
|
-
sanitize(credential: unknown): unknown;
|
|
93
|
-
removeUUIDKey(credential: unknown): unknown;
|
|
94
|
-
findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
|
|
95
|
-
validateKey(pools: Record<string, IDbPool>, username: string, uuidKey: unknown): [string, Record<string, IDbPool> | null];
|
|
96
|
-
checkTimeout(pools: Record<string, IDbPool>, value: number, limit?: number): Promise<number>;
|
|
97
|
-
readonly prototype: IDbPool;
|
|
98
|
-
new(pool: unknown, poolKey: string, uuidKey?: AuthValue | null): IDbPool;
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
## Settings
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
import type { DbSourceOptions, PurgeComponent } from "./settings";
|
|
106
|
-
|
|
107
|
-
interface DbModule {
|
|
108
|
-
// handler: "@e-mc/db";
|
|
109
|
-
mariadb?: DbStoredCredentials;
|
|
110
|
-
mongodb?: DbStoredCredentials;
|
|
111
|
-
mssql?: DbStoredCredentials;
|
|
112
|
-
mysql?: DbStoredCredentials;
|
|
113
|
-
oracle?: DbStoredCredentials;
|
|
114
|
-
postgres?: DbStoredCredentials;
|
|
115
|
-
redis?: DbStoredCredentials;
|
|
116
|
-
settings?: {
|
|
117
|
-
broadcast_id?: string | string[];
|
|
118
|
-
users?: Record<string, Record<string, unknown>>;
|
|
119
|
-
cache_dir?: string;
|
|
120
|
-
session_expires?: number;
|
|
121
|
-
user_key?: Record<string, DbSourceOptions>;
|
|
122
|
-
imports?: StringMap;
|
|
123
|
-
purge?: PurgeComponent;
|
|
124
|
-
mariadb?: DbSourceOptions;
|
|
125
|
-
mongodb?: DbSourceOptions;
|
|
126
|
-
mssql?: DbSourceOptions;
|
|
127
|
-
mysql?: DbSourceOptions;
|
|
128
|
-
oracle?: DbSourceOptions;
|
|
129
|
-
postgres?: DbSourceOptions;
|
|
130
|
-
redis?: DbSourceOptions;
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
type DbStoredCredentials = Record<string, Record<string, unknown>>;
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### Example usage
|
|
138
|
-
|
|
139
|
-
```javascript
|
|
140
|
-
const Db = require("@e-mc/db"); // Using @pi-r/mongodb
|
|
141
|
-
|
|
142
|
-
const instance = new Db({
|
|
143
|
-
mongodb: {
|
|
144
|
-
main: {
|
|
145
|
-
server: "localhost:27017",
|
|
146
|
-
auth: {
|
|
147
|
-
username: "**********",
|
|
148
|
-
password: "**********"
|
|
149
|
-
},
|
|
150
|
-
authMechanism: "SCRAM-SHA-1"
|
|
151
|
-
}
|
|
152
|
-
},
|
|
153
|
-
settings: {
|
|
154
|
-
mongodb: {
|
|
155
|
-
pool: {
|
|
156
|
-
max: 10,
|
|
157
|
-
idle: 60 * 1000,
|
|
158
|
-
queue_max: 4,
|
|
159
|
-
queue_idle: 30 * 1000,
|
|
160
|
-
timeout: 10 * 1000
|
|
161
|
-
},
|
|
162
|
-
cache: {
|
|
163
|
-
timeout: "1d",
|
|
164
|
-
when_empty: false
|
|
165
|
-
},
|
|
166
|
-
coerce: {
|
|
167
|
-
credential: false,
|
|
168
|
-
options: true
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
// instance.host = new Host();
|
|
174
|
-
instance.init();
|
|
175
|
-
|
|
176
|
-
const item = {
|
|
177
|
-
source: "mongodb",
|
|
178
|
-
credential: "main",
|
|
179
|
-
table: "demo",
|
|
180
|
-
name: "nodejs",
|
|
181
|
-
query: {
|
|
182
|
-
id: {
|
|
183
|
-
"$eq": "1"
|
|
184
|
-
}
|
|
185
|
-
},
|
|
186
|
-
willAbort: true
|
|
187
|
-
};
|
|
188
|
-
await instance.setCredential(item);
|
|
189
|
-
|
|
190
|
-
const rows = await instance.executeQuery(item, (err, item) => {
|
|
191
|
-
if (err.code === "E11000") {
|
|
192
|
-
return true; // throw err;
|
|
193
|
-
}
|
|
194
|
-
return false; // return [];
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
const [rows1, rows2] = await instance.executeBatchQuery([
|
|
198
|
-
{ ...item, usePool: true },
|
|
199
|
-
{ ...item, query: { id: { "$eq": "2" } } }
|
|
200
|
-
],
|
|
201
|
-
{ parallel: true, connectOnce: true }
|
|
202
|
-
);
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
## References
|
|
206
|
-
|
|
207
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
208
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
209
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
210
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
211
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
212
|
-
|
|
213
|
-
* https://www.npmjs.com/package/@types/node
|
|
214
|
-
|
|
215
|
-
## LICENSE
|
|
216
|
-
|
|
1
|
+
# @e-mc/db
|
|
2
|
+
|
|
3
|
+
* NodeJS 16
|
|
4
|
+
* ES2020
|
|
5
|
+
|
|
6
|
+
## General Usage
|
|
7
|
+
|
|
8
|
+
* [Read the Docs](https://e-mc.readthedocs.io)
|
|
9
|
+
|
|
10
|
+
## Interface
|
|
11
|
+
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.10.3/lib/index.d.ts)
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import type { DbDataSource } from "./squared";
|
|
16
|
+
|
|
17
|
+
import type { IHost } from "./index";
|
|
18
|
+
import type { ClientDbConstructor, IClientDb } from "./core";
|
|
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";
|
|
22
|
+
|
|
23
|
+
import type { SecureContextOptions } from "tls";
|
|
24
|
+
|
|
25
|
+
interface IDb extends IClientDb<IHost, DbModule, DbDataSource, DbSourceOptions, DbCoerceSettings> {
|
|
26
|
+
setCredential(item: DbDataSource): Promise<void>;
|
|
27
|
+
getCredential(item: DbDataSource): Record<string | number | symbol, unknown>;
|
|
28
|
+
hasSource(source: string, ...type: number[]): boolean;
|
|
29
|
+
applyCommand(...items: DbDataSource[]): void;
|
|
30
|
+
executeQuery(item: V, callback: ErrorQueryCallback): Promise<QueryResult>;
|
|
31
|
+
executeQuery(item: DbDataSource, sessionKey: string): Promise<QueryResult>;
|
|
32
|
+
executeQuery(item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
|
|
33
|
+
executeBatchQuery(batch: DbDataSource[], callback: ErrorQueryCallback, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
34
|
+
executeBatchQuery(batch: DbDataSource[], sessionKey: string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
35
|
+
executeBatchQuery(batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
36
|
+
processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], parallel: boolean): Promise<BatchQueryResult>;
|
|
37
|
+
processRows(batch: DbDataSource[], tasks: Promise<QueryResult | null>[], options?: ProcessRowsOptions, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
38
|
+
handleFail(err: unknown, item: DbDataSource, options?: HandleFailOptions): boolean;
|
|
39
|
+
readTLSCert(value: unknown, cache?: boolean): string;
|
|
40
|
+
readTLSConfig(options: SecureContextOptions, cache?: boolean): void;
|
|
41
|
+
settingsOf(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
|
|
42
|
+
settingsOf(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
|
|
43
|
+
settingsKey(source: string, name: keyof Omit<DbSourceOptions, "coerce">): unknown;
|
|
44
|
+
settingsKey(source: string, name: "coerce", component: keyof DbCoerceSettings): unknown;
|
|
45
|
+
getPoolConfig(source: string, uuidKey?: string): Required<PoolConfig> | undefined;
|
|
46
|
+
get sourceType(): DB_TYPE;
|
|
47
|
+
get commandType(): SQL_COMMAND;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface DbConstructor extends ClientDbConstructor<IHost> {
|
|
51
|
+
setPoolConfig(value: Record<string, PoolConfig>): void;
|
|
52
|
+
getPoolConfig(source: string): Required<PoolConfig> | undefined;
|
|
53
|
+
readonly prototype: IDb;
|
|
54
|
+
new(module?: DbModule, database?: DbDataSource[], ...args: unknown[]): IDb;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface IDbSourceClient {
|
|
58
|
+
DB_SOURCE_NAME: string;
|
|
59
|
+
DB_SOURCE_CLIENT: boolean;
|
|
60
|
+
DB_SOURCE_TYPE: number;
|
|
61
|
+
setCredential(this: IDb, item: DbDataSource): Promise<void>;
|
|
62
|
+
executeQuery(this: IDb, item: DbDataSource, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
|
|
63
|
+
executeBatchQuery(this: IDb, batch: DbDataSource[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
64
|
+
checkTimeout?(this: IDbSourceClient, value: number, limit?: number): Promise<number>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface IDbPool {
|
|
68
|
+
client: unknown;
|
|
69
|
+
lastAccessed: number;
|
|
70
|
+
success: number;
|
|
71
|
+
failed: number;
|
|
72
|
+
poolKey: string;
|
|
73
|
+
uuidKey: AuthValue | null;
|
|
74
|
+
add(item: DbDataSource, uuidKey?: string): this;
|
|
75
|
+
has(item: DbDataSource): boolean;
|
|
76
|
+
getConnection(credential?: unknown): Promise<unknown>;
|
|
77
|
+
remove(): void;
|
|
78
|
+
detach(force?: boolean): Promise<void>;
|
|
79
|
+
close(): Promise<void>;
|
|
80
|
+
isIdle(timeout: number): boolean;
|
|
81
|
+
isEmpty(): boolean;
|
|
82
|
+
set connected(value: boolean);
|
|
83
|
+
get persist(): boolean;
|
|
84
|
+
get closed(): boolean;
|
|
85
|
+
get closeable(): boolean;
|
|
86
|
+
set parent(value: Record<string, IDbPool>);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface DbPoolConstructor {
|
|
90
|
+
CACHE_UNUSED: readonly string[];
|
|
91
|
+
asString(credential: unknown): string;
|
|
92
|
+
sanitize(credential: unknown): unknown;
|
|
93
|
+
removeUUIDKey(credential: unknown): unknown;
|
|
94
|
+
findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
|
|
95
|
+
validateKey(pools: Record<string, IDbPool>, username: string, uuidKey: unknown): [string, Record<string, IDbPool> | null];
|
|
96
|
+
checkTimeout(pools: Record<string, IDbPool>, value: number, limit?: number): Promise<number>;
|
|
97
|
+
readonly prototype: IDbPool;
|
|
98
|
+
new(pool: unknown, poolKey: string, uuidKey?: AuthValue | null): IDbPool;
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Settings
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import type { DbSourceOptions, PurgeComponent } from "./settings";
|
|
106
|
+
|
|
107
|
+
interface DbModule {
|
|
108
|
+
// handler: "@e-mc/db";
|
|
109
|
+
mariadb?: DbStoredCredentials;
|
|
110
|
+
mongodb?: DbStoredCredentials;
|
|
111
|
+
mssql?: DbStoredCredentials;
|
|
112
|
+
mysql?: DbStoredCredentials;
|
|
113
|
+
oracle?: DbStoredCredentials;
|
|
114
|
+
postgres?: DbStoredCredentials;
|
|
115
|
+
redis?: DbStoredCredentials;
|
|
116
|
+
settings?: {
|
|
117
|
+
broadcast_id?: string | string[];
|
|
118
|
+
users?: Record<string, Record<string, unknown>>;
|
|
119
|
+
cache_dir?: string;
|
|
120
|
+
session_expires?: number;
|
|
121
|
+
user_key?: Record<string, DbSourceOptions>;
|
|
122
|
+
imports?: StringMap;
|
|
123
|
+
purge?: PurgeComponent;
|
|
124
|
+
mariadb?: DbSourceOptions;
|
|
125
|
+
mongodb?: DbSourceOptions;
|
|
126
|
+
mssql?: DbSourceOptions;
|
|
127
|
+
mysql?: DbSourceOptions;
|
|
128
|
+
oracle?: DbSourceOptions;
|
|
129
|
+
postgres?: DbSourceOptions;
|
|
130
|
+
redis?: DbSourceOptions;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
type DbStoredCredentials = Record<string, Record<string, unknown>>;
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Example usage
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
const Db = require("@e-mc/db"); // Using @pi-r/mongodb
|
|
141
|
+
|
|
142
|
+
const instance = new Db({
|
|
143
|
+
mongodb: {
|
|
144
|
+
main: {
|
|
145
|
+
server: "localhost:27017",
|
|
146
|
+
auth: {
|
|
147
|
+
username: "**********",
|
|
148
|
+
password: "**********"
|
|
149
|
+
},
|
|
150
|
+
authMechanism: "SCRAM-SHA-1"
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
settings: {
|
|
154
|
+
mongodb: {
|
|
155
|
+
pool: {
|
|
156
|
+
max: 10,
|
|
157
|
+
idle: 60 * 1000,
|
|
158
|
+
queue_max: 4,
|
|
159
|
+
queue_idle: 30 * 1000,
|
|
160
|
+
timeout: 10 * 1000
|
|
161
|
+
},
|
|
162
|
+
cache: {
|
|
163
|
+
timeout: "1d",
|
|
164
|
+
when_empty: false
|
|
165
|
+
},
|
|
166
|
+
coerce: {
|
|
167
|
+
credential: false,
|
|
168
|
+
options: true
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
// instance.host = new Host();
|
|
174
|
+
instance.init();
|
|
175
|
+
|
|
176
|
+
const item = {
|
|
177
|
+
source: "mongodb",
|
|
178
|
+
credential: "main",
|
|
179
|
+
table: "demo",
|
|
180
|
+
name: "nodejs",
|
|
181
|
+
query: {
|
|
182
|
+
id: {
|
|
183
|
+
"$eq": "1"
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
willAbort: true
|
|
187
|
+
};
|
|
188
|
+
await instance.setCredential(item);
|
|
189
|
+
|
|
190
|
+
const rows = await instance.executeQuery(item, (err, item) => {
|
|
191
|
+
if (err.code === "E11000") {
|
|
192
|
+
return true; // throw err;
|
|
193
|
+
}
|
|
194
|
+
return false; // return [];
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const [rows1, rows2] = await instance.executeBatchQuery([
|
|
198
|
+
{ ...item, usePool: true },
|
|
199
|
+
{ ...item, query: { id: { "$eq": "2" } } }
|
|
200
|
+
],
|
|
201
|
+
{ parallel: true, connectOnce: true }
|
|
202
|
+
);
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## References
|
|
206
|
+
|
|
207
|
+
- https://www.unpkg.com/@e-mc/types@0.10.3/lib/squared.d.ts
|
|
208
|
+
- https://www.unpkg.com/@e-mc/types@0.10.3/lib/core.d.ts
|
|
209
|
+
- https://www.unpkg.com/@e-mc/types@0.10.3/lib/db.d.ts
|
|
210
|
+
- https://www.unpkg.com/@e-mc/types@0.10.3/lib/http.d.ts
|
|
211
|
+
- https://www.unpkg.com/@e-mc/types@0.10.3/lib/settings.d.ts
|
|
212
|
+
|
|
213
|
+
* https://www.npmjs.com/package/@types/node
|
|
214
|
+
|
|
215
|
+
## LICENSE
|
|
216
|
+
|
|
217
217
|
BSD 3-Clause
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DbConstructor } from '
|
|
2
|
-
|
|
3
|
-
declare const Db: DbConstructor;
|
|
4
|
-
|
|
1
|
+
import type { DbConstructor } from '@e-mc/types/lib';
|
|
2
|
+
|
|
3
|
+
declare const Db: DbConstructor;
|
|
4
|
+
|
|
5
5
|
export = Db;
|
package/index.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const path = require("path");
|
|
3
|
+
const types_1 = require("@e-mc/types");
|
|
3
4
|
const core_1 = require("@e-mc/core");
|
|
4
5
|
const request_1 = require("@e-mc/request");
|
|
5
|
-
const types_1 = require("@e-mc/types");
|
|
6
6
|
const util_1 = require("@e-mc/db/util");
|
|
7
7
|
const DB_CLIENT = new Map();
|
|
8
8
|
const POOL_CONFIG = new Map();
|
|
9
9
|
function sanitizePoolConfig(value) {
|
|
10
|
-
value.min
|
|
11
|
-
value.max
|
|
12
|
-
value.idle
|
|
13
|
-
value.queue_max
|
|
14
|
-
value.queue_idle
|
|
15
|
-
value.purge
|
|
16
|
-
value.timeout
|
|
17
|
-
value.socket_timeout
|
|
10
|
+
value.min ??= -1;
|
|
11
|
+
value.max ??= -1;
|
|
12
|
+
value.idle ??= -1;
|
|
13
|
+
value.queue_max ??= -1;
|
|
14
|
+
value.queue_idle ??= -1;
|
|
15
|
+
value.purge ??= 0;
|
|
16
|
+
value.timeout ??= -1;
|
|
17
|
+
value.socket_timeout ??= -1;
|
|
18
18
|
return value;
|
|
19
19
|
}
|
|
20
20
|
function setCert(instance, items, cache) {
|
|
@@ -215,7 +215,7 @@ class Db extends core_1.ClientDb {
|
|
|
215
215
|
return (0, types_1.createAbortError)(true);
|
|
216
216
|
}
|
|
217
217
|
const tasks = (items || this.pending).map(async (data) => {
|
|
218
|
-
data.ignoreCache
|
|
218
|
+
data.ignoreCache ??= true;
|
|
219
219
|
return this.executeQuery(data).catch(() => {
|
|
220
220
|
this.applyState([data], 16);
|
|
221
221
|
return [];
|
|
@@ -269,13 +269,13 @@ class Db extends core_1.ClientDb {
|
|
|
269
269
|
const result = uuidKey && this.settingsKey(uuidKey, 'pool');
|
|
270
270
|
if (result) {
|
|
271
271
|
if (config) {
|
|
272
|
-
result.min
|
|
273
|
-
result.max
|
|
274
|
-
result.idle
|
|
275
|
-
result.queue_max
|
|
276
|
-
result.queue_idle
|
|
277
|
-
result.timeout
|
|
278
|
-
result.socket_timeout
|
|
272
|
+
result.min ??= config.min;
|
|
273
|
+
result.max ??= config.max;
|
|
274
|
+
result.idle ??= config.idle;
|
|
275
|
+
result.queue_max ??= config.queue_max;
|
|
276
|
+
result.queue_idle ??= config.queue_idle;
|
|
277
|
+
result.timeout ??= config.timeout;
|
|
278
|
+
result.socket_timeout ??= config.socket_timeout;
|
|
279
279
|
}
|
|
280
280
|
else {
|
|
281
281
|
sanitizePoolConfig(result);
|
package/package.json
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@e-mc/db",
|
|
3
|
-
"version": "0.10.
|
|
4
|
-
"description": "DB modules for E-mc.",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"types": "index.d.ts",
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/anpham6/e-mc.git",
|
|
13
|
-
"directory": "src/db"
|
|
14
|
-
},
|
|
15
|
-
"keywords": [
|
|
16
|
-
"squared",
|
|
17
|
-
"squared-functions"
|
|
18
|
-
],
|
|
19
|
-
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
-
"license": "BSD
|
|
21
|
-
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.10.
|
|
24
|
-
"@e-mc/request": "0.10.
|
|
25
|
-
"@e-mc/types": "0.10.
|
|
26
|
-
}
|
|
27
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@e-mc/db",
|
|
3
|
+
"version": "0.10.3",
|
|
4
|
+
"description": "DB modules for E-mc.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/e-mc.git",
|
|
13
|
+
"directory": "src/db"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"squared",
|
|
17
|
+
"squared-functions"
|
|
18
|
+
],
|
|
19
|
+
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
+
"license": "BSD-3-Clause",
|
|
21
|
+
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@e-mc/core": "0.10.3",
|
|
24
|
+
"@e-mc/request": "0.10.3",
|
|
25
|
+
"@e-mc/types": "0.10.3"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/pool.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DbPoolConstructor } from '
|
|
2
|
-
|
|
3
|
-
declare const DbPool: DbPoolConstructor;
|
|
4
|
-
|
|
1
|
+
import type { DbPoolConstructor } from '@e-mc/types/lib/db';
|
|
2
|
+
|
|
3
|
+
declare const DbPool: DbPoolConstructor;
|
|
4
|
+
|
|
5
5
|
export = DbPool;
|
package/pool.js
CHANGED
|
@@ -16,7 +16,7 @@ class DbPool {
|
|
|
16
16
|
for (let i = 0, length = properties.length; i < length; ++i) {
|
|
17
17
|
const attr = properties[i];
|
|
18
18
|
if (attr in credential) {
|
|
19
|
-
result
|
|
19
|
+
result ||= { ...credential };
|
|
20
20
|
result[attr] = undefined;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -93,7 +93,7 @@ class DbPool {
|
|
|
93
93
|
}
|
|
94
94
|
add(item, uuidKey) {
|
|
95
95
|
this[kItems].add(item);
|
|
96
|
-
if (uuidKey
|
|
96
|
+
if (uuidKey ||= this.uuidKey?.password) {
|
|
97
97
|
(0, util_1.setUUIDKey)(item, uuidKey);
|
|
98
98
|
}
|
|
99
99
|
this.lastAccessed = Date.now();
|
package/types/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import type { DbDataSource } from '
|
|
2
|
-
|
|
3
|
-
import type { IDb } from '
|
|
4
|
-
import type { BatchQueryResult, ExecuteBatchQueryOptions, ExecuteQueryOptions, QueryResult } from '
|
|
5
|
-
|
|
6
|
-
export interface IDbSourceClient<T extends DbDataSource = DbDataSource> {
|
|
7
|
-
DB_SOURCE_NAME: string;
|
|
8
|
-
DB_SOURCE_CLIENT: boolean;
|
|
9
|
-
DB_SOURCE_TYPE: number;
|
|
10
|
-
setCredential(this: IDb, item: T): Promise<void>;
|
|
11
|
-
executeQuery(this: IDb, item: T, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
|
|
12
|
-
executeBatchQuery(this: IDb, batch: T[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
13
|
-
checkTimeout?(this: IDbSourceClient, value: number, limit?: number): Promise<number>;
|
|
1
|
+
import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
2
|
+
|
|
3
|
+
import type { IDb } from '@e-mc/types/lib';
|
|
4
|
+
import type { BatchQueryResult, ExecuteBatchQueryOptions, ExecuteQueryOptions, QueryResult } from '@e-mc/types/lib/db';
|
|
5
|
+
|
|
6
|
+
export interface IDbSourceClient<T extends DbDataSource = DbDataSource> {
|
|
7
|
+
DB_SOURCE_NAME: string;
|
|
8
|
+
DB_SOURCE_CLIENT: boolean;
|
|
9
|
+
DB_SOURCE_TYPE: number;
|
|
10
|
+
setCredential(this: IDb, item: T): Promise<void>;
|
|
11
|
+
executeQuery(this: IDb, item: T, options?: ExecuteQueryOptions | string): Promise<QueryResult>;
|
|
12
|
+
executeBatchQuery(this: IDb, batch: T[], options?: ExecuteBatchQueryOptions | string, outResult?: BatchQueryResult): Promise<BatchQueryResult>;
|
|
13
|
+
checkTimeout?(this: IDbSourceClient, value: number, limit?: number): Promise<number>;
|
|
14
14
|
}
|
package/util.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import type { DbDataSource } from '
|
|
2
|
-
|
|
3
|
-
import type { DbConnection, SQL_COMMAND, ServerAuth } from '
|
|
4
|
-
import type { AuthValue } from '
|
|
5
|
-
|
|
6
|
-
declare namespace util {
|
|
7
|
-
const SQL_COMMAND: SQL_COMMAND;
|
|
8
|
-
/** @deprecated Types.IMPORT_MAP */
|
|
9
|
-
const IMPORTS: Record<string, string | undefined>;
|
|
10
|
-
function getBasicAuth(auth: AuthValue): string;
|
|
11
|
-
function getBasicAuth(username: unknown, password?: unknown): string;
|
|
12
|
-
function hasBasicAuth(value: string): boolean;
|
|
13
|
-
function parseConnectionString(value: string, scheme?: string): Null<DbConnection>;
|
|
14
|
-
function parseServerAuth(value: ServerAuth, all: boolean): ServerAuth;
|
|
15
|
-
function parseServerAuth(value: ServerAuth, port?: number, all?: boolean): ServerAuth;
|
|
16
|
-
function checkEmpty(value: unknown): boolean;
|
|
17
|
-
function setUUIDKey(item: DbDataSource, value: unknown): void;
|
|
18
|
-
}
|
|
19
|
-
|
|
1
|
+
import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
2
|
+
|
|
3
|
+
import type { DbConnection, SQL_COMMAND, ServerAuth } from '@e-mc/types/lib/db';
|
|
4
|
+
import type { AuthValue } from '@e-mc/types/lib/http';
|
|
5
|
+
|
|
6
|
+
declare namespace util {
|
|
7
|
+
const SQL_COMMAND: SQL_COMMAND;
|
|
8
|
+
/** @deprecated Types.IMPORT_MAP */
|
|
9
|
+
const IMPORTS: Record<string, string | undefined>;
|
|
10
|
+
function getBasicAuth(auth: AuthValue): string;
|
|
11
|
+
function getBasicAuth(username: unknown, password?: unknown): string;
|
|
12
|
+
function hasBasicAuth(value: string): boolean;
|
|
13
|
+
function parseConnectionString(value: string, scheme?: string): Null<DbConnection>;
|
|
14
|
+
function parseServerAuth(value: ServerAuth, all: boolean): ServerAuth;
|
|
15
|
+
function parseServerAuth(value: ServerAuth, port?: number, all?: boolean): ServerAuth;
|
|
16
|
+
function checkEmpty(value: unknown): boolean;
|
|
17
|
+
function setUUIDKey(item: DbDataSource, value: unknown): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
20
|
export = util;
|
package/util.js
CHANGED
|
@@ -96,11 +96,10 @@ function checkEmpty(value) {
|
|
|
96
96
|
return value === undefined || value === null ? false : value === 0 || (value instanceof Set || value instanceof Map ? value.size === 0 : (Array.isArray(value) || (0, types_1.isObject)(value)) && value.length === 0);
|
|
97
97
|
}
|
|
98
98
|
function setUUIDKey(item, value) {
|
|
99
|
-
var _a;
|
|
100
99
|
if ((0, types_1.isString)(value)) {
|
|
101
100
|
if (!(0, types_1.isPlainObject)(item.credential)) {
|
|
102
101
|
item.credential = {};
|
|
103
102
|
}
|
|
104
|
-
|
|
103
|
+
item.credential.uuidKey ||= value;
|
|
105
104
|
}
|
|
106
105
|
}
|