mielk-api 1.5.13 → 1.6.0
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/dist/db/pg/connection/pool.d.ts +2 -0
- package/dist/db/pg/connection/pool.js +14 -7
- package/dist/db/pg/connection/transaction.d.ts +5 -1
- package/dist/db/pg/connection/transaction.js +34 -7
- package/dist/db/pg/index.d.ts +2 -2
- package/dist/db/pg/index.js +2 -2
- package/dist/internal/messaging/messageTags.d.ts +5 -0
- package/dist/internal/messaging/messageTags.js +6 -0
- package/package.json +1 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { Pool } from 'pg';
|
|
2
2
|
import { PostgreDbConfig } from '../types/DbConfig.js';
|
|
3
|
+
import { QueryExecutor } from '../types/Executor.js';
|
|
3
4
|
export declare const initDb: (dbConfig: PostgreDbConfig) => void;
|
|
4
5
|
export declare function getPool(): Promise<Pool>;
|
|
6
|
+
export declare function getQueryExecutor(): Promise<QueryExecutor>;
|
|
@@ -7,10 +7,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { Msg } from '../../../internal/messaging/messageTags.js';
|
|
11
10
|
import { Pool } from 'pg';
|
|
12
|
-
|
|
11
|
+
import { getClient } from './transaction.js';
|
|
12
|
+
import { Msg } from '../../../internal/messaging/messageTags.js';
|
|
13
13
|
let config = null;
|
|
14
|
+
let pool = null;
|
|
14
15
|
export const initDb = (dbConfig) => {
|
|
15
16
|
config = Object.assign({ max: 10, min: 0, idleTimeoutMillis: 30000, connectionTimeoutMillis: 10000 }, dbConfig);
|
|
16
17
|
};
|
|
@@ -20,11 +21,6 @@ export function getPool() {
|
|
|
20
21
|
return pool;
|
|
21
22
|
if (config === null)
|
|
22
23
|
throw new Error(Msg.connection.notInitialized);
|
|
23
|
-
// if (!isProd()) {
|
|
24
|
-
// if (!config.ssh) throw new Error(Msg.connection.sshOptionsNotSpecified)
|
|
25
|
-
// const port: number = await openSshTunnel(config.ssh, config.port);
|
|
26
|
-
// config.port = port;
|
|
27
|
-
// }
|
|
28
24
|
pool = new Pool(config);
|
|
29
25
|
pool.on('error', (err) => {
|
|
30
26
|
console.error(Msg.connection.postgreConnectionError, err);
|
|
@@ -32,3 +28,14 @@ export function getPool() {
|
|
|
32
28
|
return pool;
|
|
33
29
|
});
|
|
34
30
|
}
|
|
31
|
+
export function getQueryExecutor() {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
const client = getClient();
|
|
34
|
+
if (client) {
|
|
35
|
+
return client;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return yield getPool();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import { PoolClient } from "pg";
|
|
2
|
-
export declare function withTransaction<T>(fn: (
|
|
2
|
+
export declare function withTransaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
3
|
+
export declare const getClient: () => PoolClient | null;
|
|
4
|
+
export declare function beginTransaction(): Promise<void>;
|
|
5
|
+
export declare function commitTransaction(): Promise<void>;
|
|
6
|
+
export declare function rollbackTransaction(): Promise<void>;
|
|
@@ -8,22 +8,49 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { getPool } from "./pool.js";
|
|
11
|
+
import { Msg } from "../../../internal/messaging/messageTags.js";
|
|
12
|
+
let client = null;
|
|
11
13
|
export function withTransaction(fn) {
|
|
12
14
|
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
-
const pool = yield getPool();
|
|
14
|
-
const client = yield pool.connect();
|
|
15
15
|
try {
|
|
16
|
-
yield
|
|
17
|
-
const result = yield fn(
|
|
18
|
-
yield
|
|
16
|
+
yield beginTransaction();
|
|
17
|
+
const result = yield fn();
|
|
18
|
+
yield commitTransaction();
|
|
19
19
|
return result;
|
|
20
20
|
}
|
|
21
21
|
catch (err) {
|
|
22
|
-
yield
|
|
22
|
+
yield rollbackTransaction();
|
|
23
23
|
throw err;
|
|
24
24
|
}
|
|
25
25
|
finally {
|
|
26
|
-
client
|
|
26
|
+
if (client) {
|
|
27
|
+
client.release();
|
|
28
|
+
client = null;
|
|
29
|
+
}
|
|
27
30
|
}
|
|
28
31
|
});
|
|
29
32
|
}
|
|
33
|
+
export const getClient = () => client;
|
|
34
|
+
export function beginTransaction() {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
if (client)
|
|
37
|
+
throw new Error(Msg.transactions.alreadyActive);
|
|
38
|
+
const pool = yield getPool();
|
|
39
|
+
client = yield pool.connect();
|
|
40
|
+
yield client.query('BEGIN');
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
export function commitTransaction() {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
if (!client)
|
|
46
|
+
throw new Error(Msg.transactions.noActiveTransactionToCommit);
|
|
47
|
+
yield client.query('COMMIT');
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export function rollbackTransaction() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
if (!client)
|
|
53
|
+
throw new Error(Msg.transactions.noActiveTransactionToRollback);
|
|
54
|
+
yield client.query('ROLLBACK');
|
|
55
|
+
});
|
|
56
|
+
}
|
package/dist/db/pg/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { PostgreDbConfig } from './types/DbConfig.js';
|
|
2
2
|
import { QueryExecutor } from './types/Executor.js';
|
|
3
|
-
import { initDb,
|
|
3
|
+
import { initDb, getQueryExecutor } from './connection/pool.js';
|
|
4
4
|
import { withTransaction } from './connection/transaction.js';
|
|
5
5
|
import { isUniqueViolation } from './errors/errorChecks.js';
|
|
6
6
|
import { UniqueKeyViolationError } from './errors/customErrors.js';
|
|
7
7
|
export { PostgreDbConfig, QueryExecutor };
|
|
8
|
-
export { initDb,
|
|
8
|
+
export { initDb, getQueryExecutor };
|
|
9
9
|
export { withTransaction };
|
|
10
10
|
export { isUniqueViolation };
|
|
11
11
|
export { UniqueKeyViolationError };
|
package/dist/db/pg/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { initDb,
|
|
1
|
+
import { initDb, getQueryExecutor } from './connection/pool.js';
|
|
2
2
|
import { withTransaction } from './connection/transaction.js';
|
|
3
3
|
import { isUniqueViolation } from './errors/errorChecks.js';
|
|
4
4
|
import { UniqueKeyViolationError } from './errors/customErrors.js';
|
|
5
|
-
export { initDb,
|
|
5
|
+
export { initDb, getQueryExecutor };
|
|
6
6
|
export { withTransaction };
|
|
7
7
|
export { isUniqueViolation };
|
|
8
8
|
export { UniqueKeyViolationError };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const PARENT_FOLDER = 'api/';
|
|
2
2
|
const ___HTTP_STATUS___ = `${PARENT_FOLDER}/httpStatus`;
|
|
3
3
|
const ___CONNECTION___ = `${PARENT_FOLDER}/connection`;
|
|
4
|
+
const ___TRANSACTIONS___ = `${PARENT_FOLDER}/transactions`;
|
|
4
5
|
export const Msg = {
|
|
5
6
|
apiStatus: {
|
|
6
7
|
ok: `${___HTTP_STATUS___}:ok`,
|
|
@@ -23,5 +24,10 @@ export const Msg = {
|
|
|
23
24
|
sshTunnelFailed: `${___CONNECTION___}:sshTunnelFailed`, // ❌ SSH tunnel failed
|
|
24
25
|
postgreConnectionError: `${___CONNECTION___}:postgreConnectionError`, // Unexpected PostgreSQL error
|
|
25
26
|
unauthorizedRequest: `${___CONNECTION___}:unauthorizedRequest`
|
|
27
|
+
},
|
|
28
|
+
transactions: {
|
|
29
|
+
alreadyActive: `${___TRANSACTIONS___}:alreadyActive`,
|
|
30
|
+
noActiveTransactionToCommit: `${___TRANSACTIONS___}:noActiveTransactionToCommit`,
|
|
31
|
+
noActiveTransactionToRollback: `${___TRANSACTIONS___}:noActiveTransactionToRollback`
|
|
26
32
|
}
|
|
27
33
|
};
|