@runnerpro/backend 1.9.3 → 1.9.4
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/lib/cjs/db/index.js
CHANGED
|
@@ -21,37 +21,61 @@ const pool = new Pool({
|
|
|
21
21
|
password: process.env.DB_PASSWORD,
|
|
22
22
|
host: process.env.DB_HOST,
|
|
23
23
|
port: 5432,
|
|
24
|
+
max: 10,
|
|
25
|
+
idleTimeoutMillis: 30000,
|
|
26
|
+
connectionTimeoutMillis: 10000,
|
|
27
|
+
keepAlive: true,
|
|
24
28
|
});
|
|
25
29
|
exports.pool = pool;
|
|
26
|
-
|
|
30
|
+
pool.on('error', (err, client) => {
|
|
31
|
+
var _a;
|
|
32
|
+
console.error('PG pool error', err);
|
|
33
|
+
if (client)
|
|
34
|
+
(_a = client.release) === null || _a === void 0 ? void 0 : _a.call(client);
|
|
35
|
+
});
|
|
36
|
+
const query = (queryText, values = [], _retry = false) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
37
|
const client = yield pool.connect();
|
|
28
38
|
const text = getParseQuery(queryText, values);
|
|
29
39
|
try {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
rowMode: '
|
|
40
|
+
// @ts-ignore — se añaden campos no tipados oficialmente
|
|
41
|
+
const result = (yield client.query({
|
|
42
|
+
rowMode: 'array',
|
|
33
43
|
text,
|
|
34
|
-
// @ts-ignore
|
|
44
|
+
// @ts-ignore — mezcla de tipos que TS no infiere bien
|
|
35
45
|
values: values.flat(),
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// @ts-ignore
|
|
46
|
+
}));
|
|
47
|
+
// @ts-ignore — rows existe en runtime pero no está en el tipo Submittable
|
|
39
48
|
return (result.rows || []).map((row) => {
|
|
40
49
|
const json = Object.keys(row);
|
|
41
50
|
const aux = {};
|
|
42
51
|
json.forEach((key) => {
|
|
43
|
-
// @ts-ignore
|
|
44
52
|
aux[camelize(key)] = row[key];
|
|
45
53
|
});
|
|
46
54
|
return aux;
|
|
47
55
|
});
|
|
48
56
|
}
|
|
49
57
|
catch (error) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
console.error('PG query error', error);
|
|
59
|
+
let forced = false;
|
|
60
|
+
if (!_retry && ['EPIPE', 'ECONNRESET', '57P01'].includes(error.code)) {
|
|
61
|
+
// destruimos la conexión rota y reintentamos una vez con back-off de 500 ms
|
|
62
|
+
client.release(true);
|
|
63
|
+
forced = true;
|
|
64
|
+
yield sleep(500);
|
|
65
|
+
return query(queryText, values, true);
|
|
66
|
+
}
|
|
67
|
+
if (!forced)
|
|
68
|
+
client.release();
|
|
53
69
|
return [];
|
|
54
70
|
}
|
|
71
|
+
finally {
|
|
72
|
+
try {
|
|
73
|
+
client.release();
|
|
74
|
+
}
|
|
75
|
+
catch (_) {
|
|
76
|
+
/* ya liberado */
|
|
77
|
+
}
|
|
78
|
+
}
|
|
55
79
|
});
|
|
56
80
|
exports.query = query;
|
|
57
81
|
const toPgArray = (arr) => {
|
|
@@ -104,3 +128,22 @@ const batchQuery = (queries, length = 5) => __awaiter(void 0, void 0, void 0, fu
|
|
|
104
128
|
yield Promise.all(promises);
|
|
105
129
|
});
|
|
106
130
|
exports.batchQuery = batchQuery;
|
|
131
|
+
['SIGTERM', 'SIGINT', 'beforeExit'].forEach((signal) => {
|
|
132
|
+
process.on(signal, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
133
|
+
try {
|
|
134
|
+
yield pool.end();
|
|
135
|
+
console.log('PG pool closed');
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
console.error('Error closing PG pool', e);
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
if (signal !== 'beforeExit')
|
|
142
|
+
process.exit(0);
|
|
143
|
+
}
|
|
144
|
+
}));
|
|
145
|
+
});
|
|
146
|
+
// Pequeño helper para aplicar retraso (back-off)
|
|
147
|
+
function sleep(ms) {
|
|
148
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
149
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import postgresql from 'pg';
|
|
2
2
|
declare const pool: postgresql.Pool;
|
|
3
|
-
declare const query: (queryText: string, values?: (string | number | boolean)[]) =>
|
|
3
|
+
declare const query: (queryText: string, values?: (string | number | boolean)[], _retry?: boolean) => any;
|
|
4
4
|
declare const toPgArray: (arr: string[]) => string;
|
|
5
5
|
declare const batchQuery: (queries: any, length?: number) => Promise<void>;
|
|
6
6
|
export { query, batchQuery, toPgArray, pool };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/db/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,IAAI,CAAC;AAI5B,QAAA,MAAM,IAAI,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/db/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,IAAI,CAAC;AAI5B,QAAA,MAAM,IAAI,iBAUR,CAAC;AAOH,QAAA,MAAM,KAAK,cACE,MAAM,WACT,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,0BA2CtC,CAAC;AAEF,QAAA,MAAM,SAAS,QAAS,MAAM,EAAE,WAM/B,CAAC;AA+BF,QAAA,MAAM,UAAU,YAAmB,GAAG,mCAUrC,CAAC;AAEF,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC"}
|