@m2k-5f/pgtx 2.8.0 → 2.8.2
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 +107 -47
- package/dist/clauses/array.clause.d.ts.map +1 -1
- package/dist/clauses/array.clause.js +0 -5
- package/dist/clauses/iden.caluse.d.ts.map +1 -1
- package/dist/clauses/iden.caluse.js +0 -2
- package/dist/clauses/insert.clause.d.ts.map +1 -1
- package/dist/clauses/insert.clause.js +5 -6
- package/dist/clauses/literal.clause.d.ts.map +1 -1
- package/dist/clauses/literal.clause.js +0 -3
- package/dist/clauses/update.clause.d.ts.map +1 -1
- package/dist/clauses/update.clause.js +0 -2
- package/dist/clauses/where.clause.d.ts.map +1 -1
- package/dist/clauses/where.clause.js +0 -2
- package/dist/connection.d.ts +7 -3
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +147 -116
- package/dist/error.d.ts +1 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +40 -9
- package/dist/index.d.ts +30 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/pool.d.ts +2 -0
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +6 -0
- package/dist/protocol/connection-request-writer.d.ts +1 -1
- package/dist/protocol/connection-request-writer.d.ts.map +1 -1
- package/dist/protocol/connection-request-writer.js +1 -3
- package/dist/protocol/connection-response-reader.d.ts +1 -1
- package/dist/protocol/connection-response-reader.js +1 -1
- package/dist/protocol/constants.js +8 -8
- package/dist/protocol/types.d.ts +3 -1
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +91 -1
- package/dist/query.d.ts +9 -11
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +8 -10
- package/dist/transaction.d.ts +10 -2
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +78 -23
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +0 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -46
- package/package.json +1 -1
package/dist/transaction.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Begin, Future } from "fluent-future";
|
|
2
2
|
import { IdentifierClause } from "./clauses";
|
|
3
|
-
import { ErrTransactionClosed } from "./error";
|
|
3
|
+
import { ErrConnectionClosed, ErrTransactionClosed } from "./error";
|
|
4
|
+
import { compileSqlTemplate } from "./utils";
|
|
4
5
|
/**
|
|
5
6
|
* Represents an active SQL transaction.
|
|
6
7
|
* All queries are executed on a single dedicated connection.
|
|
@@ -20,29 +21,36 @@ export class Transaction {
|
|
|
20
21
|
* Commits the current transaction.
|
|
21
22
|
*/
|
|
22
23
|
commit() {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return this.conn.query `COMMIT`
|
|
26
|
-
.tap(() => this.isFinished = true)
|
|
27
|
-
.map(() => { });
|
|
24
|
+
return this.execute `COMMIT`
|
|
25
|
+
.tap(() => this.isFinished = true);
|
|
28
26
|
}
|
|
29
27
|
/**
|
|
30
28
|
* Rolls back the current transaction.
|
|
31
29
|
*/
|
|
32
30
|
rollback() {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return this.conn.query `ROLLBACK`
|
|
36
|
-
.tap(() => this.isFinished = true)
|
|
37
|
-
.map(() => { });
|
|
31
|
+
return this.execute `ROLLBACK`
|
|
32
|
+
.tap(() => this.isFinished = true);
|
|
38
33
|
}
|
|
39
34
|
/**
|
|
40
35
|
* Executes a query within the current transaction.
|
|
41
36
|
*/
|
|
42
|
-
query(
|
|
43
|
-
if (this.isFinished)
|
|
37
|
+
query(templates, ...params) {
|
|
38
|
+
if (this.isFinished) {
|
|
39
|
+
this.conn['_logError'](ErrTransactionClosed);
|
|
44
40
|
return Future.reject(ErrTransactionClosed);
|
|
45
|
-
|
|
41
|
+
}
|
|
42
|
+
if (this.conn.isClosed) {
|
|
43
|
+
this.conn['_logError'](ErrConnectionClosed);
|
|
44
|
+
return Future.reject(ErrConnectionClosed);
|
|
45
|
+
}
|
|
46
|
+
const { text, args } = compileSqlTemplate(templates, params);
|
|
47
|
+
const resolvers = Future.withResolvers();
|
|
48
|
+
if (this.conn['_reconnecting']) {
|
|
49
|
+
return this.conn['_reconnecting']
|
|
50
|
+
.tapErr(err => this.conn['_logError'](err))
|
|
51
|
+
.andThen(() => this.conn['_performQuery'](text, args, resolvers));
|
|
52
|
+
}
|
|
53
|
+
return this.conn['_performQuery'](text, args, resolvers);
|
|
46
54
|
}
|
|
47
55
|
/**
|
|
48
56
|
* Like {@link query}, but for statements that don't return rows (INSERT/UPDATE/DDL/etc).
|
|
@@ -50,10 +58,59 @@ export class Transaction {
|
|
|
50
58
|
* @example
|
|
51
59
|
* await tx.execute`UPDATE users SET name = ${name} WHERE id = ${id}`
|
|
52
60
|
*/
|
|
53
|
-
execute(
|
|
54
|
-
if (this.isFinished)
|
|
61
|
+
execute(templates, ...params) {
|
|
62
|
+
if (this.isFinished) {
|
|
63
|
+
this.conn['_logError'](ErrTransactionClosed);
|
|
55
64
|
return Future.reject(ErrTransactionClosed);
|
|
56
|
-
|
|
65
|
+
}
|
|
66
|
+
if (this.conn.isClosed) {
|
|
67
|
+
this.conn['_logError'](ErrConnectionClosed);
|
|
68
|
+
return Future.reject(ErrConnectionClosed);
|
|
69
|
+
}
|
|
70
|
+
const { text, args } = compileSqlTemplate(templates, params);
|
|
71
|
+
const resolvers = Future.withResolvers();
|
|
72
|
+
if (this.conn['_reconnecting']) {
|
|
73
|
+
return this.conn['_reconnecting']
|
|
74
|
+
.tapErr(err => this.conn['_logError'](err))
|
|
75
|
+
.andThen(() => this.conn['_performExecute'](text, args, resolvers));
|
|
76
|
+
}
|
|
77
|
+
return this.conn['_performExecute'](text, args, resolvers);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Streams query results as a `ReadableStream`, without buffering rows in memory.
|
|
81
|
+
* Ideal for large result sets or piping straight into an HTTP response.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* for await (const row of conn.stream<User>`SELECT * FROM orders`) { ... }
|
|
85
|
+
*/
|
|
86
|
+
stream(templates, ...params) {
|
|
87
|
+
if (this.isFinished) {
|
|
88
|
+
this.conn['_logError'](ErrTransactionClosed);
|
|
89
|
+
throw ErrTransactionClosed;
|
|
90
|
+
}
|
|
91
|
+
if (this.conn.isClosed) {
|
|
92
|
+
this.conn['_logError'](ErrConnectionClosed);
|
|
93
|
+
throw ErrConnectionClosed;
|
|
94
|
+
}
|
|
95
|
+
const { text, args } = compileSqlTemplate(templates, params);
|
|
96
|
+
let controller;
|
|
97
|
+
const stream = new ReadableStream({
|
|
98
|
+
start: c => {
|
|
99
|
+
controller = c;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (this.conn['_reconnecting']) {
|
|
103
|
+
this.conn['_reconnecting']
|
|
104
|
+
.tap(() => this.conn['_performStream'](text, args, controller))
|
|
105
|
+
.tapErr(err => {
|
|
106
|
+
this.conn['_logError'](err);
|
|
107
|
+
controller.error(err);
|
|
108
|
+
})
|
|
109
|
+
.recover();
|
|
110
|
+
return stream;
|
|
111
|
+
}
|
|
112
|
+
this.conn['_performStream'](text, args, controller);
|
|
113
|
+
return stream;
|
|
57
114
|
}
|
|
58
115
|
/**
|
|
59
116
|
* Creates a sub-transaction using PostgreSQL SAVEPOINT.
|
|
@@ -66,12 +123,10 @@ export class Transaction {
|
|
|
66
123
|
* });
|
|
67
124
|
*/
|
|
68
125
|
savepoint(name, callback) {
|
|
69
|
-
if (this.isFinished)
|
|
70
|
-
return Future.reject(ErrTransactionClosed);
|
|
71
126
|
return Begin()
|
|
72
|
-
.andThen(() => this.
|
|
73
|
-
.andThen(() => Future.of(callback(this))
|
|
74
|
-
.tap(() => this.
|
|
75
|
-
.tapErr(() => this.
|
|
127
|
+
.andThen(() => this.query `SAVEPOINT ${IdentifierClause.create(name)}`)
|
|
128
|
+
.andThen(() => Future.of(() => callback(this))
|
|
129
|
+
.tap(() => this.query `RELEASE SAVEPOINT ${IdentifierClause.create(name)}`)
|
|
130
|
+
.tapErr(() => this.query `ROLLBACK TO SAVEPOINT ${IdentifierClause.create(name)}`));
|
|
76
131
|
}
|
|
77
132
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export type ConnectionPartialConfig = {
|
|
|
29
29
|
logLevel?: LogLevel;
|
|
30
30
|
int8toBigint?: boolean;
|
|
31
31
|
queryTimeout?: number;
|
|
32
|
-
|
|
32
|
+
syncSсhedule?: "beforeMicrotask" | "afterMicrotask" | "Immediate";
|
|
33
33
|
ssl?: SSLMode;
|
|
34
34
|
caPath?: string;
|
|
35
35
|
};
|
|
@@ -42,7 +42,7 @@ export type ConnectionConfig = {
|
|
|
42
42
|
logLevel: LogLevel;
|
|
43
43
|
int8toBigint: boolean;
|
|
44
44
|
queryTimeout: number;
|
|
45
|
-
|
|
45
|
+
syncSсhedule: "beforeMicrotask" | "afterMicrotask" | "Immediate";
|
|
46
46
|
ssl: SSLMode;
|
|
47
47
|
caPath?: string;
|
|
48
48
|
};
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAElD,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,KAAK,IAAI,CAAC,GAAG;IAAC,OAAO,EAAE,KAAK,CAAA;CAAC,CAAA;AAEpD,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAA;AAEnE,MAAM,MAAM,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IAC/D,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,KAAK,IAAI,CAAA;CACtE,CAAA"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -3,6 +3,4 @@ export declare function compileSqlTemplate(templates: TemplateStringsArray, args
|
|
|
3
3
|
args: unknown[];
|
|
4
4
|
text: QueryText;
|
|
5
5
|
};
|
|
6
|
-
export declare function prepareObject(obj: any): string;
|
|
7
|
-
export declare function prepareValue(value: unknown): string | null;
|
|
8
6
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAwB,SAAS,EAAE,MAAM,SAAS,CAAA;AAIzD,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,oBAAoB,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,SAAS,SAAI;;UAGpD,SAAS;EA2BtD"}
|
package/dist/utils.js
CHANGED
|
@@ -18,10 +18,6 @@ export function compileSqlTemplate(templates, args, argOffset = 0) {
|
|
|
18
18
|
value.mapIntoQuery(query);
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
|
-
if (value === undefined) {
|
|
22
|
-
throw new TypeError(`Query parameter at position ${query.args.length + argOffset + 1} is undefined.
|
|
23
|
-
Use null if you want NULL in SQL, or ensure the value is defined.`);
|
|
24
|
-
}
|
|
25
21
|
query.args.push(value);
|
|
26
22
|
query.text += `$${query.args.length + argOffset}`;
|
|
27
23
|
}
|
|
@@ -29,45 +25,3 @@ export function compileSqlTemplate(templates, args, argOffset = 0) {
|
|
|
29
25
|
!args.some(value => value instanceof Clause) && cache.set(templates, query.text);
|
|
30
26
|
return query;
|
|
31
27
|
}
|
|
32
|
-
export function prepareObject(obj) {
|
|
33
|
-
if (obj && typeof obj.toPG === "function") {
|
|
34
|
-
return JSON.stringify(obj.toPG());
|
|
35
|
-
}
|
|
36
|
-
return JSON.stringify(obj);
|
|
37
|
-
}
|
|
38
|
-
export function prepareValue(value) {
|
|
39
|
-
if (value === null || value === undefined) {
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
if (typeof value === "object") {
|
|
43
|
-
if (value instanceof Date) {
|
|
44
|
-
return value.toISOString();
|
|
45
|
-
}
|
|
46
|
-
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
|
|
47
|
-
return "\\x" + Buffer
|
|
48
|
-
.from(value.buffer, value.byteOffset, value.byteLength)
|
|
49
|
-
.toString("hex");
|
|
50
|
-
}
|
|
51
|
-
if (Array.isArray(value)) {
|
|
52
|
-
const elements = value.map(el => {
|
|
53
|
-
if (el === null || el === undefined)
|
|
54
|
-
return "NULL";
|
|
55
|
-
if (typeof el === "boolean")
|
|
56
|
-
return el ? "true" : "false";
|
|
57
|
-
if (typeof el === "object") {
|
|
58
|
-
const res = prepareValue(el);
|
|
59
|
-
return `"${res?.replace(/"/g, '\\"')}"`;
|
|
60
|
-
}
|
|
61
|
-
if (typeof el === "string") {
|
|
62
|
-
return `"${el
|
|
63
|
-
.replace(/\\/g, "\\\\")
|
|
64
|
-
.replace(/"/g, '\\"')}"`;
|
|
65
|
-
}
|
|
66
|
-
return String(el);
|
|
67
|
-
});
|
|
68
|
-
return `{${elements.join(",")}}`;
|
|
69
|
-
}
|
|
70
|
-
return prepareObject(value);
|
|
71
|
-
}
|
|
72
|
-
return String(value);
|
|
73
|
-
}
|