@e-mc/db 0.9.9 → 0.9.10
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 +212 -212
- package/index.js +16 -16
- package/package.json +27 -27
- package/pool.js +1 -1
- package/util.d.ts +18 -18
- 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,213 +1,213 @@
|
|
|
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.9.
|
|
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(): 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
|
-
findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
|
|
91
|
-
validateKey(pools: Record<string, IDbPool>, username: string, uuidKey: unknown): [string, Record<string, IDbPool> | null];
|
|
92
|
-
checkTimeout(pools: Record<string, IDbPool>, value: number, limit?: number): Promise<number>;
|
|
93
|
-
readonly prototype: IDbPool;
|
|
94
|
-
new(pool: unknown, poolKey: string, uuidKey?: AuthValue | null): IDbPool;
|
|
95
|
-
}
|
|
96
|
-
```
|
|
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
|
-
## References
|
|
202
|
-
|
|
203
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
204
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
205
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
206
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
207
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
208
|
-
|
|
209
|
-
* https://www.npmjs.com/package/@types/node
|
|
210
|
-
|
|
211
|
-
## LICENSE
|
|
212
|
-
|
|
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.9.10/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(): 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
|
+
findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
|
|
91
|
+
validateKey(pools: Record<string, IDbPool>, username: string, uuidKey: unknown): [string, Record<string, IDbPool> | null];
|
|
92
|
+
checkTimeout(pools: Record<string, IDbPool>, value: number, limit?: number): Promise<number>;
|
|
93
|
+
readonly prototype: IDbPool;
|
|
94
|
+
new(pool: unknown, poolKey: string, uuidKey?: AuthValue | null): IDbPool;
|
|
95
|
+
}
|
|
96
|
+
```
|
|
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
|
+
## References
|
|
202
|
+
|
|
203
|
+
- https://www.unpkg.com/@e-mc/types@0.9.10/lib/squared.d.ts
|
|
204
|
+
- https://www.unpkg.com/@e-mc/types@0.9.10/lib/core.d.ts
|
|
205
|
+
- https://www.unpkg.com/@e-mc/types@0.9.10/lib/db.d.ts
|
|
206
|
+
- https://www.unpkg.com/@e-mc/types@0.9.10/lib/http.d.ts
|
|
207
|
+
- https://www.unpkg.com/@e-mc/types@0.9.10/lib/settings.d.ts
|
|
208
|
+
|
|
209
|
+
* https://www.npmjs.com/package/@types/node
|
|
210
|
+
|
|
211
|
+
## LICENSE
|
|
212
|
+
|
|
213
213
|
BSD 3-Clause
|
package/index.js
CHANGED
|
@@ -7,14 +7,14 @@ 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(items, cache) {
|
|
@@ -219,7 +219,7 @@ class Db extends core_1.ClientDb {
|
|
|
219
219
|
return (0, types_1.createAbortError)(true);
|
|
220
220
|
}
|
|
221
221
|
const tasks = (items || this.pending).map(async (data) => {
|
|
222
|
-
data.ignoreCache
|
|
222
|
+
data.ignoreCache ??= true;
|
|
223
223
|
return this.executeQuery(data).catch(() => {
|
|
224
224
|
this.applyState([data], 16);
|
|
225
225
|
return [];
|
|
@@ -273,13 +273,13 @@ class Db extends core_1.ClientDb {
|
|
|
273
273
|
const result = uuidKey && this.settingsKey(uuidKey, 'pool');
|
|
274
274
|
if (result) {
|
|
275
275
|
if (config) {
|
|
276
|
-
result.min
|
|
277
|
-
result.max
|
|
278
|
-
result.idle
|
|
279
|
-
result.queue_max
|
|
280
|
-
result.queue_idle
|
|
281
|
-
result.timeout
|
|
282
|
-
result.socket_timeout
|
|
276
|
+
result.min ??= config.min;
|
|
277
|
+
result.max ??= config.max;
|
|
278
|
+
result.idle ??= config.idle;
|
|
279
|
+
result.queue_max ??= config.queue_max;
|
|
280
|
+
result.queue_idle ??= config.queue_idle;
|
|
281
|
+
result.timeout ??= config.timeout;
|
|
282
|
+
result.socket_timeout ??= config.socket_timeout;
|
|
283
283
|
}
|
|
284
284
|
else {
|
|
285
285
|
sanitizePoolConfig(result);
|
package/package.json
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@e-mc/db",
|
|
3
|
-
"version": "0.9.
|
|
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.9.
|
|
24
|
-
"@e-mc/request": "0.9.
|
|
25
|
-
"@e-mc/types": "0.9.
|
|
26
|
-
}
|
|
27
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@e-mc/db",
|
|
3
|
+
"version": "0.9.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 3-Clause",
|
|
21
|
+
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@e-mc/core": "0.9.10",
|
|
24
|
+
"@e-mc/request": "0.9.10",
|
|
25
|
+
"@e-mc/types": "0.9.10"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/pool.js
CHANGED
package/util.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import type { DbDataSource } from '../types/lib/squared';
|
|
2
|
-
|
|
3
|
-
import type { DbConnection, SQL_COMMAND, ServerAuth } from '../types/lib/db';
|
|
4
|
-
import type { AuthValue } from '../types/lib/http';
|
|
5
|
-
|
|
6
|
-
declare namespace util {
|
|
7
|
-
const SQL_COMMAND: SQL_COMMAND;
|
|
8
|
-
const IMPORTS: Record<string, string | undefined>;
|
|
9
|
-
function getBasicAuth(auth: AuthValue): string;
|
|
10
|
-
function getBasicAuth(username: unknown, password?: unknown): string;
|
|
11
|
-
function hasBasicAuth(value: string): boolean;
|
|
12
|
-
function parseConnectionString(value: string, scheme?: string): Null<DbConnection>;
|
|
13
|
-
function parseServerAuth(value: ServerAuth, all: boolean): ServerAuth;
|
|
14
|
-
function parseServerAuth(value: ServerAuth, port?: number, all?: boolean): ServerAuth;
|
|
15
|
-
function checkEmpty(value: unknown): boolean;
|
|
16
|
-
function setUUIDKey(item: DbDataSource, value: unknown): void;
|
|
17
|
-
}
|
|
18
|
-
|
|
1
|
+
import type { DbDataSource } from '../types/lib/squared';
|
|
2
|
+
|
|
3
|
+
import type { DbConnection, SQL_COMMAND, ServerAuth } from '../types/lib/db';
|
|
4
|
+
import type { AuthValue } from '../types/lib/http';
|
|
5
|
+
|
|
6
|
+
declare namespace util {
|
|
7
|
+
const SQL_COMMAND: SQL_COMMAND;
|
|
8
|
+
const IMPORTS: Record<string, string | undefined>;
|
|
9
|
+
function getBasicAuth(auth: AuthValue): string;
|
|
10
|
+
function getBasicAuth(username: unknown, password?: unknown): string;
|
|
11
|
+
function hasBasicAuth(value: string): boolean;
|
|
12
|
+
function parseConnectionString(value: string, scheme?: string): Null<DbConnection>;
|
|
13
|
+
function parseServerAuth(value: ServerAuth, all: boolean): ServerAuth;
|
|
14
|
+
function parseServerAuth(value: ServerAuth, port?: number, all?: boolean): ServerAuth;
|
|
15
|
+
function checkEmpty(value: unknown): boolean;
|
|
16
|
+
function setUUIDKey(item: DbDataSource, value: unknown): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
19
|
export = util;
|
package/util.js
CHANGED
|
@@ -103,12 +103,11 @@ function checkEmpty(value) {
|
|
|
103
103
|
}
|
|
104
104
|
exports.checkEmpty = checkEmpty;
|
|
105
105
|
function setUUIDKey(item, value) {
|
|
106
|
-
var _a;
|
|
107
106
|
if ((0, types_1.isString)(value)) {
|
|
108
107
|
if (!(0, types_1.isPlainObject)(item.credential)) {
|
|
109
108
|
item.credential = {};
|
|
110
109
|
}
|
|
111
|
-
|
|
110
|
+
item.credential.uuidKey ||= value;
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
113
|
exports.setUUIDKey = setUUIDKey;
|