postgrejs 2.22.9 → 2.23.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.
Files changed (216) hide show
  1. package/CHANGELOG.md +18 -1
  2. package/{esm/connection → connection}/bind-param.js +2 -0
  3. package/{esm/connection → connection}/connection.js +4 -2
  4. package/{esm/connection → connection}/cursor.js +8 -3
  5. package/{esm/connection → connection}/intl-connection.js +6 -3
  6. package/{esm/connection → connection}/pool.js +4 -1
  7. package/{esm/connection → connection}/portal.js +3 -1
  8. package/{esm/connection → connection}/prepared-statement.js +6 -3
  9. package/{esm/constants.js → constants.js} +1 -1
  10. package/{types/data-type-map.d.ts → data-type-map.d.ts} +2 -2
  11. package/{esm/data-type-map.js → data-type-map.js} +2 -2
  12. package/{esm/data-types → data-types}/int2-vector-type.js +1 -0
  13. package/{types/data-types → data-types}/timestamptz-type.d.ts +1 -1
  14. package/{esm/data-types → data-types}/timestamptz-type.js +1 -0
  15. package/package.json +12 -31
  16. package/{esm/protocol → protocol}/backend.js +1 -0
  17. package/{esm/protocol → protocol}/buffer-reader.js +2 -1
  18. package/{esm/protocol → protocol}/database-error.js +17 -0
  19. package/{esm/protocol → protocol}/frontend.js +2 -0
  20. package/{types/protocol → protocol}/pg-socket.d.ts +1 -1
  21. package/{esm/protocol → protocol}/pg-socket.js +9 -3
  22. package/{esm/protocol → protocol}/smart-buffer.js +9 -5
  23. package/{esm/util → util}/bigint-methods.js +1 -0
  24. package/cjs/connection/bind-param.js +0 -10
  25. package/cjs/connection/connection.js +0 -295
  26. package/cjs/connection/cursor.js +0 -91
  27. package/cjs/connection/intl-connection.js +0 -258
  28. package/cjs/connection/pool.js +0 -236
  29. package/cjs/connection/portal.js +0 -157
  30. package/cjs/connection/prepared-statement.js +0 -226
  31. package/cjs/constants.js +0 -249
  32. package/cjs/data-type-map.js +0 -131
  33. package/cjs/data-types/bool-type.js +0 -33
  34. package/cjs/data-types/box-type.js +0 -52
  35. package/cjs/data-types/bytea-type.js +0 -28
  36. package/cjs/data-types/char-type.js +0 -27
  37. package/cjs/data-types/circle-type.js +0 -51
  38. package/cjs/data-types/date-type.js +0 -75
  39. package/cjs/data-types/float4-type.js +0 -25
  40. package/cjs/data-types/float8-type.js +0 -25
  41. package/cjs/data-types/int2-type.js +0 -26
  42. package/cjs/data-types/int2-vector-type.js +0 -42
  43. package/cjs/data-types/int4-type.js +0 -28
  44. package/cjs/data-types/int8-type.js +0 -36
  45. package/cjs/data-types/json-type.js +0 -40
  46. package/cjs/data-types/jsonb-type.js +0 -44
  47. package/cjs/data-types/lseg-type.js +0 -55
  48. package/cjs/data-types/numeric-type.js +0 -125
  49. package/cjs/data-types/oid-type.js +0 -32
  50. package/cjs/data-types/point-type.js +0 -42
  51. package/cjs/data-types/time-type.js +0 -66
  52. package/cjs/data-types/timestamp-type.js +0 -89
  53. package/cjs/data-types/timestamptz-type.js +0 -76
  54. package/cjs/data-types/uuid-type.js +0 -39
  55. package/cjs/data-types/varchar-type.js +0 -27
  56. package/cjs/index.js +0 -28
  57. package/cjs/interfaces/command-result.js +0 -2
  58. package/cjs/interfaces/data-mapping-options.js +0 -2
  59. package/cjs/interfaces/data-type.js +0 -2
  60. package/cjs/interfaces/database-connection-params.js +0 -2
  61. package/cjs/interfaces/field-info.js +0 -2
  62. package/cjs/interfaces/query-options.js +0 -2
  63. package/cjs/interfaces/query-result.js +0 -2
  64. package/cjs/interfaces/script-execute-options.js +0 -2
  65. package/cjs/interfaces/script-result.js +0 -2
  66. package/cjs/interfaces/statement-prepare-options.js +0 -2
  67. package/cjs/package.json +0 -3
  68. package/cjs/protocol/backend.js +0 -262
  69. package/cjs/protocol/buffer-reader.js +0 -78
  70. package/cjs/protocol/database-error.js +0 -17
  71. package/cjs/protocol/frontend.js +0 -255
  72. package/cjs/protocol/pg-socket.js +0 -367
  73. package/cjs/protocol/protocol.js +0 -71
  74. package/cjs/protocol/sasl.js +0 -108
  75. package/cjs/protocol/smart-buffer.js +0 -178
  76. package/cjs/safe-event-emitter.js +0 -17
  77. package/cjs/tsconfig-build-cjs.tsbuildinfo +0 -1
  78. package/cjs/types.js +0 -2
  79. package/cjs/util/array-calculatedim.js +0 -18
  80. package/cjs/util/bigint-methods.js +0 -44
  81. package/cjs/util/config-from-env.js +0 -29
  82. package/cjs/util/connection-config.js +0 -61
  83. package/cjs/util/convert-row-to-object.js +0 -12
  84. package/cjs/util/decode-binaryarray.js +0 -38
  85. package/cjs/util/encode-binaryarray.js +0 -44
  86. package/cjs/util/escape-literal.js +0 -26
  87. package/cjs/util/fast-parseint.js +0 -31
  88. package/cjs/util/get-parsers.js +0 -41
  89. package/cjs/util/parse-array.js +0 -60
  90. package/cjs/util/parse-datetime.js +0 -37
  91. package/cjs/util/parse-row.js +0 -11
  92. package/cjs/util/parse-time.js +0 -30
  93. package/cjs/util/stringify-arrayliteral.js +0 -37
  94. package/cjs/util/stringify-for-sql.js +0 -29
  95. package/cjs/util/wrap-row-description.js +0 -34
  96. package/esm/package.json +0 -3
  97. package/types/index.d.cts +0 -25
  98. /package/{types/connection → connection}/bind-param.d.ts +0 -0
  99. /package/{types/connection → connection}/connection.d.ts +0 -0
  100. /package/{types/connection → connection}/cursor.d.ts +0 -0
  101. /package/{types/connection → connection}/intl-connection.d.ts +0 -0
  102. /package/{types/connection → connection}/pool.d.ts +0 -0
  103. /package/{types/connection → connection}/portal.d.ts +0 -0
  104. /package/{types/connection → connection}/prepared-statement.d.ts +0 -0
  105. /package/{types/constants.d.ts → constants.d.ts} +0 -0
  106. /package/{types/data-types → data-types}/bool-type.d.ts +0 -0
  107. /package/{esm/data-types → data-types}/bool-type.js +0 -0
  108. /package/{types/data-types → data-types}/box-type.d.ts +0 -0
  109. /package/{esm/data-types → data-types}/box-type.js +0 -0
  110. /package/{types/data-types → data-types}/bytea-type.d.ts +0 -0
  111. /package/{esm/data-types → data-types}/bytea-type.js +0 -0
  112. /package/{types/data-types → data-types}/char-type.d.ts +0 -0
  113. /package/{esm/data-types → data-types}/char-type.js +0 -0
  114. /package/{types/data-types → data-types}/circle-type.d.ts +0 -0
  115. /package/{esm/data-types → data-types}/circle-type.js +0 -0
  116. /package/{types/data-types → data-types}/date-type.d.ts +0 -0
  117. /package/{esm/data-types → data-types}/date-type.js +0 -0
  118. /package/{types/data-types → data-types}/float4-type.d.ts +0 -0
  119. /package/{esm/data-types → data-types}/float4-type.js +0 -0
  120. /package/{types/data-types → data-types}/float8-type.d.ts +0 -0
  121. /package/{esm/data-types → data-types}/float8-type.js +0 -0
  122. /package/{types/data-types → data-types}/int2-type.d.ts +0 -0
  123. /package/{esm/data-types → data-types}/int2-type.js +0 -0
  124. /package/{types/data-types → data-types}/int2-vector-type.d.ts +0 -0
  125. /package/{types/data-types → data-types}/int4-type.d.ts +0 -0
  126. /package/{esm/data-types → data-types}/int4-type.js +0 -0
  127. /package/{types/data-types → data-types}/int8-type.d.ts +0 -0
  128. /package/{esm/data-types → data-types}/int8-type.js +0 -0
  129. /package/{types/data-types → data-types}/json-type.d.ts +0 -0
  130. /package/{esm/data-types → data-types}/json-type.js +0 -0
  131. /package/{types/data-types → data-types}/jsonb-type.d.ts +0 -0
  132. /package/{esm/data-types → data-types}/jsonb-type.js +0 -0
  133. /package/{types/data-types → data-types}/lseg-type.d.ts +0 -0
  134. /package/{esm/data-types → data-types}/lseg-type.js +0 -0
  135. /package/{types/data-types → data-types}/numeric-type.d.ts +0 -0
  136. /package/{esm/data-types → data-types}/numeric-type.js +0 -0
  137. /package/{types/data-types → data-types}/oid-type.d.ts +0 -0
  138. /package/{esm/data-types → data-types}/oid-type.js +0 -0
  139. /package/{types/data-types → data-types}/point-type.d.ts +0 -0
  140. /package/{esm/data-types → data-types}/point-type.js +0 -0
  141. /package/{types/data-types → data-types}/time-type.d.ts +0 -0
  142. /package/{esm/data-types → data-types}/time-type.js +0 -0
  143. /package/{types/data-types → data-types}/timestamp-type.d.ts +0 -0
  144. /package/{esm/data-types → data-types}/timestamp-type.js +0 -0
  145. /package/{types/data-types → data-types}/uuid-type.d.ts +0 -0
  146. /package/{esm/data-types → data-types}/uuid-type.js +0 -0
  147. /package/{types/data-types → data-types}/varchar-type.d.ts +0 -0
  148. /package/{esm/data-types → data-types}/varchar-type.js +0 -0
  149. /package/{types/index.d.ts → index.d.ts} +0 -0
  150. /package/{esm/index.js → index.js} +0 -0
  151. /package/{types/interfaces → interfaces}/command-result.d.ts +0 -0
  152. /package/{esm/interfaces → interfaces}/command-result.js +0 -0
  153. /package/{types/interfaces → interfaces}/data-mapping-options.d.ts +0 -0
  154. /package/{esm/interfaces → interfaces}/data-mapping-options.js +0 -0
  155. /package/{types/interfaces → interfaces}/data-type.d.ts +0 -0
  156. /package/{esm/interfaces → interfaces}/data-type.js +0 -0
  157. /package/{types/interfaces → interfaces}/database-connection-params.d.ts +0 -0
  158. /package/{esm/interfaces → interfaces}/database-connection-params.js +0 -0
  159. /package/{types/interfaces → interfaces}/field-info.d.ts +0 -0
  160. /package/{esm/interfaces → interfaces}/field-info.js +0 -0
  161. /package/{types/interfaces → interfaces}/query-options.d.ts +0 -0
  162. /package/{esm/interfaces → interfaces}/query-options.js +0 -0
  163. /package/{types/interfaces → interfaces}/query-result.d.ts +0 -0
  164. /package/{esm/interfaces → interfaces}/query-result.js +0 -0
  165. /package/{types/interfaces → interfaces}/script-execute-options.d.ts +0 -0
  166. /package/{esm/interfaces → interfaces}/script-execute-options.js +0 -0
  167. /package/{types/interfaces → interfaces}/script-result.d.ts +0 -0
  168. /package/{esm/interfaces → interfaces}/script-result.js +0 -0
  169. /package/{types/interfaces → interfaces}/statement-prepare-options.d.ts +0 -0
  170. /package/{esm/interfaces → interfaces}/statement-prepare-options.js +0 -0
  171. /package/{types/protocol → protocol}/backend.d.ts +0 -0
  172. /package/{types/protocol → protocol}/buffer-reader.d.ts +0 -0
  173. /package/{types/protocol → protocol}/database-error.d.ts +0 -0
  174. /package/{types/protocol → protocol}/frontend.d.ts +0 -0
  175. /package/{types/protocol → protocol}/protocol.d.ts +0 -0
  176. /package/{esm/protocol → protocol}/protocol.js +0 -0
  177. /package/{types/protocol → protocol}/sasl.d.ts +0 -0
  178. /package/{esm/protocol → protocol}/sasl.js +0 -0
  179. /package/{types/protocol → protocol}/smart-buffer.d.ts +0 -0
  180. /package/{types/safe-event-emitter.d.ts → safe-event-emitter.d.ts} +0 -0
  181. /package/{esm/safe-event-emitter.js → safe-event-emitter.js} +0 -0
  182. /package/{types/types.d.ts → types.d.ts} +0 -0
  183. /package/{esm/types.js → types.js} +0 -0
  184. /package/{types/util → util}/array-calculatedim.d.ts +0 -0
  185. /package/{esm/util → util}/array-calculatedim.js +0 -0
  186. /package/{types/util → util}/bigint-methods.d.ts +0 -0
  187. /package/{types/util → util}/config-from-env.d.ts +0 -0
  188. /package/{esm/util → util}/config-from-env.js +0 -0
  189. /package/{types/util → util}/connection-config.d.ts +0 -0
  190. /package/{esm/util → util}/connection-config.js +0 -0
  191. /package/{types/util → util}/convert-row-to-object.d.ts +0 -0
  192. /package/{esm/util → util}/convert-row-to-object.js +0 -0
  193. /package/{types/util → util}/decode-binaryarray.d.ts +0 -0
  194. /package/{esm/util → util}/decode-binaryarray.js +0 -0
  195. /package/{types/util → util}/encode-binaryarray.d.ts +0 -0
  196. /package/{esm/util → util}/encode-binaryarray.js +0 -0
  197. /package/{types/util → util}/escape-literal.d.ts +0 -0
  198. /package/{esm/util → util}/escape-literal.js +0 -0
  199. /package/{types/util → util}/fast-parseint.d.ts +0 -0
  200. /package/{esm/util → util}/fast-parseint.js +0 -0
  201. /package/{types/util → util}/get-parsers.d.ts +0 -0
  202. /package/{esm/util → util}/get-parsers.js +0 -0
  203. /package/{types/util → util}/parse-array.d.ts +0 -0
  204. /package/{esm/util → util}/parse-array.js +0 -0
  205. /package/{types/util → util}/parse-datetime.d.ts +0 -0
  206. /package/{esm/util → util}/parse-datetime.js +0 -0
  207. /package/{types/util → util}/parse-row.d.ts +0 -0
  208. /package/{esm/util → util}/parse-row.js +0 -0
  209. /package/{types/util → util}/parse-time.d.ts +0 -0
  210. /package/{esm/util → util}/parse-time.js +0 -0
  211. /package/{types/util → util}/stringify-arrayliteral.d.ts +0 -0
  212. /package/{esm/util → util}/stringify-arrayliteral.js +0 -0
  213. /package/{types/util → util}/stringify-for-sql.d.ts +0 -0
  214. /package/{esm/util → util}/stringify-for-sql.js +0 -0
  215. /package/{types/util → util}/wrap-row-description.d.ts +0 -0
  216. /package/{esm/util → util}/wrap-row-description.js +0 -0
@@ -1,295 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Connection = void 0;
4
- const constants_js_1 = require("../constants.js");
5
- const data_type_map_js_1 = require("../data-type-map.js");
6
- const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
7
- const bind_param_js_1 = require("./bind-param.js");
8
- const intl_connection_js_1 = require("./intl-connection.js");
9
- const prepared_statement_js_1 = require("./prepared-statement.js");
10
- class Connection extends safe_event_emitter_js_1.SafeEventEmitter {
11
- constructor(arg0, arg1) {
12
- super();
13
- this._notificationListeners = new safe_event_emitter_js_1.SafeEventEmitter();
14
- this._closing = false;
15
- if (arg0 &&
16
- typeof arg0 === 'object' &&
17
- typeof arg0.acquire === 'function') {
18
- if (!(arg1 instanceof intl_connection_js_1.IntlConnection))
19
- throw new TypeError('Invalid argument');
20
- this._pool = arg0;
21
- this._intlCon = arg1;
22
- }
23
- else {
24
- this._intlCon = new intl_connection_js_1.IntlConnection(arg0);
25
- }
26
- this._intlCon.on('ready', (...args) => this.emit('ready', ...args));
27
- this._intlCon.on('error', (...args) => this.emit('error', ...args));
28
- this._intlCon.on('close', (...args) => this.emit('close', ...args));
29
- this._intlCon.on('connecting', (...args) => this.emit('connecting', ...args));
30
- this._intlCon.on('ready', (...args) => this.emit('ready', ...args));
31
- this._intlCon.on('terminate', (...args) => this.emit('terminate', ...args));
32
- this._intlCon.on('notification', (msg) => this._handleNotification(msg));
33
- }
34
- /**
35
- * Returns configuration object
36
- */
37
- get config() {
38
- return this._intlCon.config;
39
- }
40
- /**
41
- * Returns true if connection is in a transaction
42
- */
43
- get inTransaction() {
44
- return this._intlCon.inTransaction;
45
- }
46
- /**
47
- * Returns current state of the connection
48
- */
49
- get state() {
50
- return this._intlCon.state;
51
- }
52
- /**
53
- * Returns processId of current session
54
- */
55
- get processID() {
56
- return this._intlCon.processID;
57
- }
58
- /**
59
- * Returns information parameters for current session
60
- */
61
- get sessionParameters() {
62
- return this._intlCon.sessionParameters;
63
- }
64
- /**
65
- * Returns secret key of current session
66
- */
67
- get secretKey() {
68
- return this._intlCon.secretKey;
69
- }
70
- /**
71
- * Connects to the server
72
- */
73
- async connect() {
74
- await this._captureErrorStack(this._intlCon.connect());
75
- if (this.state === constants_js_1.ConnectionState.READY)
76
- this._closing = false;
77
- }
78
- /**
79
- * Closes connection. You can define how long time the connection will
80
- * wait for active queries before terminating the connection.
81
- * On the end of the given time, it forces to close the socket and than emits `terminate` event.
82
- *
83
- * @param terminateWait {number} - Determines how long the connection will wait for active queries before terminating.
84
- */
85
- async close(terminateWait) {
86
- this._notificationListeners.removeAllListeners();
87
- this._intlCon.statementQueue.clearQueue();
88
- if (this.state === constants_js_1.ConnectionState.CLOSED || this._closing)
89
- return;
90
- /* istanbul ignore next */
91
- if (this.listenerCount('debug')) {
92
- this.emit('debug', {
93
- location: 'Connection.close',
94
- connection: this,
95
- message: `[${this.processID}] closing`,
96
- });
97
- }
98
- this._closing = true;
99
- if (this._intlCon.refCount > 0 &&
100
- typeof terminateWait === 'number' &&
101
- terminateWait > 0) {
102
- const startTime = Date.now();
103
- return this._captureErrorStack(new Promise((resolve, reject) => {
104
- /* istanbul ignore next */
105
- if (this.listenerCount('debug')) {
106
- this.emit('debug', {
107
- location: 'Connection.close',
108
- connection: this,
109
- message: `[${this.processID}] waiting active queries`,
110
- });
111
- }
112
- const timer = setInterval(() => {
113
- if (this._intlCon.refCount <= 0 ||
114
- Date.now() > startTime + terminateWait) {
115
- clearInterval(timer);
116
- if (this._intlCon.refCount > 0) {
117
- /* istanbul ignore next */
118
- if (this.listenerCount('debug')) {
119
- this.emit('debug', {
120
- location: 'Connection.close',
121
- connection: this,
122
- message: `[${this.processID}] terminate`,
123
- });
124
- }
125
- this.emit('terminate');
126
- }
127
- this._close().then(resolve).catch(reject);
128
- }
129
- }, 50);
130
- }));
131
- }
132
- await this._close();
133
- }
134
- /**
135
- * Executes single or multiple SQL scripts using Simple Query protocol.
136
- *
137
- * @param sql {string} - SQL script that will be executed
138
- * @param options {ScriptExecuteOptions} - Execute options
139
- */
140
- async execute(sql, options) {
141
- this.emit('execute', sql, options);
142
- return this._captureErrorStack(this._intlCon.execute(sql, options)).catch((e) => {
143
- throw this._handleError(e, sql);
144
- });
145
- }
146
- async query(sql, options) {
147
- this._intlCon.assertConnected();
148
- /* istanbul ignore next */
149
- if (this.listenerCount('debug')) {
150
- this.emit('debug', {
151
- location: 'Connection.query',
152
- connection: this,
153
- message: `[${this.processID}] query | ${sql}`,
154
- sql,
155
- });
156
- }
157
- this.emit('query', sql, options);
158
- const typeMap = options?.typeMap || data_type_map_js_1.GlobalTypeMap;
159
- const paramTypes = options?.params?.map(prm => prm instanceof bind_param_js_1.BindParam ? prm.oid : typeMap.determine(prm));
160
- const statement = await this.prepare(sql, { paramTypes, typeMap }).catch((e) => {
161
- throw this._handleError(e, sql);
162
- });
163
- try {
164
- const params = options?.params?.map(prm => prm instanceof bind_param_js_1.BindParam ? prm.value : prm);
165
- return await this._captureErrorStack(statement.execute({ ...options, params }));
166
- }
167
- finally {
168
- await statement.close();
169
- }
170
- }
171
- /**
172
- * Creates a PreparedStatement instance
173
- * @param sql {string} - SQL script that will be executed
174
- * @param options {StatementPrepareOptions} - Options
175
- */
176
- async prepare(sql, options) {
177
- /* istanbul ignore next */
178
- if (this.listenerCount('debug')) {
179
- this.emit('debug', {
180
- location: 'Connection.prepare',
181
- connection: this,
182
- message: `[${this.processID}] prepare | ${sql}`,
183
- sql,
184
- });
185
- }
186
- return await this._captureErrorStack(prepared_statement_js_1.PreparedStatement.prepare(this, sql, options));
187
- }
188
- /**
189
- * Starts a transaction
190
- */
191
- startTransaction() {
192
- return this._captureErrorStack(this._intlCon.startTransaction());
193
- }
194
- /**
195
- * Commits current transaction
196
- */
197
- commit() {
198
- return this._captureErrorStack(this._intlCon.commit());
199
- }
200
- /**
201
- * Rolls back current transaction
202
- */
203
- rollback() {
204
- return this._captureErrorStack(this._intlCon.rollback());
205
- }
206
- /**
207
- * Starts transaction and creates a savepoint
208
- * @param name {string} - Name of the savepoint
209
- */
210
- async savepoint(name) {
211
- if (!this._intlCon.inTransaction)
212
- await this._intlCon.startTransaction();
213
- return this._captureErrorStack(this._intlCon.savepoint(name));
214
- }
215
- /**
216
- * Rolls back current transaction to given savepoint
217
- * @param name {string} - Name of the savepoint
218
- */
219
- rollbackToSavepoint(name) {
220
- return this._captureErrorStack(this._intlCon.rollbackToSavepoint(name));
221
- }
222
- /**
223
- * Releases savepoint
224
- * @param name {string} - Name of the savepoint
225
- */
226
- releaseSavepoint(name) {
227
- return this._captureErrorStack(this._intlCon.releaseSavepoint(name));
228
- }
229
- async listen(channel, callback) {
230
- if (!/^[A-Z]\w+$/i.test(channel))
231
- throw new TypeError(`Invalid channel name`);
232
- const registered = !!this._notificationListeners.eventNames().length;
233
- this._notificationListeners.on(channel, callback);
234
- if (!registered)
235
- await this._captureErrorStack(this.query('LISTEN ' + channel));
236
- }
237
- async unListen(channel) {
238
- if (!/^[A-Z]\w+$/i.test(channel))
239
- throw new TypeError(`Invalid channel name`);
240
- this._notificationListeners.removeAllListeners(channel);
241
- await this._captureErrorStack(this.query('UNLISTEN ' + channel));
242
- }
243
- async unListenAll() {
244
- this._notificationListeners.removeAllListeners();
245
- await this._captureErrorStack(this.query('UNLISTEN *'));
246
- }
247
- _handleNotification(msg) {
248
- this.emit('notification', msg);
249
- this._notificationListeners.emit(msg.channel, msg);
250
- }
251
- async _close() {
252
- if (this._pool) {
253
- await this._captureErrorStack(this._pool.release(this));
254
- this.emit('release');
255
- }
256
- else
257
- await this._captureErrorStack(this._intlCon.close());
258
- this._closing = false;
259
- }
260
- _handleError(err, script) {
261
- if (err.position != null) {
262
- const i1 = script.lastIndexOf('\n', err.position - 1) + 1;
263
- err.lineNr = [...script.substring(0, i1).matchAll(/\n/g)].length + 1;
264
- err.colNr = err.position - i1;
265
- const lines = script.split('\n');
266
- err.line = lines[err.lineNr - 1];
267
- err.message += `\n at line ${err.lineNr} column ${err.colNr}`;
268
- if (err.lineNr > 1)
269
- err.message += `\n${String(err.lineNr - 1).padStart(3)}| ${lines[err.lineNr - 2]}`;
270
- err.message += `\n${String(err.lineNr).padStart(3)}| ${err.line}\n .${'-'.repeat(Math.max(err.colNr - 1, 0))}^`;
271
- }
272
- return err;
273
- }
274
- async _captureErrorStack(promise) {
275
- const stack = new Error().stack;
276
- return promise.catch(e => {
277
- if (e instanceof Error && stack) {
278
- if (e.stack && stack) {
279
- e.stack =
280
- e.stack.substring(0, e.stack.indexOf('\n')) +
281
- '\n' +
282
- stack
283
- .split('\n')
284
- .filter((x, i) => i && !x.includes('._captureErrorStack'))
285
- .join('\n');
286
- }
287
- }
288
- throw e;
289
- });
290
- }
291
- [Symbol.asyncDispose]() {
292
- return this.close();
293
- }
294
- }
295
- exports.Connection = Connection;
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Cursor = void 0;
4
- const tslib_1 = require("tslib");
5
- const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
6
- const power_tasks_1 = require("power-tasks");
7
- const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
8
- const convert_row_to_object_js_1 = require("../util/convert-row-to-object.js");
9
- const parse_row_js_1 = require("../util/parse-row.js");
10
- class Cursor extends safe_event_emitter_js_1.SafeEventEmitter {
11
- constructor(statement, portal, fields, parsers, queryOptions) {
12
- super();
13
- this._taskQueue = new power_tasks_1.TaskQueue({ concurrency: 1 });
14
- this._rows = new doublylinked_1.default();
15
- this._closed = false;
16
- this._statement = statement;
17
- this._portal = portal;
18
- this._parsers = parsers;
19
- this._queryOptions = queryOptions;
20
- this.fields = fields;
21
- }
22
- get rowType() {
23
- return this._queryOptions.objectRows ? 'object' : 'array';
24
- }
25
- get isClosed() {
26
- return this._closed;
27
- }
28
- async next() {
29
- if (!this._rows.length) {
30
- if (this._closed)
31
- return;
32
- await this._fetchRows();
33
- }
34
- return this._rows.shift();
35
- }
36
- async fetch(nRows) {
37
- const out = [];
38
- if (this._closed)
39
- return out;
40
- for (let i = 0; i < nRows; i++) {
41
- if (!this._rows.length)
42
- await this._fetchRows();
43
- if (this._rows.length)
44
- out.push(this._rows.shift());
45
- else
46
- break;
47
- }
48
- return out;
49
- }
50
- async close() {
51
- if (this._closed)
52
- return;
53
- await this._portal.close();
54
- await this._statement.close();
55
- this.emit('close');
56
- this._closed = true;
57
- }
58
- async _fetchRows() {
59
- if (this._closed)
60
- return;
61
- const portal = this._portal;
62
- await this._taskQueue
63
- .enqueue(async () => {
64
- const queryOptions = this._queryOptions;
65
- const r = await portal.execute(queryOptions.fetchCount || 100);
66
- if (r && r.rows && r.rows.length) {
67
- if (this._parsers) {
68
- const objectRows = queryOptions.objectRows;
69
- const fields = this.fields;
70
- const rows = r.rows;
71
- for (let i = 0; i < rows.length; i++) {
72
- const row = rows[i];
73
- (0, parse_row_js_1.parseRow)(this._parsers, row, this._queryOptions);
74
- if (objectRows)
75
- rows[i] = (0, convert_row_to_object_js_1.convertRowToObject)(fields, row);
76
- }
77
- }
78
- this._rows.push(...r.rows);
79
- this.emit('fetch', r.rows);
80
- }
81
- else {
82
- await this.close();
83
- }
84
- })
85
- .toPromise();
86
- }
87
- [Symbol.asyncDispose]() {
88
- return this.close();
89
- }
90
- }
91
- exports.Cursor = Cursor;
@@ -1,258 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IntlConnection = void 0;
4
- exports.getIntlConnection = getIntlConnection;
5
- const power_tasks_1 = require("power-tasks");
6
- const putil_varhelpers_1 = require("putil-varhelpers");
7
- const constants_js_1 = require("../constants.js");
8
- const data_type_map_js_1 = require("../data-type-map.js");
9
- const pg_socket_js_1 = require("../protocol/pg-socket.js");
10
- const protocol_js_1 = require("../protocol/protocol.js");
11
- const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
12
- const connection_config_js_1 = require("../util/connection-config.js");
13
- const convert_row_to_object_js_1 = require("../util/convert-row-to-object.js");
14
- const escape_literal_js_1 = require("../util/escape-literal.js");
15
- const get_parsers_js_1 = require("../util/get-parsers.js");
16
- const parse_row_js_1 = require("../util/parse-row.js");
17
- const wrap_row_description_js_1 = require("../util/wrap-row-description.js");
18
- const DataFormat = protocol_js_1.Protocol.DataFormat;
19
- class IntlConnection extends safe_event_emitter_js_1.SafeEventEmitter {
20
- constructor(config) {
21
- super();
22
- this._refCount = 0;
23
- this.transactionStatus = 'I';
24
- this.statementQueue = new power_tasks_1.TaskQueue({ concurrency: 1 });
25
- this._config = Object.freeze((0, connection_config_js_1.getConnectionConfig)(config));
26
- this.socket = new pg_socket_js_1.PgSocket(this._config);
27
- this.socket.on('error', err => this._onError(err));
28
- this.socket.on('close', () => this.emit('close'));
29
- this.socket.on('notification', payload => this.emit('notification', payload));
30
- this.socket.on('connecting', () => this.emit('connecting'));
31
- this._onErrorSavePoint = 'SP_' + Math.round(Math.random() * 100000000);
32
- }
33
- get config() {
34
- return this._config;
35
- }
36
- get inTransaction() {
37
- return this.transactionStatus === 'T' || this.transactionStatus === 'E';
38
- }
39
- get state() {
40
- return this.socket.state;
41
- }
42
- get refCount() {
43
- return this._refCount;
44
- }
45
- get processID() {
46
- return this.socket.processID;
47
- }
48
- get secretKey() {
49
- return this.socket.secretKey;
50
- }
51
- get sessionParameters() {
52
- return this.socket.sessionParameters;
53
- }
54
- async connect() {
55
- if (this.socket.state === constants_js_1.ConnectionState.READY)
56
- return;
57
- await new Promise((resolve, reject) => {
58
- const handleConnectError = (err) => reject(err);
59
- this.socket.once('ready', () => {
60
- this.socket.removeListener('error', handleConnectError);
61
- resolve();
62
- });
63
- this.socket.once('error', handleConnectError);
64
- this.socket.connect();
65
- });
66
- let startupCommand = '';
67
- if (this.config.schema)
68
- startupCommand +=
69
- 'SET search_path = ' + (0, escape_literal_js_1.escapeLiteral)(this.config.schema) + ';';
70
- if (this.config.timezone)
71
- startupCommand +=
72
- 'SET timezone TO ' + (0, escape_literal_js_1.escapeLiteral)(this.config.timezone) + ';';
73
- if (startupCommand)
74
- await this.execute(startupCommand, { autoCommit: true });
75
- this.emit('ready');
76
- }
77
- async close() {
78
- if (this.state === constants_js_1.ConnectionState.CLOSED)
79
- return;
80
- this.statementQueue.clearQueue();
81
- return new Promise(resolve => {
82
- if (this.socket.state === constants_js_1.ConnectionState.CLOSED)
83
- return;
84
- this.socket.once('close', resolve);
85
- this.socket.sendTerminateMessage(() => {
86
- this.socket.close();
87
- this.emit('close');
88
- });
89
- });
90
- }
91
- async execute(sql, options, cb) {
92
- this.assertConnected();
93
- return this.statementQueue
94
- .enqueue(async () => {
95
- const transactionCommand = sql.match(/^(\bBEGIN\b|\bCOMMIT\b|\bSTART\b|\bROLLBACK|SAVEPOINT|RELEASE\b)/i);
96
- let beginFirst = false;
97
- let commitLast = false;
98
- if (!transactionCommand) {
99
- if (!this.inTransaction &&
100
- (options?.autoCommit != null
101
- ? options?.autoCommit
102
- : this.config.autoCommit) === false) {
103
- beginFirst = true;
104
- }
105
- if (this.inTransaction && options?.autoCommit)
106
- commitLast = true;
107
- }
108
- if (beginFirst)
109
- await this._execute('BEGIN');
110
- const rollbackOnError = !transactionCommand &&
111
- (options?.rollbackOnError != null
112
- ? options.rollbackOnError
113
- : (0, putil_varhelpers_1.coerceToBoolean)(this.config.rollbackOnError, true));
114
- if (this.inTransaction && rollbackOnError)
115
- await this._execute('SAVEPOINT ' + this._onErrorSavePoint);
116
- try {
117
- const result = await this._execute(sql, options, cb);
118
- if (commitLast)
119
- await this._execute('COMMIT');
120
- else if (this.inTransaction && rollbackOnError) {
121
- await this._execute('RELEASE ' + this._onErrorSavePoint + ';');
122
- }
123
- return result;
124
- }
125
- catch (e) {
126
- if (this.inTransaction && rollbackOnError)
127
- await this._execute('ROLLBACK TO ' + this._onErrorSavePoint + ';');
128
- throw e;
129
- }
130
- })
131
- .toPromise();
132
- }
133
- async startTransaction() {
134
- if (!this.inTransaction)
135
- await this.execute('BEGIN');
136
- }
137
- async savepoint(name) {
138
- if (!(name && name.match(/^[a-zA-Z]\w+$/)))
139
- throw new Error(`Invalid savepoint "${name}"`);
140
- await this.execute('BEGIN; SAVEPOINT ' + name);
141
- }
142
- async commit() {
143
- if (this.inTransaction)
144
- await this.execute('COMMIT');
145
- }
146
- async rollback() {
147
- if (this.inTransaction)
148
- await this.execute('ROLLBACK');
149
- }
150
- async rollbackToSavepoint(name) {
151
- if (!(name && name.match(/^[a-zA-Z]\w+$/)))
152
- throw new Error(`Invalid savepoint "${name}"`);
153
- await this.execute('ROLLBACK TO SAVEPOINT ' + name, { autoCommit: false });
154
- }
155
- async releaseSavepoint(name) {
156
- if (!(name && name.match(/^[a-zA-Z]\w+$/)))
157
- throw new Error(`Invalid savepoint "${name}"`);
158
- await this.execute('RELEASE SAVEPOINT ' + name, { autoCommit: false });
159
- }
160
- ref() {
161
- this._refCount++;
162
- }
163
- unref() {
164
- this._refCount--;
165
- return !this._refCount;
166
- }
167
- assertConnected() {
168
- if (this.state === constants_js_1.ConnectionState.CLOSING)
169
- throw new Error('Connection is closing');
170
- if (this.state === constants_js_1.ConnectionState.CLOSED)
171
- throw new Error('Connection closed');
172
- }
173
- async _execute(sql, options, cb) {
174
- this.ref();
175
- try {
176
- const startTime = Date.now();
177
- const result = {
178
- totalCommands: 0,
179
- totalTime: 0,
180
- results: [],
181
- };
182
- const opts = options || {};
183
- this.socket.sendQueryMessage(sql);
184
- let currentStart = Date.now();
185
- let parsers;
186
- let current = { command: undefined };
187
- let fields;
188
- const typeMap = opts.typeMap || data_type_map_js_1.GlobalTypeMap;
189
- return await this.socket.capture(async (code, msg, done) => {
190
- switch (code) {
191
- case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse:
192
- case protocol_js_1.Protocol.BackendMessageCode.CopyInResponse:
193
- case protocol_js_1.Protocol.BackendMessageCode.CopyOutResponse:
194
- case protocol_js_1.Protocol.BackendMessageCode.EmptyQueryResponse:
195
- break;
196
- case protocol_js_1.Protocol.BackendMessageCode.RowDescription:
197
- fields = msg.fields;
198
- parsers = (0, get_parsers_js_1.getParsers)(typeMap, fields);
199
- current.fields = (0, wrap_row_description_js_1.wrapRowDescription)(typeMap, fields, DataFormat.text);
200
- current.rows = [];
201
- break;
202
- case protocol_js_1.Protocol.BackendMessageCode.DataRow:
203
- {
204
- let row = msg.columns.map((x) => x.toString('utf8'));
205
- // The null override assumes we can trust PG to always send the RowDescription first
206
- (0, parse_row_js_1.parseRow)(parsers, row, opts);
207
- if (opts.objectRows && current.fields)
208
- row = (0, convert_row_to_object_js_1.convertRowToObject)(current.fields, row);
209
- if (cb)
210
- cb('row', row);
211
- current.rows = current.rows || [];
212
- current.rows.push(row);
213
- }
214
- break;
215
- case protocol_js_1.Protocol.BackendMessageCode.CommandComplete:
216
- // Ignore BEGIN command that we added to sql
217
- current.command = msg.command;
218
- if (current.command === 'DELETE' ||
219
- current.command === 'INSERT' ||
220
- current.command === 'UPDATE') {
221
- current.rowsAffected = msg.rowCount;
222
- }
223
- current.executeTime = Date.now() - currentStart;
224
- if (current.rows)
225
- current.rowType =
226
- opts.objectRows && current.fields ? 'object' : 'array';
227
- result.results.push(current);
228
- if (cb)
229
- cb('command-complete', current);
230
- current = { command: undefined };
231
- currentStart = Date.now();
232
- break;
233
- case protocol_js_1.Protocol.BackendMessageCode.ReadyForQuery:
234
- this.transactionStatus = msg.status;
235
- result.totalTime = Date.now() - startTime;
236
- // Ignore COMMIT command that we added to sql
237
- result.totalCommands = result.results.length;
238
- done(undefined, result);
239
- break;
240
- default:
241
- break;
242
- }
243
- });
244
- }
245
- finally {
246
- this.unref();
247
- }
248
- }
249
- _onError(err) {
250
- if (this.socket.state !== constants_js_1.ConnectionState.READY)
251
- return;
252
- this.emit('error', err);
253
- }
254
- }
255
- exports.IntlConnection = IntlConnection;
256
- function getIntlConnection(connection) {
257
- return connection._intlCon;
258
- }