@relq/orm 0.1.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.
Files changed (254) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +236 -0
  3. package/dist/cjs/__test-types.cjs +17 -0
  4. package/dist/cjs/addon/cursor.cjs +1473 -0
  5. package/dist/cjs/addon/pg.cjs +4969 -0
  6. package/dist/cjs/cache/index.cjs +9 -0
  7. package/dist/cjs/cache/query-cache.cjs +311 -0
  8. package/dist/cjs/condition/array-condition-builder.cjs +527 -0
  9. package/dist/cjs/condition/array-numeric-condition-builder.cjs +186 -0
  10. package/dist/cjs/condition/array-specialized-condition-builder.cjs +206 -0
  11. package/dist/cjs/condition/array-string-condition-builder.cjs +146 -0
  12. package/dist/cjs/condition/base-condition-builder.cjs +2 -0
  13. package/dist/cjs/condition/condition-collector.cjs +461 -0
  14. package/dist/cjs/condition/fulltext-condition-builder.cjs +61 -0
  15. package/dist/cjs/condition/geometric-condition-builder.cjs +228 -0
  16. package/dist/cjs/condition/index.cjs +29 -0
  17. package/dist/cjs/condition/jsonb-condition-builder.cjs +448 -0
  18. package/dist/cjs/condition/network-condition-builder.cjs +237 -0
  19. package/dist/cjs/condition/postgis-condition-builder.cjs +188 -0
  20. package/dist/cjs/condition/range-condition-builder.cjs +98 -0
  21. package/dist/cjs/core/helpers/ConnectedAggregateBuilder.cjs +132 -0
  22. package/dist/cjs/core/helpers/ConnectedCTEBuilder.cjs +53 -0
  23. package/dist/cjs/core/helpers/ConnectedCountBuilder.cjs +73 -0
  24. package/dist/cjs/core/helpers/ConnectedDeleteBuilder.cjs +65 -0
  25. package/dist/cjs/core/helpers/ConnectedInsertBuilder.cjs +112 -0
  26. package/dist/cjs/core/helpers/ConnectedInsertFromSelectBuilder.cjs +66 -0
  27. package/dist/cjs/core/helpers/ConnectedQueryBuilder.cjs +146 -0
  28. package/dist/cjs/core/helpers/ConnectedRawQueryBuilder.cjs +46 -0
  29. package/dist/cjs/core/helpers/ConnectedSelectBuilder.cjs +331 -0
  30. package/dist/cjs/core/helpers/ConnectedTransactionBuilder.cjs +105 -0
  31. package/dist/cjs/core/helpers/ConnectedUpdateBuilder.cjs +79 -0
  32. package/dist/cjs/core/helpers/PaginateBuilder.cjs +178 -0
  33. package/dist/cjs/core/helpers/ReturningExecutor.cjs +70 -0
  34. package/dist/cjs/core/helpers/capability-guard.cjs +10 -0
  35. package/dist/cjs/core/helpers/index.cjs +31 -0
  36. package/dist/cjs/core/helpers/methods.cjs +10 -0
  37. package/dist/cjs/core/helpers/query-convenience.cjs +238 -0
  38. package/dist/cjs/core/helpers/select-joins.cjs +251 -0
  39. package/dist/cjs/core/helpers/select-pagination.cjs +233 -0
  40. package/dist/cjs/core/helpers/select-types.cjs +2 -0
  41. package/dist/cjs/core/pg-family/cockroachdb-client/capabilities.cjs +31 -0
  42. package/dist/cjs/core/pg-family/cockroachdb-client/index.cjs +7 -0
  43. package/dist/cjs/core/pg-family/cockroachdb-client/relq-cockroach.cjs +16 -0
  44. package/dist/cjs/core/pg-family/dsql-client/capabilities.cjs +31 -0
  45. package/dist/cjs/core/pg-family/dsql-client/index.cjs +7 -0
  46. package/dist/cjs/core/pg-family/dsql-client/relq-dsql.cjs +16 -0
  47. package/dist/cjs/core/pg-family/index.cjs +19 -0
  48. package/dist/cjs/core/pg-family/nile-client/capabilities.cjs +31 -0
  49. package/dist/cjs/core/pg-family/nile-client/index.cjs +7 -0
  50. package/dist/cjs/core/pg-family/nile-client/relq-nile.cjs +36 -0
  51. package/dist/cjs/core/pg-family/nile-client/tenant-context.cjs +44 -0
  52. package/dist/cjs/core/pg-family/pg-client/capabilities.cjs +31 -0
  53. package/dist/cjs/core/pg-family/pg-client/index.cjs +7 -0
  54. package/dist/cjs/core/pg-family/pg-client/relq-postgres.cjs +43 -0
  55. package/dist/cjs/core/pg-family/shared/pg-base.cjs +385 -0
  56. package/dist/cjs/core/pg-family/shared/pg-dialect.cjs +67 -0
  57. package/dist/cjs/core/pg-family/shared/pg-error-parser.cjs +34 -0
  58. package/dist/cjs/core/pg-family/shared/pg-type-coercion.cjs +14 -0
  59. package/dist/cjs/core/relq-base.cjs +307 -0
  60. package/dist/cjs/core/relq-client.cjs +56 -0
  61. package/dist/cjs/core/shared/cleanup.cjs +36 -0
  62. package/dist/cjs/core/shared/column-mapping.cjs +97 -0
  63. package/dist/cjs/core/shared/errors.cjs +17 -0
  64. package/dist/cjs/core/shared/index.cjs +24 -0
  65. package/dist/cjs/core/shared/table-accessor.cjs +22 -0
  66. package/dist/cjs/core/shared/transform.cjs +35 -0
  67. package/dist/cjs/core/shared/types.cjs +2 -0
  68. package/dist/cjs/core/shared/validation.cjs +140 -0
  69. package/dist/cjs/core/types/core.types.cjs +2 -0
  70. package/dist/cjs/count/count-builder.cjs +88 -0
  71. package/dist/cjs/count/index.cjs +5 -0
  72. package/dist/cjs/delete/delete-builder.cjs +176 -0
  73. package/dist/cjs/delete/index.cjs +5 -0
  74. package/dist/cjs/explain/explain-builder.cjs +99 -0
  75. package/dist/cjs/explain/index.cjs +5 -0
  76. package/dist/cjs/index.cjs +26 -0
  77. package/dist/cjs/insert/conflict-builder.cjs +213 -0
  78. package/dist/cjs/insert/index.cjs +5 -0
  79. package/dist/cjs/insert/insert-builder.cjs +320 -0
  80. package/dist/cjs/insert/insert-from-select-builder.cjs +86 -0
  81. package/dist/cjs/pubsub/index.cjs +7 -0
  82. package/dist/cjs/pubsub/listen-notify-builder.cjs +57 -0
  83. package/dist/cjs/pubsub/listener-connection.cjs +180 -0
  84. package/dist/cjs/raw/index.cjs +8 -0
  85. package/dist/cjs/raw/raw-query-builder.cjs +27 -0
  86. package/dist/cjs/raw/sql-template.cjs +73 -0
  87. package/dist/cjs/select/aggregate-builder.cjs +179 -0
  88. package/dist/cjs/select/index.cjs +16 -0
  89. package/dist/cjs/select/join-builder.cjs +192 -0
  90. package/dist/cjs/select/join-condition-builder.cjs +189 -0
  91. package/dist/cjs/select/join-internals.cjs +5 -0
  92. package/dist/cjs/select/join-many-condition-builder.cjs +159 -0
  93. package/dist/cjs/select/scalar-query-builder.cjs +134 -0
  94. package/dist/cjs/select/scalar-select-builder.cjs +78 -0
  95. package/dist/cjs/select/select-builder.cjs +426 -0
  96. package/dist/cjs/select/sql-expression.cjs +38 -0
  97. package/dist/cjs/select/table-proxy.cjs +99 -0
  98. package/dist/cjs/shared/aws-dsql.cjs +181 -0
  99. package/dist/cjs/shared/errors/relq-errors.cjs +361 -0
  100. package/dist/cjs/shared/pg-format.cjs +383 -0
  101. package/dist/cjs/shared/types/config-types.cjs +51 -0
  102. package/dist/cjs/transaction/index.cjs +6 -0
  103. package/dist/cjs/transaction/transaction-builder.cjs +78 -0
  104. package/dist/cjs/types/aggregate-types.cjs +2 -0
  105. package/dist/cjs/types/inference-types.cjs +18 -0
  106. package/dist/cjs/types/pagination-types.cjs +7 -0
  107. package/dist/cjs/types/result-types.cjs +2 -0
  108. package/dist/cjs/types/scalar-types.cjs +2 -0
  109. package/dist/cjs/types/schema-types.cjs +2 -0
  110. package/dist/cjs/types/subscription-types.cjs +2 -0
  111. package/dist/cjs/types.cjs +2 -0
  112. package/dist/cjs/update/array-update-builder.cjs +232 -0
  113. package/dist/cjs/update/index.cjs +16 -0
  114. package/dist/cjs/update/jsonb-update-builder.cjs +219 -0
  115. package/dist/cjs/update/update-builder.cjs +274 -0
  116. package/dist/cjs/utils/addon/pg/cursor.cjs +8 -0
  117. package/dist/cjs/utils/addon/pg/pg.cjs +23 -0
  118. package/dist/cjs/utils/case-converter.cjs +58 -0
  119. package/dist/cjs/utils/env-resolver.cjs +226 -0
  120. package/dist/cjs/utils/environment-detection.cjs +124 -0
  121. package/dist/cjs/utils/fk-resolver.cjs +186 -0
  122. package/dist/cjs/utils/index.cjs +25 -0
  123. package/dist/cjs/utils/pool-defaults.cjs +91 -0
  124. package/dist/cjs/utils/type-coercion.cjs +120 -0
  125. package/dist/cjs/window/index.cjs +5 -0
  126. package/dist/cjs/window/window-builder.cjs +80 -0
  127. package/dist/esm/__test-types.js +15 -0
  128. package/dist/esm/addon/cursor.js +1440 -0
  129. package/dist/esm/addon/pg.js +4931 -0
  130. package/dist/esm/cache/index.js +1 -0
  131. package/dist/esm/cache/query-cache.js +303 -0
  132. package/dist/esm/condition/array-condition-builder.js +519 -0
  133. package/dist/esm/condition/array-numeric-condition-builder.js +182 -0
  134. package/dist/esm/condition/array-specialized-condition-builder.js +200 -0
  135. package/dist/esm/condition/array-string-condition-builder.js +142 -0
  136. package/dist/esm/condition/base-condition-builder.js +1 -0
  137. package/dist/esm/condition/condition-collector.js +452 -0
  138. package/dist/esm/condition/fulltext-condition-builder.js +53 -0
  139. package/dist/esm/condition/geometric-condition-builder.js +220 -0
  140. package/dist/esm/condition/index.js +8 -0
  141. package/dist/esm/condition/jsonb-condition-builder.js +439 -0
  142. package/dist/esm/condition/network-condition-builder.js +229 -0
  143. package/dist/esm/condition/postgis-condition-builder.js +180 -0
  144. package/dist/esm/condition/range-condition-builder.js +90 -0
  145. package/dist/esm/core/helpers/ConnectedAggregateBuilder.js +128 -0
  146. package/dist/esm/core/helpers/ConnectedCTEBuilder.js +49 -0
  147. package/dist/esm/core/helpers/ConnectedCountBuilder.js +69 -0
  148. package/dist/esm/core/helpers/ConnectedDeleteBuilder.js +61 -0
  149. package/dist/esm/core/helpers/ConnectedInsertBuilder.js +108 -0
  150. package/dist/esm/core/helpers/ConnectedInsertFromSelectBuilder.js +62 -0
  151. package/dist/esm/core/helpers/ConnectedQueryBuilder.js +142 -0
  152. package/dist/esm/core/helpers/ConnectedRawQueryBuilder.js +42 -0
  153. package/dist/esm/core/helpers/ConnectedSelectBuilder.js +327 -0
  154. package/dist/esm/core/helpers/ConnectedTransactionBuilder.js +100 -0
  155. package/dist/esm/core/helpers/ConnectedUpdateBuilder.js +75 -0
  156. package/dist/esm/core/helpers/PaginateBuilder.js +174 -0
  157. package/dist/esm/core/helpers/ReturningExecutor.js +66 -0
  158. package/dist/esm/core/helpers/capability-guard.js +7 -0
  159. package/dist/esm/core/helpers/index.js +13 -0
  160. package/dist/esm/core/helpers/methods.js +6 -0
  161. package/dist/esm/core/helpers/query-convenience.js +194 -0
  162. package/dist/esm/core/helpers/select-joins.js +246 -0
  163. package/dist/esm/core/helpers/select-pagination.js +226 -0
  164. package/dist/esm/core/helpers/select-types.js +1 -0
  165. package/dist/esm/core/pg-family/cockroachdb-client/capabilities.js +28 -0
  166. package/dist/esm/core/pg-family/cockroachdb-client/index.js +2 -0
  167. package/dist/esm/core/pg-family/cockroachdb-client/relq-cockroach.js +12 -0
  168. package/dist/esm/core/pg-family/dsql-client/capabilities.js +28 -0
  169. package/dist/esm/core/pg-family/dsql-client/index.js +2 -0
  170. package/dist/esm/core/pg-family/dsql-client/relq-dsql.js +12 -0
  171. package/dist/esm/core/pg-family/index.js +6 -0
  172. package/dist/esm/core/pg-family/nile-client/capabilities.js +28 -0
  173. package/dist/esm/core/pg-family/nile-client/index.js +2 -0
  174. package/dist/esm/core/pg-family/nile-client/relq-nile.js +32 -0
  175. package/dist/esm/core/pg-family/nile-client/tenant-context.js +40 -0
  176. package/dist/esm/core/pg-family/pg-client/capabilities.js +28 -0
  177. package/dist/esm/core/pg-family/pg-client/index.js +2 -0
  178. package/dist/esm/core/pg-family/pg-client/relq-postgres.js +39 -0
  179. package/dist/esm/core/pg-family/shared/pg-base.js +347 -0
  180. package/dist/esm/core/pg-family/shared/pg-dialect.js +63 -0
  181. package/dist/esm/core/pg-family/shared/pg-error-parser.js +29 -0
  182. package/dist/esm/core/pg-family/shared/pg-type-coercion.js +6 -0
  183. package/dist/esm/core/relq-base.js +270 -0
  184. package/dist/esm/core/relq-client.js +48 -0
  185. package/dist/esm/core/shared/cleanup.js +27 -0
  186. package/dist/esm/core/shared/column-mapping.js +90 -0
  187. package/dist/esm/core/shared/errors.js +13 -0
  188. package/dist/esm/core/shared/index.js +6 -0
  189. package/dist/esm/core/shared/table-accessor.js +19 -0
  190. package/dist/esm/core/shared/transform.js +30 -0
  191. package/dist/esm/core/shared/types.js +1 -0
  192. package/dist/esm/core/shared/validation.js +136 -0
  193. package/dist/esm/core/types/core.types.js +1 -0
  194. package/dist/esm/count/count-builder.js +81 -0
  195. package/dist/esm/count/index.js +1 -0
  196. package/dist/esm/delete/delete-builder.js +169 -0
  197. package/dist/esm/delete/index.js +1 -0
  198. package/dist/esm/explain/explain-builder.js +95 -0
  199. package/dist/esm/explain/index.js +1 -0
  200. package/dist/esm/index.js +7 -0
  201. package/dist/esm/insert/conflict-builder.js +202 -0
  202. package/dist/esm/insert/index.js +1 -0
  203. package/dist/esm/insert/insert-builder.js +313 -0
  204. package/dist/esm/insert/insert-from-select-builder.js +79 -0
  205. package/dist/esm/pubsub/index.js +1 -0
  206. package/dist/esm/pubsub/listen-notify-builder.js +48 -0
  207. package/dist/esm/pubsub/listener-connection.js +173 -0
  208. package/dist/esm/raw/index.js +2 -0
  209. package/dist/esm/raw/raw-query-builder.js +20 -0
  210. package/dist/esm/raw/sql-template.js +66 -0
  211. package/dist/esm/select/aggregate-builder.js +172 -0
  212. package/dist/esm/select/index.js +4 -0
  213. package/dist/esm/select/join-builder.js +184 -0
  214. package/dist/esm/select/join-condition-builder.js +181 -0
  215. package/dist/esm/select/join-internals.js +2 -0
  216. package/dist/esm/select/join-many-condition-builder.js +151 -0
  217. package/dist/esm/select/scalar-query-builder.js +126 -0
  218. package/dist/esm/select/scalar-select-builder.js +70 -0
  219. package/dist/esm/select/select-builder.js +419 -0
  220. package/dist/esm/select/sql-expression.js +33 -0
  221. package/dist/esm/select/table-proxy.js +91 -0
  222. package/dist/esm/shared/aws-dsql.js +140 -0
  223. package/dist/esm/shared/errors/relq-errors.js +339 -0
  224. package/dist/esm/shared/pg-format.js +375 -0
  225. package/dist/esm/shared/types/config-types.js +46 -0
  226. package/dist/esm/transaction/index.js +1 -0
  227. package/dist/esm/transaction/transaction-builder.js +70 -0
  228. package/dist/esm/types/aggregate-types.js +1 -0
  229. package/dist/esm/types/inference-types.js +12 -0
  230. package/dist/esm/types/pagination-types.js +4 -0
  231. package/dist/esm/types/result-types.js +1 -0
  232. package/dist/esm/types/scalar-types.js +1 -0
  233. package/dist/esm/types/schema-types.js +1 -0
  234. package/dist/esm/types/subscription-types.js +1 -0
  235. package/dist/esm/types.js +1 -0
  236. package/dist/esm/update/array-update-builder.js +219 -0
  237. package/dist/esm/update/index.js +3 -0
  238. package/dist/esm/update/jsonb-update-builder.js +211 -0
  239. package/dist/esm/update/update-builder.js +267 -0
  240. package/dist/esm/utils/addon/pg/cursor.js +1 -0
  241. package/dist/esm/utils/addon/pg/pg.js +2 -0
  242. package/dist/esm/utils/case-converter.js +55 -0
  243. package/dist/esm/utils/env-resolver.js +213 -0
  244. package/dist/esm/utils/environment-detection.js +114 -0
  245. package/dist/esm/utils/fk-resolver.js +178 -0
  246. package/dist/esm/utils/index.js +4 -0
  247. package/dist/esm/utils/pool-defaults.js +85 -0
  248. package/dist/esm/utils/type-coercion.js +112 -0
  249. package/dist/esm/window/index.js +1 -0
  250. package/dist/esm/window/window-builder.js +73 -0
  251. package/dist/index.cjs +1 -0
  252. package/dist/index.d.ts +7281 -0
  253. package/dist/index.js +1 -0
  254. package/package.json +52 -0
@@ -0,0 +1,339 @@
1
+ import process from 'node:process';
2
+ export function setupErrorHandler() {
3
+ if (typeof process === 'undefined' || !process.on)
4
+ return;
5
+ process.on('uncaughtException', (error) => {
6
+ if (error instanceof RelqError) {
7
+ const inspect = error[Symbol.for('nodejs.util.inspect.custom')];
8
+ if (typeof inspect === 'function') {
9
+ console.error(inspect.call(error, 0, {}));
10
+ }
11
+ else {
12
+ console.error(`${error.name}: ${error.message}`);
13
+ }
14
+ }
15
+ else {
16
+ console.error(`${error.name || 'Error'}: ${error.message}`);
17
+ if (error.stack) {
18
+ const stackLines = error.stack.split('\n').filter(line => line.trim().startsWith('at '));
19
+ console.error(stackLines.slice(0, 5).join('\n'));
20
+ }
21
+ }
22
+ process.exit();
23
+ });
24
+ process.on('unhandledRejection', (reason) => {
25
+ if (reason instanceof RelqError) {
26
+ const inspect = reason[Symbol.for('nodejs.util.inspect.custom')];
27
+ if (typeof inspect === 'function') {
28
+ console.error(inspect.call(reason, 0, {}));
29
+ }
30
+ else {
31
+ console.error(`${reason.name}: ${reason.message}`);
32
+ }
33
+ }
34
+ else if (reason instanceof Error) {
35
+ console.error(`${reason.name || 'Error'}: ${reason.message}`);
36
+ if (reason.stack) {
37
+ const stackLines = reason.stack.split('\n').filter(line => line.trim().startsWith('at '));
38
+ console.error(stackLines.slice(0, 5).join('\n'));
39
+ }
40
+ }
41
+ else {
42
+ console.error('Unhandled rejection:', reason);
43
+ }
44
+ process.exit();
45
+ });
46
+ }
47
+ export class RelqError extends Error {
48
+ name;
49
+ cause;
50
+ timestamp;
51
+ constructor(message, cause) {
52
+ super(message);
53
+ this.name = 'RelqError';
54
+ this.cause = cause;
55
+ this.timestamp = new Date();
56
+ if (Error.captureStackTrace) {
57
+ Error.captureStackTrace(this, this.constructor);
58
+ }
59
+ }
60
+ [Symbol.for('nodejs.util.inspect.custom')](_depth, _options) {
61
+ const lines = [
62
+ `${this.name}: ${this.message}`,
63
+ ` timestamp: ${this.timestamp.toISOString()}`
64
+ ];
65
+ const props = this._getInspectProps();
66
+ for (const [key, value] of Object.entries(props)) {
67
+ const strVal = value === undefined ? 'undefined' : JSON.stringify(value);
68
+ lines.push(`${key.padStart(10)}: ${strVal}`);
69
+ }
70
+ if (this.stack) {
71
+ const stackLines = this.stack.split('\n').filter(line => line.trim().startsWith('at ') || line.includes(this.name));
72
+ lines.push('');
73
+ lines.push(...stackLines.slice(1, 6));
74
+ }
75
+ return lines.join('\n');
76
+ }
77
+ _getInspectProps() {
78
+ return {};
79
+ }
80
+ toJSON() {
81
+ return {
82
+ name: this.name,
83
+ message: this.message,
84
+ cause: this.cause?.message,
85
+ timestamp: this.timestamp.toISOString(),
86
+ stack: this.stack
87
+ };
88
+ }
89
+ }
90
+ export class RelqConnectionError extends RelqError {
91
+ name = 'RelqConnectionError';
92
+ code;
93
+ host;
94
+ port;
95
+ constructor(message, options) {
96
+ super(message, options?.cause);
97
+ this.code = options?.code;
98
+ this.host = options?.host;
99
+ this.port = options?.port;
100
+ }
101
+ _getInspectProps() {
102
+ return { code: this.code, host: this.host, port: this.port };
103
+ }
104
+ toJSON() {
105
+ return {
106
+ ...super.toJSON(),
107
+ code: this.code,
108
+ host: this.host,
109
+ port: this.port
110
+ };
111
+ }
112
+ }
113
+ export class RelqQueryError extends RelqError {
114
+ name = 'RelqQueryError';
115
+ sql;
116
+ code;
117
+ detail;
118
+ hint;
119
+ constructor(message, options) {
120
+ super(message, options?.cause);
121
+ this.sql = options?.sql;
122
+ this.code = options?.code;
123
+ this.detail = options?.detail;
124
+ this.hint = options?.hint;
125
+ }
126
+ _getInspectProps() {
127
+ return { sql: this.sql, detail: this.detail, hint: this.hint, code: this.code };
128
+ }
129
+ toJSON() {
130
+ return {
131
+ ...super.toJSON(),
132
+ sql: this.sql,
133
+ code: this.code,
134
+ detail: this.detail,
135
+ hint: this.hint
136
+ };
137
+ }
138
+ }
139
+ export class RelqTransactionError extends RelqError {
140
+ name = 'RelqTransactionError';
141
+ operation;
142
+ transactionState;
143
+ constructor(message, operation, options) {
144
+ super(message, options?.cause);
145
+ this.operation = operation;
146
+ this.transactionState = options?.transactionState;
147
+ }
148
+ _getInspectProps() {
149
+ return { operation: this.operation, transactionState: this.transactionState };
150
+ }
151
+ toJSON() {
152
+ return {
153
+ ...super.toJSON(),
154
+ operation: this.operation,
155
+ transactionState: this.transactionState
156
+ };
157
+ }
158
+ }
159
+ export class RelqConfigError extends RelqError {
160
+ name = 'RelqConfigError';
161
+ field;
162
+ value;
163
+ constructor(message, options) {
164
+ super(message);
165
+ this.field = options?.field;
166
+ this.value = options?.value;
167
+ }
168
+ _getInspectProps() {
169
+ return { field: this.field, value: this.value };
170
+ }
171
+ toJSON() {
172
+ return {
173
+ ...super.toJSON(),
174
+ field: this.field,
175
+ value: this.value
176
+ };
177
+ }
178
+ }
179
+ export class RelqTimeoutError extends RelqError {
180
+ name = 'RelqTimeoutError';
181
+ timeout;
182
+ operation;
183
+ constructor(message, timeout, operation, cause) {
184
+ super(message, cause);
185
+ this.timeout = timeout;
186
+ this.operation = operation;
187
+ }
188
+ _getInspectProps() {
189
+ return { timeout: this.timeout, operation: this.operation };
190
+ }
191
+ toJSON() {
192
+ return {
193
+ ...super.toJSON(),
194
+ timeout: this.timeout,
195
+ operation: this.operation
196
+ };
197
+ }
198
+ }
199
+ export class RelqPoolError extends RelqError {
200
+ name = 'RelqPoolError';
201
+ poolSize;
202
+ activeConnections;
203
+ waitingClients;
204
+ constructor(message, options) {
205
+ super(message, options?.cause);
206
+ this.poolSize = options?.poolSize;
207
+ this.activeConnections = options?.activeConnections;
208
+ this.waitingClients = options?.waitingClients;
209
+ }
210
+ _getInspectProps() {
211
+ return { poolSize: this.poolSize, activeConnections: this.activeConnections, waitingClients: this.waitingClients };
212
+ }
213
+ toJSON() {
214
+ return {
215
+ ...super.toJSON(),
216
+ poolSize: this.poolSize,
217
+ activeConnections: this.activeConnections,
218
+ waitingClients: this.waitingClients
219
+ };
220
+ }
221
+ }
222
+ export class RelqEnvironmentError extends RelqError {
223
+ name = 'RelqEnvironmentError';
224
+ environment;
225
+ reason;
226
+ constructor(message, environment, reason) {
227
+ super(message);
228
+ this.environment = environment;
229
+ this.reason = reason;
230
+ }
231
+ _getInspectProps() {
232
+ return { environment: this.environment, reason: this.reason };
233
+ }
234
+ toJSON() {
235
+ return {
236
+ ...super.toJSON(),
237
+ environment: this.environment,
238
+ reason: this.reason
239
+ };
240
+ }
241
+ }
242
+ export class RelqBuilderError extends RelqError {
243
+ name = 'RelqBuilderError';
244
+ builder;
245
+ missing;
246
+ hint;
247
+ constructor(message, options) {
248
+ super(message);
249
+ this.builder = options?.builder;
250
+ this.missing = options?.missing;
251
+ this.hint = options?.hint;
252
+ }
253
+ _getInspectProps() {
254
+ return { builder: this.builder, missing: this.missing, hint: this.hint };
255
+ }
256
+ toJSON() {
257
+ return {
258
+ ...super.toJSON(),
259
+ builder: this.builder,
260
+ missing: this.missing,
261
+ hint: this.hint
262
+ };
263
+ }
264
+ }
265
+ export function isRelqError(error) {
266
+ return error instanceof RelqError;
267
+ }
268
+ export function isRelqConnectionError(error) {
269
+ return error instanceof RelqConnectionError;
270
+ }
271
+ export function isRelqQueryError(error) {
272
+ return error instanceof RelqQueryError;
273
+ }
274
+ export function isRelqBuilderError(error) {
275
+ return error instanceof RelqBuilderError;
276
+ }
277
+ export function wrapError(error, context) {
278
+ if (isRelqError(error)) {
279
+ return error;
280
+ }
281
+ if (error instanceof Error) {
282
+ const message = context ? `${context}: ${error.message}` : error.message;
283
+ return new RelqError(message, error);
284
+ }
285
+ const message = context
286
+ ? `${context}: ${String(error)}`
287
+ : String(error);
288
+ return new RelqError(message);
289
+ }
290
+ export function parsePostgresError(error, sql) {
291
+ const message = error.message || 'Database error';
292
+ const code = error.code;
293
+ if (isNetworkErrorCode(code)) {
294
+ return new RelqConnectionError(message, {
295
+ cause: error,
296
+ code,
297
+ host: error.hostname || error.address,
298
+ port: error.port
299
+ });
300
+ }
301
+ if (code === '57P01' || code === '57P03' || code === '08006' || code === '08001' || code === '08004') {
302
+ return new RelqConnectionError(message, {
303
+ cause: error,
304
+ code,
305
+ host: error.hostname,
306
+ port: error.port
307
+ });
308
+ }
309
+ if (code === '57014') {
310
+ return new RelqTimeoutError('Query execution timed out', error.timeout || 0, 'query', error);
311
+ }
312
+ if (message.includes('timeout exceeded when trying to connect')) {
313
+ return new RelqTimeoutError('Connection timed out', error.timeout || 0, 'connection', error);
314
+ }
315
+ if (!code && (message.includes('Connection terminated') ||
316
+ message.includes('connection is closed') ||
317
+ message.includes('Client has encountered a connection error'))) {
318
+ return new RelqConnectionError(message, {
319
+ cause: error,
320
+ host: error.hostname || error.address,
321
+ port: error.port
322
+ });
323
+ }
324
+ return new RelqQueryError(message, {
325
+ cause: error,
326
+ sql,
327
+ code,
328
+ detail: error.detail,
329
+ hint: error.hint
330
+ });
331
+ }
332
+ const NETWORK_ERROR_CODES = new Set([
333
+ 'ECONNREFUSED', 'ECONNRESET', 'ENOTFOUND', 'ESERVFAIL',
334
+ 'ETIMEDOUT', 'EPIPE', 'EAI_AGAIN', 'EHOSTUNREACH',
335
+ 'CONNECTION_LOST', 'PROTOCOL_CONNECTION_LOST',
336
+ ]);
337
+ function isNetworkErrorCode(code) {
338
+ return !!code && NETWORK_ERROR_CODES.has(code);
339
+ }
@@ -0,0 +1,375 @@
1
+ const reservedMap = {
2
+ "AES128": true,
3
+ "AES256": true,
4
+ "ALL": true,
5
+ "ALLOWOVERWRITE": true,
6
+ "ANALYSE": true,
7
+ "ANALYZE": true,
8
+ "AND": true,
9
+ "ANY": true,
10
+ "ARRAY": true,
11
+ "AS": true,
12
+ "ASC": true,
13
+ "AUTHORIZATION": true,
14
+ "BACKUP": true,
15
+ "BETWEEN": true,
16
+ "BINARY": true,
17
+ "BLANKSASNULL": true,
18
+ "BOTH": true,
19
+ "BYTEDICT": true,
20
+ "CASE": true,
21
+ "CAST": true,
22
+ "CHECK": true,
23
+ "COLLATE": true,
24
+ "COLUMN": true,
25
+ "CONSTRAINT": true,
26
+ "CREATE": true,
27
+ "CREDENTIALS": true,
28
+ "CROSS": true,
29
+ "CURRENT_DATE": true,
30
+ "CURRENT_TIME": true,
31
+ "CURRENT_TIMESTAMP": true,
32
+ "CURRENT_USER": true,
33
+ "CURRENT_USER_ID": true,
34
+ "DEFAULT": true,
35
+ "DEFERRABLE": true,
36
+ "DEFLATE": true,
37
+ "DEFRAG": true,
38
+ "DELTA": true,
39
+ "DELTA32K": true,
40
+ "DESC": true,
41
+ "DISABLE": true,
42
+ "DISTINCT": true,
43
+ "DO": true,
44
+ "ELSE": true,
45
+ "EMPTYASNULL": true,
46
+ "ENABLE": true,
47
+ "ENCODE": true,
48
+ "ENCRYPT": true,
49
+ "ENCRYPTION": true,
50
+ "END": true,
51
+ "EXCEPT": true,
52
+ "EXPLICIT": true,
53
+ "FALSE": true,
54
+ "FOR": true,
55
+ "FOREIGN": true,
56
+ "FREEZE": true,
57
+ "FROM": true,
58
+ "FULL": true,
59
+ "GLOBALDICT256": true,
60
+ "GLOBALDICT64K": true,
61
+ "GRANT": true,
62
+ "GROUP": true,
63
+ "GZIP": true,
64
+ "HAVING": true,
65
+ "IDENTITY": true,
66
+ "IGNORE": true,
67
+ "ILIKE": true,
68
+ "IN": true,
69
+ "INITIALLY": true,
70
+ "INNER": true,
71
+ "INTERSECT": true,
72
+ "INTO": true,
73
+ "IS": true,
74
+ "ISNULL": true,
75
+ "JOIN": true,
76
+ "LEADING": true,
77
+ "LEFT": true,
78
+ "LIKE": true,
79
+ "LIMIT": true,
80
+ "LOCALTIME": true,
81
+ "LOCALTIMESTAMP": true,
82
+ "LUN": true,
83
+ "LUNS": true,
84
+ "LZO": true,
85
+ "LZOP": true,
86
+ "MINUS": true,
87
+ "MOSTLY13": true,
88
+ "MOSTLY32": true,
89
+ "MOSTLY8": true,
90
+ "NATURAL": true,
91
+ "NEW": true,
92
+ "NOT": true,
93
+ "NOTNULL": true,
94
+ "NULL": true,
95
+ "NULLS": true,
96
+ "OFF": true,
97
+ "OFFLINE": true,
98
+ "OFFSET": true,
99
+ "OLD": true,
100
+ "ON": true,
101
+ "ONLY": true,
102
+ "OPEN": true,
103
+ "OR": true,
104
+ "ORDER": true,
105
+ "OUTER": true,
106
+ "OVERLAPS": true,
107
+ "PARALLEL": true,
108
+ "PARTITION": true,
109
+ "PERCENT": true,
110
+ "PLACING": true,
111
+ "PRIMARY": true,
112
+ "RAW": true,
113
+ "READRATIO": true,
114
+ "RECOVER": true,
115
+ "REFERENCES": true,
116
+ "REJECTLOG": true,
117
+ "RESORT": true,
118
+ "RESTORE": true,
119
+ "RIGHT": true,
120
+ "SELECT": true,
121
+ "SESSION_USER": true,
122
+ "SIMILAR": true,
123
+ "SOME": true,
124
+ "SYSDATE": true,
125
+ "SYSTEM": true,
126
+ "TABLE": true,
127
+ "TAG": true,
128
+ "TDES": true,
129
+ "TEXT255": true,
130
+ "TEXT32K": true,
131
+ "THEN": true,
132
+ "TO": true,
133
+ "TOP": true,
134
+ "TRAILING": true,
135
+ "TRUE": true,
136
+ "TRUNCATECOLUMNS": true,
137
+ "UNION": true,
138
+ "UNIQUE": true,
139
+ "USER": true,
140
+ "USING": true,
141
+ "VERBOSE": true,
142
+ "WALLET": true,
143
+ "WHEN": true,
144
+ "WHERE": true,
145
+ "WITH": true,
146
+ "WITHOUT": true,
147
+ };
148
+ const fmtPattern = {
149
+ ident: 'I',
150
+ literal: 'L',
151
+ string: 's',
152
+ };
153
+ function formatDate(date) {
154
+ date = date.replace('T', ' ');
155
+ date = date.replace('Z', '+00');
156
+ return date;
157
+ }
158
+ function isReserved(value) {
159
+ return !!reservedMap[value.toUpperCase()];
160
+ }
161
+ function arrayToList(useSpace, array, formatter) {
162
+ let sql = useSpace ? ' (' : '(';
163
+ for (let i = 0; i < array.length; i++) {
164
+ sql += (i === 0 ? '' : ', ') + formatter(array[i]);
165
+ }
166
+ sql += ')';
167
+ return sql;
168
+ }
169
+ export function quoteIdent(value) {
170
+ if (value === undefined || value === null) {
171
+ throw new Error('SQL identifier cannot be null or undefined');
172
+ }
173
+ else if (value === false) {
174
+ return '"f"';
175
+ }
176
+ else if (value === true) {
177
+ return '"t"';
178
+ }
179
+ else if (value instanceof Date) {
180
+ return '"' + formatDate(value.toISOString()) + '"';
181
+ }
182
+ else if (typeof Buffer !== 'undefined' && value instanceof Buffer) {
183
+ throw new Error('SQL identifier cannot be a buffer');
184
+ }
185
+ else if (Array.isArray(value)) {
186
+ const temp = [];
187
+ for (let i = 0; i < value.length; i++) {
188
+ if (Array.isArray(value[i])) {
189
+ throw new Error('Nested array to grouped list conversion is not supported for SQL identifier');
190
+ }
191
+ else {
192
+ temp.push(quoteIdent(value[i]));
193
+ }
194
+ }
195
+ return temp.toString();
196
+ }
197
+ else if (value === Object(value)) {
198
+ throw new Error('SQL identifier cannot be an object');
199
+ }
200
+ const ident = value.toString().slice(0);
201
+ if (/^[a-z_][a-z0-9_$]*$/.test(ident) && !isReserved(ident)) {
202
+ return ident;
203
+ }
204
+ let quoted = '"';
205
+ for (let i = 0; i < ident.length; i++) {
206
+ const c = ident[i];
207
+ if (c === '"') {
208
+ quoted += c + c;
209
+ }
210
+ else {
211
+ quoted += c;
212
+ }
213
+ }
214
+ quoted += '"';
215
+ return quoted;
216
+ }
217
+ export function quoteLiteral(value) {
218
+ let literal = '';
219
+ let explicitCast = null;
220
+ if (value === undefined || value === null) {
221
+ return 'NULL';
222
+ }
223
+ else if (value === false) {
224
+ return "'f'";
225
+ }
226
+ else if (value === true) {
227
+ return "'t'";
228
+ }
229
+ else if (value instanceof Date) {
230
+ return "'" + formatDate(value.toISOString()) + "'";
231
+ }
232
+ else if (typeof Buffer !== 'undefined' && value instanceof Buffer) {
233
+ return "E'\\\\x" + value.toString('hex') + "'";
234
+ }
235
+ else if (Array.isArray(value)) {
236
+ const temp = [];
237
+ for (let i = 0; i < value.length; i++) {
238
+ if (Array.isArray(value[i])) {
239
+ temp.push(arrayToList(i !== 0, value[i], quoteLiteral));
240
+ }
241
+ else {
242
+ temp.push(quoteLiteral(value[i]));
243
+ }
244
+ }
245
+ return temp.toString();
246
+ }
247
+ else if (value === Object(value)) {
248
+ explicitCast = 'jsonb';
249
+ literal = JSON.stringify(value);
250
+ }
251
+ else {
252
+ literal = value.toString().slice(0);
253
+ }
254
+ let hasBackslash = false;
255
+ let quoted = "'";
256
+ for (let i = 0; i < literal.length; i++) {
257
+ const c = literal[i];
258
+ if (c === "'") {
259
+ quoted += c + c;
260
+ }
261
+ else if (c === '\\') {
262
+ quoted += c + c;
263
+ hasBackslash = true;
264
+ }
265
+ else {
266
+ quoted += c;
267
+ }
268
+ }
269
+ quoted += "'";
270
+ if (hasBackslash) {
271
+ quoted = 'E' + quoted;
272
+ }
273
+ if (explicitCast) {
274
+ quoted += '::' + explicitCast;
275
+ }
276
+ return quoted;
277
+ }
278
+ export function quoteString(value) {
279
+ if (value === undefined || value === null) {
280
+ return '';
281
+ }
282
+ else if (value === false) {
283
+ return 'f';
284
+ }
285
+ else if (value === true) {
286
+ return 't';
287
+ }
288
+ else if (value instanceof Date) {
289
+ return formatDate(value.toISOString());
290
+ }
291
+ else if (typeof Buffer !== 'undefined' && value instanceof Buffer) {
292
+ return '\\x' + value.toString('hex');
293
+ }
294
+ else if (Array.isArray(value)) {
295
+ const temp = [];
296
+ for (let i = 0; i < value.length; i++) {
297
+ if (value[i] !== null && value[i] !== undefined) {
298
+ if (Array.isArray(value[i])) {
299
+ temp.push(arrayToList(i !== 0, value[i], quoteString));
300
+ }
301
+ else {
302
+ temp.push(quoteString(value[i]));
303
+ }
304
+ }
305
+ }
306
+ return temp.toString();
307
+ }
308
+ else if (value === Object(value)) {
309
+ return JSON.stringify(value);
310
+ }
311
+ return value.toString().slice(0);
312
+ }
313
+ export function config(cfg) {
314
+ fmtPattern.ident = 'I';
315
+ fmtPattern.literal = 'L';
316
+ fmtPattern.string = 's';
317
+ if (cfg && cfg.pattern) {
318
+ if (cfg.pattern.ident) {
319
+ fmtPattern.ident = cfg.pattern.ident;
320
+ }
321
+ if (cfg.pattern.literal) {
322
+ fmtPattern.literal = cfg.pattern.literal;
323
+ }
324
+ if (cfg.pattern.string) {
325
+ fmtPattern.string = cfg.pattern.string;
326
+ }
327
+ }
328
+ }
329
+ export function formatWithArray(fmt, parameters) {
330
+ const params = parameters;
331
+ let re = '%(%|(\\d+\\$)?[';
332
+ re += fmtPattern.ident;
333
+ re += fmtPattern.literal;
334
+ re += fmtPattern.string;
335
+ re += '])';
336
+ const regex = new RegExp(re, 'g');
337
+ let index = 0;
338
+ return fmt.replace(regex, function (_, type) {
339
+ if (type === '%') {
340
+ return '%';
341
+ }
342
+ let position = index;
343
+ const tokens = type.split('$');
344
+ if (tokens.length > 1) {
345
+ position = parseInt(tokens[0]) - 1;
346
+ type = tokens[1];
347
+ }
348
+ if (position < 0) {
349
+ throw new Error('specified argument 0 but arguments start at 1');
350
+ }
351
+ else if (position > params.length - 1) {
352
+ throw new Error('too few arguments');
353
+ }
354
+ index = position + 1;
355
+ if (type === fmtPattern.ident) {
356
+ return quoteIdent(params[position]);
357
+ }
358
+ else if (type === fmtPattern.literal) {
359
+ return quoteLiteral(params[position]);
360
+ }
361
+ else if (type === fmtPattern.string) {
362
+ return quoteString(params[position]);
363
+ }
364
+ return _;
365
+ });
366
+ }
367
+ export function format(fmt, ...args) {
368
+ return formatWithArray(fmt, args);
369
+ }
370
+ export default format;
371
+ format.ident = quoteIdent;
372
+ format.literal = quoteLiteral;
373
+ format.string = quoteString;
374
+ format.withArray = formatWithArray;
375
+ format.config = config;