@starbemtech/star-db-query-builder 1.0.38 → 1.2.1
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/.github/workflows/publish.yml +118 -0
- package/ARCHITECTURE.md +313 -0
- package/CHANGELOG.md +65 -0
- package/README.md +1309 -129
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/lcov-report/block-navigation.js +1 -1
- package/coverage/lcov-report/index.html +41 -11
- package/coverage/lcov-report/mysqlClient.ts.html +685 -0
- package/coverage/lcov-report/pgClient.ts.html +823 -0
- package/coverage/lcov-report/sorter.js +21 -7
- package/coverage/lcov.info +533 -0
- package/coverage/mysqlClient.ts.html +685 -0
- package/coverage/pgClient.ts.html +823 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/src/core/repository.d.ts +374 -0
- package/dist/src/core/repository.js +677 -0
- package/dist/src/core/repository.js.map +1 -0
- package/dist/src/{default → core}/types.d.ts +9 -1
- package/dist/src/{default → core}/types.js.map +1 -1
- package/dist/src/core/utils.d.ts +133 -0
- package/dist/src/{default → core}/utils.js +167 -0
- package/dist/src/core/utils.js.map +1 -0
- package/dist/src/db/IDatabaseClient.d.ts +10 -1
- package/dist/src/db/initDb.d.ts +120 -1
- package/dist/src/db/initDb.js +119 -0
- package/dist/src/db/initDb.js.map +1 -1
- package/dist/src/db/mysqlClient.d.ts +23 -1
- package/dist/src/db/mysqlClient.js +115 -0
- package/dist/src/db/mysqlClient.js.map +1 -1
- package/dist/src/db/pgClient.d.ts +22 -1
- package/dist/src/db/pgClient.js +127 -0
- package/dist/src/db/pgClient.js.map +1 -1
- package/dist/src/monitor/monitor.d.ts +6 -1
- package/dist/src/monitor/monitor.js +5 -0
- package/dist/src/monitor/monitor.js.map +1 -1
- package/dist/src/setupTests.d.ts +26 -0
- package/dist/src/setupTests.js +43 -0
- package/dist/src/setupTests.js.map +1 -0
- package/docs/INDEX.md +145 -0
- package/docs/methods/findFirst.md +394 -0
- package/docs/methods/findMany.md +587 -0
- package/docs/methods/insert.md +536 -0
- package/docs/methods/insertMany.md +627 -0
- package/docs/methods/joins.md +781 -0
- package/docs/methods/rawQuery.md +284 -0
- package/docs/methods/transactions.md +737 -0
- package/eslint.config.mjs +77 -0
- package/index.ts +5 -4
- package/jest.config.ts +23 -28
- package/package.json +52 -29
- package/scripts/release.sh +123 -0
- package/src/core/repository.ts +865 -0
- package/src/{default → core}/types.ts +11 -2
- package/src/{default → core}/utils.ts +168 -0
- package/src/db/IDatabaseClient.ts +11 -1
- package/src/db/__tests__/mysqlClient.test.ts +262 -0
- package/src/db/__tests__/pgClient.test.ts +260 -0
- package/src/db/initDb.ts +120 -1
- package/src/db/mysqlClient.ts +119 -3
- package/src/db/pgClient.ts +131 -3
- package/src/monitor/monitor.ts +5 -0
- package/src/setupTests.ts +45 -0
- package/tsconfig.test.json +21 -0
- package/.eslintignore +0 -4
- package/.eslintrc.json +0 -32
- package/coverage/clover.xml +0 -6
- package/coverage/coverage-final.json +0 -1
- package/dist/.eslintrc.json +0 -32
- package/dist/src/default/genericRepository.d.ts +0 -20
- package/dist/src/default/genericRepository.js +0 -192
- package/dist/src/default/genericRepository.js.map +0 -1
- package/dist/src/default/utils.d.ts +0 -9
- package/dist/src/default/utils.js.map +0 -1
- package/index.d.ts +0 -60
- package/src/default/genericRepository.ts +0 -320
- /package/dist/src/{default → core}/types.js +0 -0
package/dist/src/db/initDb.js
CHANGED
|
@@ -7,6 +7,53 @@ const pgClient_1 = require("./pgClient");
|
|
|
7
7
|
const mysqlClient_1 = require("./mysqlClient");
|
|
8
8
|
const dbClients = {};
|
|
9
9
|
const defaultName = 'default';
|
|
10
|
+
/**
|
|
11
|
+
* Initialize a database client with the specified configuration
|
|
12
|
+
*
|
|
13
|
+
* @template T - The type of connection options (PoolConfig for PostgreSQL or PoolOptions for MySQL)
|
|
14
|
+
* @param config - Configuration object for database initialization
|
|
15
|
+
* @param config.name - Optional name for the database client (defaults to 'default')
|
|
16
|
+
* @param config.type - Database type ('pg' for PostgreSQL or 'mysql' for MySQL)
|
|
17
|
+
* @param config.options - Connection options specific to the database type
|
|
18
|
+
* @param config.retryOptions - Optional retry configuration for failed queries
|
|
19
|
+
* @param config.installUnaccentExtension - Optional flag to install unaccent extension (PostgreSQL only)
|
|
20
|
+
* @returns Promise<void> - Resolves when the database client is successfully initialized
|
|
21
|
+
*
|
|
22
|
+
* @throws {Error} When type is not provided or is invalid
|
|
23
|
+
* @throws {Error} When connection options are not provided
|
|
24
|
+
* @throws {Error} When an unsupported database type is specified
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Initialize PostgreSQL client
|
|
28
|
+
* await initDb({
|
|
29
|
+
* name: 'myPostgresDb',
|
|
30
|
+
* type: 'pg',
|
|
31
|
+
* options: {
|
|
32
|
+
* host: 'localhost',
|
|
33
|
+
* port: 5432,
|
|
34
|
+
* database: 'mydb',
|
|
35
|
+
* user: 'user',
|
|
36
|
+
* password: 'password'
|
|
37
|
+
* },
|
|
38
|
+
* retryOptions: { maxRetries: 3, delay: 1000 },
|
|
39
|
+
* installUnaccentExtension: true
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Initialize MySQL client
|
|
44
|
+
* await initDb({
|
|
45
|
+
* name: 'myMysqlDb',
|
|
46
|
+
* type: 'mysql',
|
|
47
|
+
* options: {
|
|
48
|
+
* host: 'localhost',
|
|
49
|
+
* port: 3306,
|
|
50
|
+
* database: 'mydb',
|
|
51
|
+
* user: 'user',
|
|
52
|
+
* password: 'password'
|
|
53
|
+
* },
|
|
54
|
+
* retryOptions: { maxRetries: 3, delay: 1000 }
|
|
55
|
+
* });
|
|
56
|
+
*/
|
|
10
57
|
const initDb = async (config) => {
|
|
11
58
|
if (!config.type)
|
|
12
59
|
throw new Error('Type is required. Accept values: pg | mysql');
|
|
@@ -28,6 +75,53 @@ const initDb = async (config) => {
|
|
|
28
75
|
}
|
|
29
76
|
};
|
|
30
77
|
exports.initDb = initDb;
|
|
78
|
+
/**
|
|
79
|
+
* Initialize a database client with the specified configuration
|
|
80
|
+
*
|
|
81
|
+
* @template T - The type of connection options (PoolConfig for PostgreSQL or PoolOptions for MySQL)
|
|
82
|
+
* @param config - Configuration object for database initialization
|
|
83
|
+
* @param config.name - Optional name for the database client (defaults to 'default')
|
|
84
|
+
* @param config.type - Database type ('pg' for PostgreSQL or 'mysql' for MySQL)
|
|
85
|
+
* @param config.options - Connection options specific to the database type
|
|
86
|
+
* @param config.retryOptions - Optional retry configuration for failed queries
|
|
87
|
+
* @param config.installUnaccentExtension - Optional flag to install unaccent extension (PostgreSQL only)
|
|
88
|
+
* @returns Promise<void> - Resolves when the database client is successfully initialized
|
|
89
|
+
*
|
|
90
|
+
* @throws {Error} When database type is not provided or is invalid
|
|
91
|
+
* @throws {Error} When connection options are not provided
|
|
92
|
+
* @throws {Error} When an unsupported database type is specified
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* // Initialize PostgreSQL client
|
|
96
|
+
* await initDb({
|
|
97
|
+
* name: 'myPostgresDb',
|
|
98
|
+
* type: 'pg',
|
|
99
|
+
* options: {
|
|
100
|
+
* host: 'localhost',
|
|
101
|
+
* port: 5432,
|
|
102
|
+
* database: 'mydb',
|
|
103
|
+
* user: 'user',
|
|
104
|
+
* password: 'password'
|
|
105
|
+
* },
|
|
106
|
+
* retryOptions: { maxRetries: 3, delay: 1000 },
|
|
107
|
+
* installUnaccentExtension: true
|
|
108
|
+
* });
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* // Initialize MySQL client
|
|
112
|
+
* await initDb({
|
|
113
|
+
* name: 'myMysqlDb',
|
|
114
|
+
* type: 'mysql',
|
|
115
|
+
* options: {
|
|
116
|
+
* host: 'localhost',
|
|
117
|
+
* port: 3306,
|
|
118
|
+
* database: 'mydb',
|
|
119
|
+
* user: 'user',
|
|
120
|
+
* password: 'password'
|
|
121
|
+
* },
|
|
122
|
+
* retryOptions: { maxRetries: 3, delay: 1000 }
|
|
123
|
+
* });
|
|
124
|
+
*/
|
|
31
125
|
const getDbClient = (name) => {
|
|
32
126
|
const client = dbClients[name || defaultName];
|
|
33
127
|
if (!client) {
|
|
@@ -36,6 +130,31 @@ const getDbClient = (name) => {
|
|
|
36
130
|
return client;
|
|
37
131
|
};
|
|
38
132
|
exports.getDbClient = getDbClient;
|
|
133
|
+
/**
|
|
134
|
+
* Retrieves a specific database client by name
|
|
135
|
+
*
|
|
136
|
+
* @param name - Optional name of the database client to retrieve. If not provided, returns the default client
|
|
137
|
+
* @returns IDatabaseClient - The requested database client instance
|
|
138
|
+
*
|
|
139
|
+
* @throws {Error} When the specified database client name is not found or not initialized
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* // Get default database client
|
|
143
|
+
* const defaultClient = getDbClient();
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* // Get specific database client by name
|
|
147
|
+
* const postgresClient = getDbClient('myPostgresDb');
|
|
148
|
+
* const mysqlClient = getDbClient('myMysqlDb');
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* // Handle error when client is not found
|
|
152
|
+
* try {
|
|
153
|
+
* const client = getDbClient('nonExistentDb');
|
|
154
|
+
* } catch (error) {
|
|
155
|
+
* console.error('Database client not found:', error.message);
|
|
156
|
+
* }
|
|
157
|
+
*/
|
|
39
158
|
const getAllDbClients = () => {
|
|
40
159
|
return dbClients;
|
|
41
160
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initDb.js","sourceRoot":"","sources":["../../../src/db/initDb.ts"],"names":[],"mappings":";;;AAAA,2BAAqC;AACrC,4CAA8D;A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GAAG,IAAA,+BAAiB,EACvD,IAAI,EACJ,MAAM,CAAC,YAAY,CACpB,CAAA;QAED,OAAO,CAAC,IAAI,CACV,2DAA2D,MAAM,CAAC,IAAI,wBAAwB,CAC/F,CAAA;IACH,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;IAC9C,CAAC;AACH,CAAC,CAAA;AAtCY,QAAA,MAAM,UAsClB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACI,MAAM,WAAW,GAAG,CAAC,IAAa,EAAmB,EAAE;IAC5D,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,IAAI,WAAW,CAAC,CAAA;IAC7C,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,oBAAoB,IAAI,sBAAsB,CAAC,CAAA;IACjE,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAPY,QAAA,WAAW,eAOvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACI,MAAM,eAAe,GAAG,GAAoC,EAAE;IACnE,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA;AAFY,QAAA,eAAe,mBAE3B"}
|
|
@@ -1,4 +1,26 @@
|
|
|
1
1
|
import { Pool } from 'mysql2/promise';
|
|
2
2
|
import { IDatabaseClient } from './IDatabaseClient';
|
|
3
|
-
import { RetryOptions } from '../
|
|
3
|
+
import { RetryOptions } from '../core/types';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a MySQL database client
|
|
6
|
+
*
|
|
7
|
+
* This function initializes a MySQL database client with the provided pool configuration
|
|
8
|
+
* and optional retry options. It sets up monitoring for connection events and query operations.
|
|
9
|
+
*
|
|
10
|
+
* @param pool - The MySQL pool instance
|
|
11
|
+
* @param retryOptions - Optional retry options for failed queries
|
|
12
|
+
* @returns IDatabaseClient - The MySQL database client instance
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
25
|
+
*/
|
|
4
26
|
export declare const createMysqlClient: (pool: Pool, retryOptions?: RetryOptions) => IDatabaseClient;
|
|
@@ -12,9 +12,54 @@ const transientErrorCodes = new Set([
|
|
|
12
12
|
'PROTOCOL_CONNECTION_LOST',
|
|
13
13
|
'ECONNREFUSED',
|
|
14
14
|
]);
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the given error is a transient error that can be retried
|
|
17
|
+
*
|
|
18
|
+
* Transient errors are temporary network or connection issues that typically
|
|
19
|
+
* resolve themselves and can be safely retried. This function checks if the
|
|
20
|
+
* error code matches known transient error patterns for MySQL connections.
|
|
21
|
+
*
|
|
22
|
+
* @param error - The error object to check
|
|
23
|
+
* @returns boolean - True if the error is transient and can be retried, false otherwise
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* try {
|
|
27
|
+
* const result = await pool.execute(sql, params);
|
|
28
|
+
* } catch (error) {
|
|
29
|
+
* if (isTransientError(error)) {
|
|
30
|
+
* // Retry the operation
|
|
31
|
+
* console.log('Transient error detected, retrying...');
|
|
32
|
+
* } else {
|
|
33
|
+
* // Handle permanent error
|
|
34
|
+
* throw error;
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
15
38
|
function isTransientError(error) {
|
|
16
39
|
return error && error.code && transientErrorCodes.has(error.code);
|
|
17
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates a MySQL database client
|
|
43
|
+
*
|
|
44
|
+
* This function initializes a MySQL database client with the provided pool configuration
|
|
45
|
+
* and optional retry options. It sets up monitoring for connection events and query operations.
|
|
46
|
+
*
|
|
47
|
+
* @param pool - The MySQL pool instance
|
|
48
|
+
* @param retryOptions - Optional retry options for failed queries
|
|
49
|
+
* @returns IDatabaseClient - The MySQL database client instance
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const mysqlClient = createMysqlClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
62
|
+
*/
|
|
18
63
|
const createMysqlClient = (pool, retryOptions) => {
|
|
19
64
|
monitor_1.monitor.emit(monitor_1.MonitorEvents.CONNECTION_CREATED, {
|
|
20
65
|
clientType: 'mysql',
|
|
@@ -68,6 +113,76 @@ const createMysqlClient = (pool, retryOptions) => {
|
|
|
68
113
|
}
|
|
69
114
|
}, retryOptions);
|
|
70
115
|
},
|
|
116
|
+
beginTransaction: async () => {
|
|
117
|
+
const connection = await pool.getConnection();
|
|
118
|
+
try {
|
|
119
|
+
await connection.beginTransaction();
|
|
120
|
+
return {
|
|
121
|
+
query: async (sql, params) => {
|
|
122
|
+
const startTime = Date.now();
|
|
123
|
+
try {
|
|
124
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.QUERY_START, {
|
|
125
|
+
clientType: 'mysql',
|
|
126
|
+
sql,
|
|
127
|
+
params,
|
|
128
|
+
attempt: 1,
|
|
129
|
+
inTransaction: true,
|
|
130
|
+
});
|
|
131
|
+
const [rows] = await connection.execute(sql, params);
|
|
132
|
+
const elapsedTime = Date.now() - startTime;
|
|
133
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.QUERY_END, {
|
|
134
|
+
clientType: 'mysql',
|
|
135
|
+
sql,
|
|
136
|
+
params,
|
|
137
|
+
attempt: 1,
|
|
138
|
+
elapsedTime,
|
|
139
|
+
inTransaction: true,
|
|
140
|
+
});
|
|
141
|
+
return rows;
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
const elapsedTime = Date.now() - startTime;
|
|
145
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.QUERY_ERROR, {
|
|
146
|
+
clientType: 'mysql',
|
|
147
|
+
sql,
|
|
148
|
+
params,
|
|
149
|
+
attempt: 1,
|
|
150
|
+
elapsedTime,
|
|
151
|
+
error,
|
|
152
|
+
inTransaction: true,
|
|
153
|
+
});
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
commit: async () => {
|
|
158
|
+
try {
|
|
159
|
+
await connection.commit();
|
|
160
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.TRANSACTION_COMMIT, {
|
|
161
|
+
clientType: 'mysql',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
connection.release();
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
rollback: async () => {
|
|
169
|
+
try {
|
|
170
|
+
await connection.rollback();
|
|
171
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.TRANSACTION_ROLLBACK, {
|
|
172
|
+
clientType: 'mysql',
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
connection.release();
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
connection.release();
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
},
|
|
71
186
|
};
|
|
72
187
|
};
|
|
73
188
|
exports.createMysqlClient = createMysqlClient;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mysqlClient.js","sourceRoot":"","sources":["../../../src/db/mysqlClient.ts"],"names":[],"mappings":";;;;;;AACA,kEAAwC;AAGxC,gDAA2D;AAE3D,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC;IAClC,YAAY;IACZ,WAAW;IACX,0BAA0B;IAC1B,cAAc;CACf,CAAC,CAAA;AAEF,SAAS,gBAAgB,CAAC,KAAU;IAClC,OAAO,KAAK,IAAI,KAAK,CAAC,IAAI,IAAI,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;AACnE,CAAC;
|
|
1
|
+
{"version":3,"file":"mysqlClient.js","sourceRoot":"","sources":["../../../src/db/mysqlClient.ts"],"names":[],"mappings":";;;;;;AACA,kEAAwC;AAGxC,gDAA2D;AAE3D,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC;IAClC,YAAY;IACZ,WAAW;IACX,0BAA0B;IAC1B,cAAc;CACf,CAAC,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAS,gBAAgB,CAAC,KAAU;IAClC,OAAO,KAAK,IAAI,KAAK,CAAC,IAAI,IAAI,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;AACnE,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACI,MAAM,iBAAiB,GAAG,CAC/B,IAAU,EACV,YAA2B,EACV,EAAE;IACnB,iBAAO,CAAC,IAAI,CAAC,uBAAa,CAAC,kBAAkB,EAAE;QAC7C,UAAU,EAAE,OAAO;QACnB,WAAW,EAAE,IAAI,CAAC,MAAM;KACzB,CAAC,CAAA;IAEF,OAAO;QACL,UAAU,EAAE,OAAO;QACnB,KAAK,EAAE,KAAK,EAAK,GAAW,EAAE,MAAc,EAAc,EAAE;YAC1D,OAAO,IAAA,uBAAY,EAAC,KAAK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AAC,CAAA;4BACF,MAAM,KAAK,CAAA;wBACb,CAAC;oBACH,CAAC;oBACD,MAAM,EAAE,KAAK,IAAmB,EAAE;wBAChC,IAAI,CAAC;4BACH,MAAM,UAAU,CAAC,MAAM,EAAE,CAAA;4BACzB,iBAAO,CAAC,IAAI,CAAC,uBAAa,CAAC,kBAAkB,EAAE;gCAC7C,UAAU,EAAE,OAAO;6BACpB,CAAC,CAAA;wBACJ,CAAC;gCAAS,CAAC;4BACT,UAAU,CAAC,OAAO,EAAE,CAAA;wBACtB,CAAC;oBACH,CAAC;oBACD,QAAQ,EAAE,KAAK,IAAmB,EAAE;wBAClC,IAAI,CAAC;4BACH,MAAM,UAAU,CAAC,QAAQ,EAAE,CAAA;4BAC3B,iBAAO,CAAC,IAAI,CAAC,uBAAa,CAAC,oBAAoB,EAAE;gCAC/C,UAAU,EAAE,OAAO;6BACpB,CAAC,CAAA;wBACJ,CAAC;gCAAS,CAAC;4BACT,UAAU,CAAC,OAAO,EAAE,CAAA;wBACtB,CAAC;oBACH,CAAC;iBACF,CAAA;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,UAAU,CAAC,OAAO,EAAE,CAAA;gBACpB,MAAM,KAAK,CAAA;YACb,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC,CAAA;AAzIY,QAAA,iBAAiB,qBAyI7B"}
|
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
import { Pool, PoolConfig } from 'pg';
|
|
2
2
|
import { IDatabaseClient } from './IDatabaseClient';
|
|
3
|
-
import { RetryOptions } from '../
|
|
3
|
+
import { RetryOptions } from '../core/types';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a PostgreSQL database client
|
|
6
|
+
*
|
|
7
|
+
* This function initializes a PostgreSQL database client with the provided pool configuration
|
|
8
|
+
* and optional retry options. It sets up monitoring for connection events and query operations.
|
|
9
|
+
*
|
|
10
|
+
* @param pool - The PostgreSQL pool instance
|
|
11
|
+
* @param retryOptions - Optional retry options for failed queries
|
|
12
|
+
* @param poolConfig - Optional pool configuration
|
|
13
|
+
* @param installUnaccentExtension - Optional flag to install unaccent extension
|
|
14
|
+
* @returns IDatabaseClient - The PostgreSQL database client instance
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const pgClient = await createPgClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const pgClient = await createPgClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const pgClient = await createPgClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
24
|
+
*/
|
|
4
25
|
export declare const createPgClient: (pool: Pool, retryOptions?: RetryOptions, poolConfig?: PoolConfig, installUnaccentExtension?: boolean) => Promise<IDatabaseClient>;
|
package/dist/src/db/pgClient.js
CHANGED
|
@@ -7,9 +7,45 @@ exports.createPgClient = void 0;
|
|
|
7
7
|
const promise_retry_1 = __importDefault(require("promise-retry"));
|
|
8
8
|
const monitor_1 = require("../monitor/monitor");
|
|
9
9
|
const transientErrorCodes = new Set(['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED']);
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the given error is a transient error that can be retried
|
|
12
|
+
*
|
|
13
|
+
* Transient errors are temporary network or connection issues that typically
|
|
14
|
+
* resolve themselves and can be safely retried. This function checks if the
|
|
15
|
+
* error code matches known transient error patterns for PostgreSQL connections.
|
|
16
|
+
*
|
|
17
|
+
* @param error - The error object to check
|
|
18
|
+
* @returns boolean - True if the error is transient and can be retried, false otherwise
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* try {
|
|
22
|
+
* const result = await pool.query(sql, params);
|
|
23
|
+
* } catch (error) {
|
|
24
|
+
* if (isTransientError(error)) {
|
|
25
|
+
* // Retry the operation
|
|
26
|
+
* console.log('Transient error detected, retrying...');
|
|
27
|
+
* } else {
|
|
28
|
+
* // Handle permanent error
|
|
29
|
+
* throw error;
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
*/
|
|
10
33
|
function isTransientError(error) {
|
|
11
34
|
return error && error.code && transientErrorCodes.has(error.code);
|
|
12
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Ensures that the unaccent extension is installed in the PostgreSQL database
|
|
38
|
+
*
|
|
39
|
+
* This function checks if the unaccent extension is installed in the database
|
|
40
|
+
* and installs it if it is not. It also emits a connection created event to
|
|
41
|
+
* the monitor.
|
|
42
|
+
*
|
|
43
|
+
* @param pool - The PostgreSQL pool instance
|
|
44
|
+
* @returns Promise<void> - Resolves when the unaccent extension is installed or already exists
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* await ensureUnaccentExtension(pool);
|
|
48
|
+
*/
|
|
13
49
|
async function ensureUnaccentExtension(pool) {
|
|
14
50
|
try {
|
|
15
51
|
const checkResult = await pool.query(`
|
|
@@ -35,6 +71,27 @@ async function ensureUnaccentExtension(pool) {
|
|
|
35
71
|
${error}`);
|
|
36
72
|
}
|
|
37
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a PostgreSQL database client
|
|
76
|
+
*
|
|
77
|
+
* This function initializes a PostgreSQL database client with the provided pool configuration
|
|
78
|
+
* and optional retry options. It sets up monitoring for connection events and query operations.
|
|
79
|
+
*
|
|
80
|
+
* @param pool - The PostgreSQL pool instance
|
|
81
|
+
* @param retryOptions - Optional retry options for failed queries
|
|
82
|
+
* @param poolConfig - Optional pool configuration
|
|
83
|
+
* @param installUnaccentExtension - Optional flag to install unaccent extension
|
|
84
|
+
* @returns IDatabaseClient - The PostgreSQL database client instance
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const pgClient = await createPgClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* const pgClient = await createPgClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* const pgClient = await createPgClient(pool, { retries: 3, factor: 2, minTimeout: 1000 });
|
|
94
|
+
*/
|
|
38
95
|
const createPgClient = async (pool, retryOptions, poolConfig, installUnaccentExtension) => {
|
|
39
96
|
if (installUnaccentExtension) {
|
|
40
97
|
await ensureUnaccentExtension(pool);
|
|
@@ -91,6 +148,76 @@ const createPgClient = async (pool, retryOptions, poolConfig, installUnaccentExt
|
|
|
91
148
|
}
|
|
92
149
|
}, retryOptions);
|
|
93
150
|
},
|
|
151
|
+
beginTransaction: async () => {
|
|
152
|
+
const client = await pool.connect();
|
|
153
|
+
try {
|
|
154
|
+
await client.query('BEGIN');
|
|
155
|
+
return {
|
|
156
|
+
query: async (sql, params) => {
|
|
157
|
+
const startTime = Date.now();
|
|
158
|
+
try {
|
|
159
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.QUERY_START, {
|
|
160
|
+
clientType: 'pg',
|
|
161
|
+
sql,
|
|
162
|
+
params,
|
|
163
|
+
attempt: 1,
|
|
164
|
+
inTransaction: true,
|
|
165
|
+
});
|
|
166
|
+
const { rows } = await client.query(sql, params);
|
|
167
|
+
const elapsedTime = Date.now() - startTime;
|
|
168
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.QUERY_END, {
|
|
169
|
+
clientType: 'pg',
|
|
170
|
+
sql,
|
|
171
|
+
params,
|
|
172
|
+
attempt: 1,
|
|
173
|
+
elapsedTime,
|
|
174
|
+
inTransaction: true,
|
|
175
|
+
});
|
|
176
|
+
return rows;
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
const elapsedTime = Date.now() - startTime;
|
|
180
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.QUERY_ERROR, {
|
|
181
|
+
clientType: 'pg',
|
|
182
|
+
sql,
|
|
183
|
+
params,
|
|
184
|
+
attempt: 1,
|
|
185
|
+
elapsedTime,
|
|
186
|
+
error,
|
|
187
|
+
inTransaction: true,
|
|
188
|
+
});
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
commit: async () => {
|
|
193
|
+
try {
|
|
194
|
+
await client.query('COMMIT');
|
|
195
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.TRANSACTION_COMMIT, {
|
|
196
|
+
clientType: 'pg',
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
client.release();
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
rollback: async () => {
|
|
204
|
+
try {
|
|
205
|
+
await client.query('ROLLBACK');
|
|
206
|
+
monitor_1.monitor.emit(monitor_1.MonitorEvents.TRANSACTION_ROLLBACK, {
|
|
207
|
+
clientType: 'pg',
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
client.release();
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
client.release();
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
},
|
|
94
221
|
};
|
|
95
222
|
};
|
|
96
223
|
exports.createPgClient = createPgClient;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pgClient.js","sourceRoot":"","sources":["../../../src/db/pgClient.ts"],"names":[],"mappings":";;;;;;AACA,kEAAwC;AAGxC,gDAA2D;AAE3D,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,YAAY,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC,CAAA;AAEhF,SAAS,gBAAgB,CAAC,KAAU;IAClC,OAAO,KAAK,IAAI,KAAK,CAAC,IAAI,IAAI,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;AACnE,CAAC;AAED,KAAK,UAAU,uBAAuB,CAAC,IAAU;IAC/C,IAAI,CAAC;QACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC;;KA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BAAkB,EAAE;gCAC7C,UAAU,EAAE,IAAI;6BACjB,CAAC,CAAA;wBACJ,CAAC;gCAAS,CAAC;4BACT,MAAM,CAAC,OAAO,EAAE,CAAA;wBAClB,CAAC;oBACH,CAAC;oBACD,QAAQ,EAAE,KAAK,IAAmB,EAAE;wBAClC,IAAI,CAAC;4BACH,MAAM,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;4BAC9B,iBAAO,CAAC,IAAI,CAAC,uBAAa,CAAC,oBAAoB,EAAE;gCAC/C,UAAU,EAAE,IAAI;6BACjB,CAAC,CAAA;wBACJ,CAAC;gCAAS,CAAC;4BACT,MAAM,CAAC,OAAO,EAAE,CAAA;wBAClB,CAAC;oBACH,CAAC;iBACF,CAAA;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,CAAC,OAAO,EAAE,CAAA;gBAChB,MAAM,KAAK,CAAA;YACb,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC,CAAA;AAhJY,QAAA,cAAc,kBAgJ1B"}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { EventEmitter } from 'stream';
|
|
2
|
+
/**
|
|
3
|
+
* Monitor events
|
|
4
|
+
*/
|
|
2
5
|
export declare enum MonitorEvents {
|
|
3
6
|
CONNECTION_CREATED = "connection_created",
|
|
4
7
|
QUERY_START = "query_start",
|
|
5
8
|
QUERY_END = "query_end",
|
|
6
9
|
QUERY_ERROR = "query_error",
|
|
7
|
-
RETRY_ATTEMPT = "retry_attempt"
|
|
10
|
+
RETRY_ATTEMPT = "retry_attempt",
|
|
11
|
+
TRANSACTION_COMMIT = "transaction_commit",
|
|
12
|
+
TRANSACTION_ROLLBACK = "transaction_rollback"
|
|
8
13
|
}
|
|
9
14
|
export declare const monitor: EventEmitter<[never]>;
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.monitor = exports.MonitorEvents = void 0;
|
|
4
4
|
const stream_1 = require("stream");
|
|
5
|
+
/**
|
|
6
|
+
* Monitor events
|
|
7
|
+
*/
|
|
5
8
|
var MonitorEvents;
|
|
6
9
|
(function (MonitorEvents) {
|
|
7
10
|
MonitorEvents["CONNECTION_CREATED"] = "connection_created";
|
|
@@ -9,6 +12,8 @@ var MonitorEvents;
|
|
|
9
12
|
MonitorEvents["QUERY_END"] = "query_end";
|
|
10
13
|
MonitorEvents["QUERY_ERROR"] = "query_error";
|
|
11
14
|
MonitorEvents["RETRY_ATTEMPT"] = "retry_attempt";
|
|
15
|
+
MonitorEvents["TRANSACTION_COMMIT"] = "transaction_commit";
|
|
16
|
+
MonitorEvents["TRANSACTION_ROLLBACK"] = "transaction_rollback";
|
|
12
17
|
})(MonitorEvents || (exports.MonitorEvents = MonitorEvents = {}));
|
|
13
18
|
exports.monitor = new stream_1.EventEmitter();
|
|
14
19
|
//# sourceMappingURL=monitor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"monitor.js","sourceRoot":"","sources":["../../../src/monitor/monitor.ts"],"names":[],"mappings":";;;AAAA,mCAAqC;AAErC,IAAY,
|
|
1
|
+
{"version":3,"file":"monitor.js","sourceRoot":"","sources":["../../../src/monitor/monitor.ts"],"names":[],"mappings":";;;AAAA,mCAAqC;AAErC;;GAEG;AACH,IAAY,aAQX;AARD,WAAY,aAAa;IACvB,0DAAyC,CAAA;IACzC,4CAA2B,CAAA;IAC3B,wCAAuB,CAAA;IACvB,4CAA2B,CAAA;IAC3B,gDAA+B,CAAA;IAC/B,0DAAyC,CAAA;IACzC,8DAA6C,CAAA;AAC/C,CAAC,EARW,aAAa,6BAAb,aAAa,QAQxB;AAEY,QAAA,OAAO,GAAG,IAAI,qBAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare const testUtils: {
|
|
2
|
+
createMockDbClient: (clientType?: "pg" | "mysql") => {
|
|
3
|
+
clientType: "pg" | "mysql";
|
|
4
|
+
query: jest.Mock<any, any, any>;
|
|
5
|
+
connect: jest.Mock<any, any, any>;
|
|
6
|
+
end: jest.Mock<any, any, any>;
|
|
7
|
+
};
|
|
8
|
+
createSampleUser: () => {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
email: string;
|
|
12
|
+
status: string;
|
|
13
|
+
created_at: Date;
|
|
14
|
+
updated_at: Date;
|
|
15
|
+
};
|
|
16
|
+
createSampleConditions: () => {
|
|
17
|
+
status: {
|
|
18
|
+
operator: string;
|
|
19
|
+
value: string;
|
|
20
|
+
};
|
|
21
|
+
email: {
|
|
22
|
+
operator: string;
|
|
23
|
+
value: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Jest setup file for star-db-query-builder tests
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.testUtils = void 0;
|
|
5
|
+
// Mock console methods to reduce noise in tests
|
|
6
|
+
const originalConsole = { ...console };
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
// Suppress console.log, console.warn, and console.error during tests
|
|
9
|
+
jest.spyOn(console, 'log').mockImplementation(() => { });
|
|
10
|
+
jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
11
|
+
jest.spyOn(console, 'error').mockImplementation(() => { });
|
|
12
|
+
});
|
|
13
|
+
afterAll(() => {
|
|
14
|
+
// Restore original console methods
|
|
15
|
+
console.log = originalConsole.log;
|
|
16
|
+
console.warn = originalConsole.warn;
|
|
17
|
+
console.error = originalConsole.error;
|
|
18
|
+
});
|
|
19
|
+
// Export test utilities for use in test files
|
|
20
|
+
exports.testUtils = {
|
|
21
|
+
// Helper to create mock database client
|
|
22
|
+
createMockDbClient: (clientType = 'pg') => ({
|
|
23
|
+
clientType,
|
|
24
|
+
query: jest.fn(),
|
|
25
|
+
connect: jest.fn(),
|
|
26
|
+
end: jest.fn(),
|
|
27
|
+
}),
|
|
28
|
+
// Helper to create sample data
|
|
29
|
+
createSampleUser: () => ({
|
|
30
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
31
|
+
name: 'John Doe',
|
|
32
|
+
email: 'john.doe@example.com',
|
|
33
|
+
status: 'active',
|
|
34
|
+
created_at: new Date('2023-01-01'),
|
|
35
|
+
updated_at: new Date('2023-01-01'),
|
|
36
|
+
}),
|
|
37
|
+
// Helper to create sample conditions
|
|
38
|
+
createSampleConditions: () => ({
|
|
39
|
+
status: { operator: '=', value: 'active' },
|
|
40
|
+
email: { operator: 'ILIKE', value: '%example.com' },
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=setupTests.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setupTests.js","sourceRoot":"","sources":["../../src/setupTests.ts"],"names":[],"mappings":";AAAA,kDAAkD;;;AAElD,gDAAgD;AAChD,MAAM,eAAe,GAAG,EAAE,GAAG,OAAO,EAAE,CAAA;AAEtC,SAAS,CAAC,GAAG,EAAE;IACb,qEAAqE;IACrE,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;IACvD,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;IACxD,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;AAC3D,CAAC,CAAC,CAAA;AAEF,QAAQ,CAAC,GAAG,EAAE;IACZ,mCAAmC;IACnC,OAAO,CAAC,GAAG,GAAG,eAAe,CAAC,GAAG,CAAA;IACjC,OAAO,CAAC,IAAI,GAAG,eAAe,CAAC,IAAI,CAAA;IACnC,OAAO,CAAC,KAAK,GAAG,eAAe,CAA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