@simplysm/orm-node 13.0.69 → 13.0.70
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/README.md +20 -476
- package/dist/connections/mssql-db-conn.d.ts +5 -5
- package/dist/connections/mssql-db-conn.js +12 -12
- package/dist/connections/mssql-db-conn.js.map +1 -1
- package/dist/connections/mysql-db-conn.d.ts +3 -3
- package/dist/connections/mysql-db-conn.js +9 -9
- package/dist/connections/mysql-db-conn.js.map +1 -1
- package/dist/connections/postgresql-db-conn.d.ts +3 -3
- package/dist/connections/postgresql-db-conn.js +6 -6
- package/dist/connections/postgresql-db-conn.js.map +1 -1
- package/dist/create-db-conn.d.ts +5 -5
- package/dist/create-db-conn.js +1 -1
- package/dist/create-orm.d.ts +18 -18
- package/dist/node-db-context-executor.d.ts +30 -30
- package/dist/node-db-context-executor.js +28 -28
- package/dist/pooled-db-conn.d.ts +27 -27
- package/dist/pooled-db-conn.js +29 -29
- package/dist/pooled-db-conn.js.map +1 -1
- package/dist/types/db-conn.d.ts +47 -47
- package/dist/types/db-conn.d.ts.map +1 -1
- package/dist/types/db-conn.js +2 -2
- package/package.json +5 -5
- package/src/connections/mssql-db-conn.ts +18 -18
- package/src/connections/mysql-db-conn.ts +25 -25
- package/src/connections/postgresql-db-conn.ts +14 -14
- package/src/create-db-conn.ts +17 -17
- package/src/create-orm.ts +20 -20
- package/src/node-db-context-executor.ts +34 -34
- package/src/pooled-db-conn.ts +42 -42
- package/src/types/db-conn.ts +48 -48
package/src/pooled-db-conn.ts
CHANGED
|
@@ -7,13 +7,13 @@ import { DB_CONN_ERRORS, type DbConn, type DbConnConfig } from "./types/db-conn"
|
|
|
7
7
|
const logger = consola.withTag("pooled-db-conn");
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* DB connection wrapper managed by connection pool
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* Supports connection pooling using the generic-pool library.
|
|
13
|
+
* Acquires and returns actual physical connections from/to the pool.
|
|
14
14
|
*/
|
|
15
15
|
export class PooledDbConn extends EventEmitter<{ close: void }> implements DbConn {
|
|
16
|
-
//
|
|
16
|
+
// Actual physical connection borrowed from pool
|
|
17
17
|
private _rawConn?: DbConn;
|
|
18
18
|
|
|
19
19
|
constructor(
|
|
@@ -40,16 +40,16 @@ export class PooledDbConn extends EventEmitter<{ close: void }> implements DbCon
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* Acquire DB connection from pool
|
|
44
44
|
*
|
|
45
|
-
* @throws {SdError}
|
|
45
|
+
* @throws {SdError} When already connected
|
|
46
46
|
*/
|
|
47
47
|
async connect(): Promise<void> {
|
|
48
48
|
if (this._rawConn != null) {
|
|
49
49
|
throw new SdError(DB_CONN_ERRORS.ALREADY_CONNECTED);
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
// 1.
|
|
52
|
+
// 1. Acquire connection from pool
|
|
53
53
|
try {
|
|
54
54
|
this._rawConn = await this._pool.acquire();
|
|
55
55
|
} catch (err) {
|
|
@@ -57,57 +57,57 @@ export class PooledDbConn extends EventEmitter<{ close: void }> implements DbCon
|
|
|
57
57
|
const cause = this._getLastCreateError?.() ?? (err instanceof Error ? err : undefined);
|
|
58
58
|
throw new SdError(
|
|
59
59
|
...(cause != null ? [cause] : []),
|
|
60
|
-
`DB
|
|
60
|
+
`DB connection failed [${dialect}://${host}:${port ?? ""}/${database ?? ""}]`,
|
|
61
61
|
);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
// 2.
|
|
65
|
-
//
|
|
64
|
+
// 2. Register listener to handle physical connection loss (timeout, etc.)
|
|
65
|
+
// If connection disconnects while in use, PooledDbConn must emit close event
|
|
66
66
|
this._rawConn.on("close", this._onRawConnClose);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
|
-
*
|
|
70
|
+
* Return DB connection to pool (does not terminate actual connection)
|
|
71
71
|
*/
|
|
72
72
|
async close(): Promise<void> {
|
|
73
73
|
if (this._rawConn != null) {
|
|
74
|
-
// 1.
|
|
74
|
+
// 1. If transaction is in progress, rollback to return clean state to pool
|
|
75
75
|
if (this._rawConn.isInTransaction) {
|
|
76
76
|
try {
|
|
77
77
|
await this._rawConn.rollbackTransaction();
|
|
78
78
|
} catch (err) {
|
|
79
|
-
//
|
|
80
|
-
logger.warn("
|
|
79
|
+
// Log failure and continue (connection may already be disconnected)
|
|
80
|
+
logger.warn("Rollback failed when returning to pool", err instanceof Error ? err.message : String(err));
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
// 2.
|
|
84
|
+
// 2. Remove listener (so it won't affect reuse by other wrappers when returned to pool)
|
|
85
85
|
this._rawConn.off("close", this._onRawConnClose);
|
|
86
86
|
|
|
87
|
-
// 3.
|
|
87
|
+
// 3. Return connection to pool (does not actually close it)
|
|
88
88
|
await this._pool.release(this._rawConn);
|
|
89
89
|
this._rawConn = undefined;
|
|
90
90
|
|
|
91
|
-
// 4.
|
|
91
|
+
// 4. Notify consumer that connection is logically closed
|
|
92
92
|
this.emit("close");
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
//
|
|
96
|
+
// Handler for physical connection loss
|
|
97
97
|
private readonly _onRawConnClose = () => {
|
|
98
|
-
//
|
|
98
|
+
// Remove reference since physical connection is lost (will be filtered during pool validation)
|
|
99
99
|
this._rawConn = undefined;
|
|
100
|
-
//
|
|
100
|
+
// Notify consumer
|
|
101
101
|
this.emit("close");
|
|
102
102
|
};
|
|
103
103
|
|
|
104
|
-
// ---
|
|
104
|
+
// --- Below are delegation methods ---
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
|
-
*
|
|
107
|
+
* Begin transaction
|
|
108
108
|
*
|
|
109
|
-
* @param isolationLevel -
|
|
110
|
-
* @throws {SdError}
|
|
109
|
+
* @param isolationLevel - Transaction isolation level
|
|
110
|
+
* @throws {SdError} When connection is not acquired
|
|
111
111
|
*/
|
|
112
112
|
async beginTransaction(isolationLevel?: IsolationLevel): Promise<void> {
|
|
113
113
|
const conn = this._requireRawConn();
|
|
@@ -115,9 +115,9 @@ export class PooledDbConn extends EventEmitter<{ close: void }> implements DbCon
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
|
-
*
|
|
118
|
+
* Commit transaction
|
|
119
119
|
*
|
|
120
|
-
* @throws {SdError}
|
|
120
|
+
* @throws {SdError} When connection is not acquired
|
|
121
121
|
*/
|
|
122
122
|
async commitTransaction(): Promise<void> {
|
|
123
123
|
const conn = this._requireRawConn();
|
|
@@ -125,9 +125,9 @@ export class PooledDbConn extends EventEmitter<{ close: void }> implements DbCon
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
/**
|
|
128
|
-
*
|
|
128
|
+
* Rollback transaction
|
|
129
129
|
*
|
|
130
|
-
* @throws {SdError}
|
|
130
|
+
* @throws {SdError} When connection is not acquired
|
|
131
131
|
*/
|
|
132
132
|
async rollbackTransaction(): Promise<void> {
|
|
133
133
|
const conn = this._requireRawConn();
|
|
@@ -135,11 +135,11 @@ export class PooledDbConn extends EventEmitter<{ close: void }> implements DbCon
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
|
-
* SQL
|
|
138
|
+
* Execute SQL query
|
|
139
139
|
*
|
|
140
|
-
* @param queries -
|
|
141
|
-
* @returns
|
|
142
|
-
* @throws {SdError}
|
|
140
|
+
* @param queries - SQL query array to execute
|
|
141
|
+
* @returns Result array for each query
|
|
142
|
+
* @throws {SdError} When connection is not acquired
|
|
143
143
|
*/
|
|
144
144
|
async execute(queries: string[]): Promise<Record<string, unknown>[][]> {
|
|
145
145
|
const conn = this._requireRawConn();
|
|
@@ -147,12 +147,12 @@ export class PooledDbConn extends EventEmitter<{ close: void }> implements DbCon
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
/**
|
|
150
|
-
*
|
|
150
|
+
* Execute parameterized SQL query
|
|
151
151
|
*
|
|
152
|
-
* @param query - SQL
|
|
153
|
-
* @param params -
|
|
154
|
-
* @returns
|
|
155
|
-
* @throws {SdError}
|
|
152
|
+
* @param query - SQL query string
|
|
153
|
+
* @param params - Query parameter array
|
|
154
|
+
* @returns Query result array
|
|
155
|
+
* @throws {SdError} When connection is not acquired
|
|
156
156
|
*/
|
|
157
157
|
async executeParametrized(
|
|
158
158
|
query: string,
|
|
@@ -163,12 +163,12 @@ export class PooledDbConn extends EventEmitter<{ close: void }> implements DbCon
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
/**
|
|
166
|
-
*
|
|
166
|
+
* Bulk insert data (using native bulk API)
|
|
167
167
|
*
|
|
168
|
-
* @param tableName -
|
|
169
|
-
* @param columnMetas -
|
|
170
|
-
* @param records -
|
|
171
|
-
* @throws {SdError}
|
|
168
|
+
* @param tableName - Target table name
|
|
169
|
+
* @param columnMetas - Column metadata
|
|
170
|
+
* @param records - Record array to insert
|
|
171
|
+
* @throws {SdError} When connection is not acquired
|
|
172
172
|
*/
|
|
173
173
|
async bulkInsert(
|
|
174
174
|
tableName: string,
|
package/src/types/db-conn.ts
CHANGED
|
@@ -2,25 +2,25 @@ import type { EventEmitter } from "@simplysm/core-common";
|
|
|
2
2
|
import type { ColumnMeta, Dialect, IsolationLevel } from "@simplysm/orm-common";
|
|
3
3
|
|
|
4
4
|
// ============================================
|
|
5
|
-
//
|
|
5
|
+
// Common constants
|
|
6
6
|
// ============================================
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* DB
|
|
9
|
+
* DB connection establishment timeout (10 seconds)
|
|
10
10
|
*/
|
|
11
11
|
export const DB_CONN_CONNECT_TIMEOUT = 10 * 1000;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* DB
|
|
14
|
+
* DB query default timeout (10 minutes)
|
|
15
15
|
*/
|
|
16
16
|
export const DB_CONN_DEFAULT_TIMEOUT = 10 * 60 * 1000;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* DB
|
|
19
|
+
* DB connection error messages
|
|
20
20
|
*/
|
|
21
21
|
export const DB_CONN_ERRORS = {
|
|
22
|
-
NOT_CONNECTED: "'Connection'
|
|
23
|
-
ALREADY_CONNECTED: "
|
|
22
|
+
NOT_CONNECTED: "'Connection' is not connected.",
|
|
23
|
+
ALREADY_CONNECTED: "'Connection' is already connected.",
|
|
24
24
|
} as const;
|
|
25
25
|
|
|
26
26
|
// ============================================
|
|
@@ -28,86 +28,86 @@ export const DB_CONN_ERRORS = {
|
|
|
28
28
|
// ============================================
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* Low-level DB connection interface
|
|
32
32
|
*
|
|
33
|
-
*
|
|
34
|
-
* - {@link MysqlDbConn} - MySQL
|
|
35
|
-
* - {@link MssqlDbConn} - MSSQL
|
|
36
|
-
* - {@link PostgresqlDbConn} - PostgreSQL
|
|
33
|
+
* Implementations for each DBMS implement this interface.
|
|
34
|
+
* - {@link MysqlDbConn} - MySQL connection
|
|
35
|
+
* - {@link MssqlDbConn} - MSSQL connection
|
|
36
|
+
* - {@link PostgresqlDbConn} - PostgreSQL connection
|
|
37
37
|
*
|
|
38
38
|
* @remarks
|
|
39
|
-
*
|
|
39
|
+
* Inherits from EventEmitter and emits 'close' events.
|
|
40
40
|
*/
|
|
41
41
|
export interface DbConn extends EventEmitter<{ close: void }> {
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* Connection configuration
|
|
44
44
|
*/
|
|
45
45
|
config: DbConnConfig;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* Whether connected
|
|
49
49
|
*/
|
|
50
50
|
isConnected: boolean;
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
53
|
+
* Whether transaction is in progress
|
|
54
54
|
*/
|
|
55
55
|
isInTransaction: boolean;
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
* DB
|
|
58
|
+
* Establish DB connection
|
|
59
59
|
*/
|
|
60
60
|
connect(): Promise<void>;
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
|
-
* DB
|
|
63
|
+
* Close DB connection
|
|
64
64
|
*/
|
|
65
65
|
close(): Promise<void>;
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
*
|
|
68
|
+
* Begin transaction
|
|
69
69
|
*
|
|
70
|
-
* @param isolationLevel -
|
|
70
|
+
* @param isolationLevel - Isolation level (optional)
|
|
71
71
|
*/
|
|
72
72
|
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
|
-
*
|
|
75
|
+
* Commit transaction
|
|
76
76
|
*/
|
|
77
77
|
commitTransaction(): Promise<void>;
|
|
78
78
|
|
|
79
79
|
/**
|
|
80
|
-
*
|
|
80
|
+
* Rollback transaction
|
|
81
81
|
*/
|
|
82
82
|
rollbackTransaction(): Promise<void>;
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
|
-
* SQL
|
|
85
|
+
* Execute SQL query array
|
|
86
86
|
*
|
|
87
|
-
* @param queries -
|
|
88
|
-
* @returns
|
|
87
|
+
* @param queries - SQL string array to execute
|
|
88
|
+
* @returns Array of result arrays for each query
|
|
89
89
|
*/
|
|
90
90
|
execute(queries: string[]): Promise<Record<string, unknown>[][]>;
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
*
|
|
93
|
+
* Execute parameterized query
|
|
94
94
|
*
|
|
95
|
-
* @param query - SQL
|
|
96
|
-
* @param params -
|
|
97
|
-
* @returns
|
|
95
|
+
* @param query - SQL query string
|
|
96
|
+
* @param params - Binding parameters (optional)
|
|
97
|
+
* @returns Array of result arrays
|
|
98
98
|
*/
|
|
99
99
|
executeParametrized(query: string, params?: unknown[]): Promise<Record<string, unknown>[][]>;
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
102
|
+
* Bulk INSERT (using native bulk API)
|
|
103
103
|
*
|
|
104
104
|
* - MSSQL: tedious BulkLoad
|
|
105
|
-
* - MySQL: LOAD DATA LOCAL INFILE (
|
|
105
|
+
* - MySQL: LOAD DATA LOCAL INFILE (temporary file)
|
|
106
106
|
* - PostgreSQL: COPY FROM STDIN
|
|
107
107
|
*
|
|
108
|
-
* @param tableName -
|
|
109
|
-
* @param columnMetas -
|
|
110
|
-
* @param records -
|
|
108
|
+
* @param tableName - Table name (database.table or database.schema.table)
|
|
109
|
+
* @param columnMetas - Column name → ColumnMeta mapping
|
|
110
|
+
* @param records - Record array to insert
|
|
111
111
|
*/
|
|
112
112
|
bulkInsert(
|
|
113
113
|
tableName: string,
|
|
@@ -121,33 +121,33 @@ export interface DbConn extends EventEmitter<{ close: void }> {
|
|
|
121
121
|
// ============================================
|
|
122
122
|
|
|
123
123
|
/**
|
|
124
|
-
*
|
|
124
|
+
* Connection pool configuration
|
|
125
125
|
*
|
|
126
126
|
* @remarks
|
|
127
|
-
*
|
|
128
|
-
* - min: 1 (
|
|
129
|
-
* - max: 10 (
|
|
130
|
-
* - acquireTimeoutMillis: 30000 (
|
|
131
|
-
* - idleTimeoutMillis: 30000 (
|
|
127
|
+
* Default values for each setting:
|
|
128
|
+
* - min: 1 (minimum connection count)
|
|
129
|
+
* - max: 10 (maximum connection count)
|
|
130
|
+
* - acquireTimeoutMillis: 30000 (connection acquisition timeout)
|
|
131
|
+
* - idleTimeoutMillis: 30000 (idle connection timeout)
|
|
132
132
|
*/
|
|
133
133
|
export interface DbPoolConfig {
|
|
134
|
-
/**
|
|
134
|
+
/** Minimum connection count (default: 1) */
|
|
135
135
|
min?: number;
|
|
136
|
-
/**
|
|
136
|
+
/** Maximum connection count (default: 10) */
|
|
137
137
|
max?: number;
|
|
138
|
-
/**
|
|
138
|
+
/** Connection acquisition timeout (milliseconds, default: 30000) */
|
|
139
139
|
acquireTimeoutMillis?: number;
|
|
140
|
-
/**
|
|
140
|
+
/** Idle connection timeout (milliseconds, default: 30000) */
|
|
141
141
|
idleTimeoutMillis?: number;
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
|
-
* DB
|
|
145
|
+
* DB connection configuration type (branching by dialect)
|
|
146
146
|
*/
|
|
147
147
|
export type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
|
|
148
148
|
|
|
149
149
|
/**
|
|
150
|
-
* MySQL
|
|
150
|
+
* MySQL connection configuration
|
|
151
151
|
*/
|
|
152
152
|
export interface MysqlDbConnConfig {
|
|
153
153
|
dialect: "mysql";
|
|
@@ -161,7 +161,7 @@ export interface MysqlDbConnConfig {
|
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
/**
|
|
164
|
-
* MSSQL
|
|
164
|
+
* MSSQL connection configuration
|
|
165
165
|
*/
|
|
166
166
|
export interface MssqlDbConnConfig {
|
|
167
167
|
dialect: "mssql" | "mssql-azure";
|
|
@@ -176,7 +176,7 @@ export interface MssqlDbConnConfig {
|
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
/**
|
|
179
|
-
* PostgreSQL
|
|
179
|
+
* PostgreSQL connection configuration
|
|
180
180
|
*/
|
|
181
181
|
export interface PostgresqlDbConnConfig {
|
|
182
182
|
dialect: "postgresql";
|
|
@@ -191,7 +191,7 @@ export interface PostgresqlDbConnConfig {
|
|
|
191
191
|
}
|
|
192
192
|
|
|
193
193
|
/**
|
|
194
|
-
*
|
|
194
|
+
* Extract Dialect from DbConnConfig
|
|
195
195
|
*/
|
|
196
196
|
export function getDialectFromConfig(config: DbConnConfig): Dialect {
|
|
197
197
|
if (config.dialect === "mssql-azure") {
|