relq 1.0.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 (305) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +862 -0
  3. package/dist/addons/buffer.js +1869 -0
  4. package/dist/addons/pg-cursor.js +1425 -0
  5. package/dist/addons/pg-format.js +2248 -0
  6. package/dist/addons/pg.js +4790 -0
  7. package/dist/bin/relq.js +2 -0
  8. package/dist/cjs/cache/index.cjs +9 -0
  9. package/dist/cjs/cache/query-cache.cjs +311 -0
  10. package/dist/cjs/cli/commands/add.cjs +82 -0
  11. package/dist/cjs/cli/commands/commit.cjs +145 -0
  12. package/dist/cjs/cli/commands/diff.cjs +84 -0
  13. package/dist/cjs/cli/commands/export.cjs +333 -0
  14. package/dist/cjs/cli/commands/fetch.cjs +59 -0
  15. package/dist/cjs/cli/commands/generate.cjs +242 -0
  16. package/dist/cjs/cli/commands/history.cjs +165 -0
  17. package/dist/cjs/cli/commands/import.cjs +524 -0
  18. package/dist/cjs/cli/commands/init.cjs +437 -0
  19. package/dist/cjs/cli/commands/introspect.cjs +142 -0
  20. package/dist/cjs/cli/commands/log.cjs +62 -0
  21. package/dist/cjs/cli/commands/migrate.cjs +167 -0
  22. package/dist/cjs/cli/commands/pull.cjs +410 -0
  23. package/dist/cjs/cli/commands/push.cjs +165 -0
  24. package/dist/cjs/cli/commands/rollback.cjs +169 -0
  25. package/dist/cjs/cli/commands/status.cjs +110 -0
  26. package/dist/cjs/cli/commands/sync.cjs +79 -0
  27. package/dist/cjs/cli/index.cjs +275 -0
  28. package/dist/cjs/cli/utils/change-tracker.cjs +446 -0
  29. package/dist/cjs/cli/utils/commit-manager.cjs +239 -0
  30. package/dist/cjs/cli/utils/config-loader.cjs +127 -0
  31. package/dist/cjs/cli/utils/env-loader.cjs +62 -0
  32. package/dist/cjs/cli/utils/fast-introspect.cjs +398 -0
  33. package/dist/cjs/cli/utils/git-utils.cjs +404 -0
  34. package/dist/cjs/cli/utils/migration-generator.cjs +269 -0
  35. package/dist/cjs/cli/utils/relqignore.cjs +114 -0
  36. package/dist/cjs/cli/utils/repo-manager.cjs +515 -0
  37. package/dist/cjs/cli/utils/schema-comparator.cjs +313 -0
  38. package/dist/cjs/cli/utils/schema-diff.cjs +284 -0
  39. package/dist/cjs/cli/utils/schema-hash.cjs +108 -0
  40. package/dist/cjs/cli/utils/schema-introspect.cjs +455 -0
  41. package/dist/cjs/cli/utils/snapshot-manager.cjs +223 -0
  42. package/dist/cjs/cli/utils/spinner.cjs +108 -0
  43. package/dist/cjs/cli/utils/sql-generator.cjs +520 -0
  44. package/dist/cjs/cli/utils/sql-parser.cjs +999 -0
  45. package/dist/cjs/cli/utils/type-generator.cjs +2061 -0
  46. package/dist/cjs/condition/array-condition-builder.cjs +503 -0
  47. package/dist/cjs/condition/array-numeric-condition-builder.cjs +186 -0
  48. package/dist/cjs/condition/array-specialized-condition-builder.cjs +206 -0
  49. package/dist/cjs/condition/array-string-condition-builder.cjs +146 -0
  50. package/dist/cjs/condition/base-condition-builder.cjs +2 -0
  51. package/dist/cjs/condition/condition-collector.cjs +284 -0
  52. package/dist/cjs/condition/fulltext-condition-builder.cjs +61 -0
  53. package/dist/cjs/condition/geometric-condition-builder.cjs +208 -0
  54. package/dist/cjs/condition/index.cjs +25 -0
  55. package/dist/cjs/condition/jsonb-condition-builder.cjs +160 -0
  56. package/dist/cjs/condition/network-condition-builder.cjs +230 -0
  57. package/dist/cjs/condition/range-condition-builder.cjs +82 -0
  58. package/dist/cjs/config/config.cjs +190 -0
  59. package/dist/cjs/config/index.cjs +9 -0
  60. package/dist/cjs/constants/pg-values.cjs +68 -0
  61. package/dist/cjs/copy/copy-builder.cjs +316 -0
  62. package/dist/cjs/copy/index.cjs +6 -0
  63. package/dist/cjs/core/query-builder.cjs +440 -0
  64. package/dist/cjs/core/relq-client.cjs +1831 -0
  65. package/dist/cjs/core/typed-kuery-client.cjs +2 -0
  66. package/dist/cjs/count/count-builder.cjs +88 -0
  67. package/dist/cjs/count/index.cjs +5 -0
  68. package/dist/cjs/cte/cte-builder.cjs +89 -0
  69. package/dist/cjs/cte/index.cjs +5 -0
  70. package/dist/cjs/ddl/function.cjs +48 -0
  71. package/dist/cjs/ddl/index.cjs +7 -0
  72. package/dist/cjs/ddl/sql.cjs +54 -0
  73. package/dist/cjs/delete/delete-builder.cjs +135 -0
  74. package/dist/cjs/delete/index.cjs +5 -0
  75. package/dist/cjs/errors/relq-errors.cjs +329 -0
  76. package/dist/cjs/examples/fulltext-search-test.cjs +122 -0
  77. package/dist/cjs/explain/explain-builder.cjs +99 -0
  78. package/dist/cjs/explain/index.cjs +5 -0
  79. package/dist/cjs/function/create-function-builder.cjs +196 -0
  80. package/dist/cjs/function/index.cjs +6 -0
  81. package/dist/cjs/functions/advanced-functions.cjs +241 -0
  82. package/dist/cjs/functions/case-builder.cjs +66 -0
  83. package/dist/cjs/functions/geometric-functions.cjs +104 -0
  84. package/dist/cjs/functions/index.cjs +184 -0
  85. package/dist/cjs/functions/network-functions.cjs +86 -0
  86. package/dist/cjs/functions/sql-functions.cjs +431 -0
  87. package/dist/cjs/index.cjs +164 -0
  88. package/dist/cjs/indexing/create-index-builder.cjs +187 -0
  89. package/dist/cjs/indexing/drop-index-builder.cjs +89 -0
  90. package/dist/cjs/indexing/index-types.cjs +2 -0
  91. package/dist/cjs/indexing/index.cjs +8 -0
  92. package/dist/cjs/insert/conflict-builder.cjs +173 -0
  93. package/dist/cjs/insert/index.cjs +5 -0
  94. package/dist/cjs/insert/insert-builder.cjs +254 -0
  95. package/dist/cjs/introspect/index.cjs +229 -0
  96. package/dist/cjs/maintenance/index.cjs +6 -0
  97. package/dist/cjs/maintenance/vacuum-builder.cjs +166 -0
  98. package/dist/cjs/pubsub/index.cjs +7 -0
  99. package/dist/cjs/pubsub/listen-notify-builder.cjs +57 -0
  100. package/dist/cjs/pubsub/listener-connection.cjs +180 -0
  101. package/dist/cjs/raw/index.cjs +5 -0
  102. package/dist/cjs/raw/raw-query-builder.cjs +27 -0
  103. package/dist/cjs/schema/index.cjs +15 -0
  104. package/dist/cjs/schema/schema-builder.cjs +1167 -0
  105. package/dist/cjs/schema-builder.cjs +21 -0
  106. package/dist/cjs/schema-definition/column-types.cjs +829 -0
  107. package/dist/cjs/schema-definition/index.cjs +62 -0
  108. package/dist/cjs/schema-definition/introspection.cjs +620 -0
  109. package/dist/cjs/schema-definition/partitions.cjs +129 -0
  110. package/dist/cjs/schema-definition/pg-enum.cjs +76 -0
  111. package/dist/cjs/schema-definition/pg-function.cjs +91 -0
  112. package/dist/cjs/schema-definition/pg-sequence.cjs +56 -0
  113. package/dist/cjs/schema-definition/pg-trigger.cjs +108 -0
  114. package/dist/cjs/schema-definition/relations.cjs +98 -0
  115. package/dist/cjs/schema-definition/sql-expressions.cjs +202 -0
  116. package/dist/cjs/schema-definition/table-definition.cjs +636 -0
  117. package/dist/cjs/select/aggregate-builder.cjs +179 -0
  118. package/dist/cjs/select/index.cjs +5 -0
  119. package/dist/cjs/select/select-builder.cjs +233 -0
  120. package/dist/cjs/sequence/index.cjs +7 -0
  121. package/dist/cjs/sequence/sequence-builder.cjs +264 -0
  122. package/dist/cjs/table/alter-table-builder.cjs +146 -0
  123. package/dist/cjs/table/constraint-builder.cjs +102 -0
  124. package/dist/cjs/table/create-table-builder.cjs +248 -0
  125. package/dist/cjs/table/index.cjs +17 -0
  126. package/dist/cjs/table/partition-builder.cjs +131 -0
  127. package/dist/cjs/table/truncate-builder.cjs +70 -0
  128. package/dist/cjs/transaction/index.cjs +6 -0
  129. package/dist/cjs/transaction/transaction-builder.cjs +78 -0
  130. package/dist/cjs/trigger/create-trigger-builder.cjs +174 -0
  131. package/dist/cjs/trigger/index.cjs +6 -0
  132. package/dist/cjs/types/aggregate-types.cjs +2 -0
  133. package/dist/cjs/types/config-types.cjs +40 -0
  134. package/dist/cjs/types/inference-types.cjs +18 -0
  135. package/dist/cjs/types/pagination-types.cjs +7 -0
  136. package/dist/cjs/types/result-types.cjs +2 -0
  137. package/dist/cjs/types/schema-types.cjs +2 -0
  138. package/dist/cjs/types/subscription-types.cjs +2 -0
  139. package/dist/cjs/types.cjs +2 -0
  140. package/dist/cjs/update/array-update-builder.cjs +205 -0
  141. package/dist/cjs/update/index.cjs +13 -0
  142. package/dist/cjs/update/update-builder.cjs +195 -0
  143. package/dist/cjs/utils/case-converter.cjs +58 -0
  144. package/dist/cjs/utils/environment-detection.cjs +120 -0
  145. package/dist/cjs/utils/index.cjs +10 -0
  146. package/dist/cjs/utils/pool-defaults.cjs +106 -0
  147. package/dist/cjs/utils/type-coercion.cjs +118 -0
  148. package/dist/cjs/view/create-view-builder.cjs +180 -0
  149. package/dist/cjs/view/index.cjs +7 -0
  150. package/dist/cjs/window/index.cjs +5 -0
  151. package/dist/cjs/window/window-builder.cjs +80 -0
  152. package/dist/config.cjs +1 -0
  153. package/dist/config.d.ts +655 -0
  154. package/dist/config.js +1 -0
  155. package/dist/esm/cache/index.js +1 -0
  156. package/dist/esm/cache/query-cache.js +303 -0
  157. package/dist/esm/cli/commands/add.js +78 -0
  158. package/dist/esm/cli/commands/commit.js +109 -0
  159. package/dist/esm/cli/commands/diff.js +81 -0
  160. package/dist/esm/cli/commands/export.js +297 -0
  161. package/dist/esm/cli/commands/fetch.js +56 -0
  162. package/dist/esm/cli/commands/generate.js +206 -0
  163. package/dist/esm/cli/commands/history.js +129 -0
  164. package/dist/esm/cli/commands/import.js +488 -0
  165. package/dist/esm/cli/commands/init.js +401 -0
  166. package/dist/esm/cli/commands/introspect.js +106 -0
  167. package/dist/esm/cli/commands/log.js +59 -0
  168. package/dist/esm/cli/commands/migrate.js +131 -0
  169. package/dist/esm/cli/commands/pull.js +374 -0
  170. package/dist/esm/cli/commands/push.js +129 -0
  171. package/dist/esm/cli/commands/rollback.js +133 -0
  172. package/dist/esm/cli/commands/status.js +107 -0
  173. package/dist/esm/cli/commands/sync.js +76 -0
  174. package/dist/esm/cli/index.js +240 -0
  175. package/dist/esm/cli/utils/change-tracker.js +405 -0
  176. package/dist/esm/cli/utils/commit-manager.js +191 -0
  177. package/dist/esm/cli/utils/config-loader.js +86 -0
  178. package/dist/esm/cli/utils/env-loader.js +57 -0
  179. package/dist/esm/cli/utils/fast-introspect.js +362 -0
  180. package/dist/esm/cli/utils/git-utils.js +347 -0
  181. package/dist/esm/cli/utils/migration-generator.js +263 -0
  182. package/dist/esm/cli/utils/relqignore.js +74 -0
  183. package/dist/esm/cli/utils/repo-manager.js +444 -0
  184. package/dist/esm/cli/utils/schema-comparator.js +307 -0
  185. package/dist/esm/cli/utils/schema-diff.js +276 -0
  186. package/dist/esm/cli/utils/schema-hash.js +69 -0
  187. package/dist/esm/cli/utils/schema-introspect.js +418 -0
  188. package/dist/esm/cli/utils/snapshot-manager.js +179 -0
  189. package/dist/esm/cli/utils/spinner.js +101 -0
  190. package/dist/esm/cli/utils/sql-generator.js +504 -0
  191. package/dist/esm/cli/utils/sql-parser.js +992 -0
  192. package/dist/esm/cli/utils/type-generator.js +2058 -0
  193. package/dist/esm/condition/array-condition-builder.js +495 -0
  194. package/dist/esm/condition/array-numeric-condition-builder.js +182 -0
  195. package/dist/esm/condition/array-specialized-condition-builder.js +200 -0
  196. package/dist/esm/condition/array-string-condition-builder.js +142 -0
  197. package/dist/esm/condition/base-condition-builder.js +1 -0
  198. package/dist/esm/condition/condition-collector.js +275 -0
  199. package/dist/esm/condition/fulltext-condition-builder.js +53 -0
  200. package/dist/esm/condition/geometric-condition-builder.js +200 -0
  201. package/dist/esm/condition/index.js +7 -0
  202. package/dist/esm/condition/jsonb-condition-builder.js +152 -0
  203. package/dist/esm/condition/network-condition-builder.js +222 -0
  204. package/dist/esm/condition/range-condition-builder.js +74 -0
  205. package/dist/esm/config/config.js +150 -0
  206. package/dist/esm/config/index.js +1 -0
  207. package/dist/esm/constants/pg-values.js +63 -0
  208. package/dist/esm/copy/copy-builder.js +308 -0
  209. package/dist/esm/copy/index.js +1 -0
  210. package/dist/esm/core/query-builder.js +426 -0
  211. package/dist/esm/core/relq-client.js +1791 -0
  212. package/dist/esm/core/typed-kuery-client.js +1 -0
  213. package/dist/esm/count/count-builder.js +81 -0
  214. package/dist/esm/count/index.js +1 -0
  215. package/dist/esm/cte/cte-builder.js +82 -0
  216. package/dist/esm/cte/index.js +1 -0
  217. package/dist/esm/ddl/function.js +45 -0
  218. package/dist/esm/ddl/index.js +2 -0
  219. package/dist/esm/ddl/sql.js +51 -0
  220. package/dist/esm/delete/delete-builder.js +128 -0
  221. package/dist/esm/delete/index.js +1 -0
  222. package/dist/esm/errors/relq-errors.js +310 -0
  223. package/dist/esm/examples/fulltext-search-test.js +117 -0
  224. package/dist/esm/explain/explain-builder.js +95 -0
  225. package/dist/esm/explain/index.js +1 -0
  226. package/dist/esm/function/create-function-builder.js +188 -0
  227. package/dist/esm/function/index.js +1 -0
  228. package/dist/esm/functions/advanced-functions.js +231 -0
  229. package/dist/esm/functions/case-builder.js +58 -0
  230. package/dist/esm/functions/geometric-functions.js +97 -0
  231. package/dist/esm/functions/index.js +171 -0
  232. package/dist/esm/functions/network-functions.js +79 -0
  233. package/dist/esm/functions/sql-functions.js +421 -0
  234. package/dist/esm/index.js +34 -0
  235. package/dist/esm/indexing/create-index-builder.js +180 -0
  236. package/dist/esm/indexing/drop-index-builder.js +81 -0
  237. package/dist/esm/indexing/index-types.js +1 -0
  238. package/dist/esm/indexing/index.js +2 -0
  239. package/dist/esm/insert/conflict-builder.js +162 -0
  240. package/dist/esm/insert/index.js +1 -0
  241. package/dist/esm/insert/insert-builder.js +247 -0
  242. package/dist/esm/introspect/index.js +224 -0
  243. package/dist/esm/maintenance/index.js +1 -0
  244. package/dist/esm/maintenance/vacuum-builder.js +158 -0
  245. package/dist/esm/pubsub/index.js +1 -0
  246. package/dist/esm/pubsub/listen-notify-builder.js +48 -0
  247. package/dist/esm/pubsub/listener-connection.js +173 -0
  248. package/dist/esm/raw/index.js +1 -0
  249. package/dist/esm/raw/raw-query-builder.js +20 -0
  250. package/dist/esm/schema/index.js +1 -0
  251. package/dist/esm/schema/schema-builder.js +1150 -0
  252. package/dist/esm/schema-builder.js +2 -0
  253. package/dist/esm/schema-definition/column-types.js +738 -0
  254. package/dist/esm/schema-definition/index.js +10 -0
  255. package/dist/esm/schema-definition/introspection.js +614 -0
  256. package/dist/esm/schema-definition/partitions.js +123 -0
  257. package/dist/esm/schema-definition/pg-enum.js +70 -0
  258. package/dist/esm/schema-definition/pg-function.js +85 -0
  259. package/dist/esm/schema-definition/pg-sequence.js +50 -0
  260. package/dist/esm/schema-definition/pg-trigger.js +102 -0
  261. package/dist/esm/schema-definition/relations.js +90 -0
  262. package/dist/esm/schema-definition/sql-expressions.js +193 -0
  263. package/dist/esm/schema-definition/table-definition.js +630 -0
  264. package/dist/esm/select/aggregate-builder.js +172 -0
  265. package/dist/esm/select/index.js +1 -0
  266. package/dist/esm/select/select-builder.js +226 -0
  267. package/dist/esm/sequence/index.js +1 -0
  268. package/dist/esm/sequence/sequence-builder.js +255 -0
  269. package/dist/esm/table/alter-table-builder.js +138 -0
  270. package/dist/esm/table/constraint-builder.js +95 -0
  271. package/dist/esm/table/create-table-builder.js +241 -0
  272. package/dist/esm/table/index.js +5 -0
  273. package/dist/esm/table/partition-builder.js +121 -0
  274. package/dist/esm/table/truncate-builder.js +63 -0
  275. package/dist/esm/transaction/index.js +1 -0
  276. package/dist/esm/transaction/transaction-builder.js +70 -0
  277. package/dist/esm/trigger/create-trigger-builder.js +166 -0
  278. package/dist/esm/trigger/index.js +1 -0
  279. package/dist/esm/types/aggregate-types.js +1 -0
  280. package/dist/esm/types/config-types.js +36 -0
  281. package/dist/esm/types/inference-types.js +12 -0
  282. package/dist/esm/types/pagination-types.js +4 -0
  283. package/dist/esm/types/result-types.js +1 -0
  284. package/dist/esm/types/schema-types.js +1 -0
  285. package/dist/esm/types/subscription-types.js +1 -0
  286. package/dist/esm/types.js +1 -0
  287. package/dist/esm/update/array-update-builder.js +192 -0
  288. package/dist/esm/update/index.js +2 -0
  289. package/dist/esm/update/update-builder.js +188 -0
  290. package/dist/esm/utils/case-converter.js +55 -0
  291. package/dist/esm/utils/environment-detection.js +113 -0
  292. package/dist/esm/utils/index.js +2 -0
  293. package/dist/esm/utils/pool-defaults.js +100 -0
  294. package/dist/esm/utils/type-coercion.js +110 -0
  295. package/dist/esm/view/create-view-builder.js +171 -0
  296. package/dist/esm/view/index.js +1 -0
  297. package/dist/esm/window/index.js +1 -0
  298. package/dist/esm/window/window-builder.js +73 -0
  299. package/dist/index.cjs +1 -0
  300. package/dist/index.d.ts +10341 -0
  301. package/dist/index.js +1 -0
  302. package/dist/schema-builder.cjs +1 -0
  303. package/dist/schema-builder.d.ts +2272 -0
  304. package/dist/schema-builder.js +1 -0
  305. package/package.json +55 -0
@@ -0,0 +1,224 @@
1
+ function mapTypeToCode(sqlType) {
2
+ const type = sqlType.toUpperCase().trim();
3
+ if (type === 'SERIAL')
4
+ return 'serial()';
5
+ if (type === 'SMALLSERIAL')
6
+ return 'smallserial()';
7
+ if (type === 'BIGSERIAL')
8
+ return 'bigserial()';
9
+ if (type === 'SMALLINT' || type === 'INT2')
10
+ return 'smallint()';
11
+ if (type === 'INTEGER' || type === 'INT' || type === 'INT4')
12
+ return 'integer()';
13
+ if (type === 'BIGINT' || type === 'INT8')
14
+ return 'bigint()';
15
+ if (type === 'REAL' || type === 'FLOAT4')
16
+ return 'real()';
17
+ if (type === 'DOUBLE PRECISION' || type === 'FLOAT8')
18
+ return 'doublePrecision()';
19
+ const numericMatch = type.match(/^NUMERIC\((\d+)(?:,\s*(\d+))?\)$/);
20
+ if (numericMatch) {
21
+ return numericMatch[2]
22
+ ? `numeric(${numericMatch[1]}, ${numericMatch[2]})`
23
+ : `numeric(${numericMatch[1]})`;
24
+ }
25
+ if (type === 'NUMERIC' || type === 'DECIMAL')
26
+ return 'numeric()';
27
+ const varcharMatch = type.match(/^VARCHAR\((\d+)\)$/);
28
+ if (varcharMatch)
29
+ return `varchar(${varcharMatch[1]})`;
30
+ if (type === 'VARCHAR')
31
+ return 'varchar()';
32
+ const charMatch = type.match(/^CHAR\((\d+)\)$/);
33
+ if (charMatch)
34
+ return `char(${charMatch[1]})`;
35
+ if (type === 'CHAR')
36
+ return 'char()';
37
+ if (type === 'TEXT')
38
+ return 'text()';
39
+ if (type === 'BOOLEAN' || type === 'BOOL')
40
+ return 'boolean()';
41
+ if (type === 'DATE')
42
+ return 'date()';
43
+ if (type === 'TIME')
44
+ return 'time()';
45
+ if (type === 'TIMETZ' || type === 'TIME WITH TIME ZONE')
46
+ return 'timetz()';
47
+ if (type === 'TIMESTAMP')
48
+ return 'timestamp()';
49
+ if (type === 'TIMESTAMPTZ' || type === 'TIMESTAMP WITH TIME ZONE')
50
+ return 'timestamptz()';
51
+ if (type === 'INTERVAL')
52
+ return 'interval()';
53
+ if (type === 'UUID')
54
+ return 'uuid()';
55
+ if (type === 'JSON')
56
+ return 'json()';
57
+ if (type === 'JSONB')
58
+ return 'jsonb()';
59
+ if (type === 'BYTEA')
60
+ return 'bytea()';
61
+ const arrayMatch = type.match(/^(.+)\[\]$/);
62
+ if (arrayMatch) {
63
+ const baseType = mapTypeToCode(arrayMatch[1]);
64
+ return `${baseType.slice(0, -1)}.array()`;
65
+ }
66
+ const vectorMatch = type.match(/^VECTOR\((\d+)\)$/);
67
+ if (vectorMatch)
68
+ return `vector(${vectorMatch[1]})`;
69
+ if (type === 'VECTOR')
70
+ return 'vector()';
71
+ if (type.startsWith('GEOMETRY'))
72
+ return 'geometry()';
73
+ if (type.startsWith('GEOGRAPHY'))
74
+ return 'geography()';
75
+ if (type === 'INET')
76
+ return 'inet()';
77
+ if (type === 'CIDR')
78
+ return 'cidr()';
79
+ if (type === 'MACADDR')
80
+ return 'macaddr()';
81
+ if (type === 'TSVECTOR')
82
+ return 'tsvector()';
83
+ if (type === 'TSQUERY')
84
+ return 'tsquery()';
85
+ return `sql\`${sqlType.toLowerCase()}\``;
86
+ }
87
+ function parseCreateTable(sql) {
88
+ const tableMatch = sql.match(/CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?["']?(\w+)["']?\s*\(([\s\S]+)\)/i);
89
+ if (!tableMatch)
90
+ return null;
91
+ const tableName = tableMatch[1];
92
+ const columnsStr = tableMatch[2];
93
+ const columns = [];
94
+ const tableConstraints = [];
95
+ const parts = [];
96
+ let depth = 0;
97
+ let current = '';
98
+ for (const char of columnsStr) {
99
+ if (char === '(')
100
+ depth++;
101
+ else if (char === ')')
102
+ depth--;
103
+ else if (char === ',' && depth === 0) {
104
+ parts.push(current.trim());
105
+ current = '';
106
+ continue;
107
+ }
108
+ current += char;
109
+ }
110
+ if (current.trim())
111
+ parts.push(current.trim());
112
+ const primaryKeyColumns = new Set();
113
+ const uniqueColumns = new Set();
114
+ const foreignKeys = new Map();
115
+ for (const part of parts) {
116
+ const trimmed = part.trim();
117
+ const pkMatch = trimmed.match(/^PRIMARY\s+KEY\s*\(([^)]+)\)/i);
118
+ if (pkMatch) {
119
+ const cols = pkMatch[1].split(',').map(c => c.trim().replace(/["']/g, ''));
120
+ cols.forEach(c => primaryKeyColumns.add(c));
121
+ continue;
122
+ }
123
+ const uqMatch = trimmed.match(/^UNIQUE\s*\(([^)]+)\)/i);
124
+ if (uqMatch) {
125
+ const cols = uqMatch[1].split(',').map(c => c.trim().replace(/["']/g, ''));
126
+ cols.forEach(c => uniqueColumns.add(c));
127
+ continue;
128
+ }
129
+ const fkMatch = trimmed.match(/^FOREIGN\s+KEY\s*\((\w+)\)\s*REFERENCES\s+["']?(\w+)["']?\s*\((\w+)\)/i);
130
+ if (fkMatch) {
131
+ foreignKeys.set(fkMatch[1], { table: fkMatch[2], column: fkMatch[3] });
132
+ continue;
133
+ }
134
+ }
135
+ for (const part of parts) {
136
+ const trimmed = part.trim();
137
+ if (/^(PRIMARY\s+KEY|UNIQUE|FOREIGN\s+KEY|CHECK|CONSTRAINT)/i.test(trimmed)) {
138
+ continue;
139
+ }
140
+ const colMatch = trimmed.match(/^["']?(\w+)["']?\s+(\w+(?:\([^)]*\))?(?:\[\])?)/i);
141
+ if (!colMatch)
142
+ continue;
143
+ const name = colMatch[1];
144
+ const dataType = colMatch[2].toUpperCase();
145
+ const rest = trimmed.slice(colMatch[0].length).trim();
146
+ const isPrimaryKey = primaryKeyColumns.has(name) || /PRIMARY\s+KEY/i.test(rest);
147
+ const isUnique = uniqueColumns.has(name) || /UNIQUE/i.test(rest);
148
+ const isNullable = !/NOT\s+NULL/i.test(rest) && !isPrimaryKey;
149
+ let references = foreignKeys.get(name) || null;
150
+ const refMatch = rest.match(/REFERENCES\s+["']?(\w+)["']?\s*\((\w+)\)/i);
151
+ if (refMatch) {
152
+ references = { table: refMatch[1], column: refMatch[2] };
153
+ }
154
+ let defaultValue = null;
155
+ const defaultMatch = rest.match(/DEFAULT\s+(.+?)(?:\s+(?:NOT\s+NULL|UNIQUE|PRIMARY|REFERENCES|CHECK)|$)/i);
156
+ if (defaultMatch) {
157
+ let val = defaultMatch[1].trim();
158
+ if (val.endsWith(','))
159
+ val = val.slice(0, -1);
160
+ defaultValue = val;
161
+ }
162
+ if (isPrimaryKey && primaryKeyColumns.size === 0) {
163
+ primaryKeyColumns.add(name);
164
+ }
165
+ columns.push({
166
+ name,
167
+ dataType,
168
+ typeCode: mapTypeToCode(dataType),
169
+ isNullable,
170
+ isPrimaryKey,
171
+ isUnique,
172
+ defaultValue,
173
+ references,
174
+ });
175
+ }
176
+ return {
177
+ name: tableName,
178
+ columns,
179
+ indexes: [],
180
+ };
181
+ }
182
+ export function parseSqlToDefineTable(sql) {
183
+ const tables = [];
184
+ const createTableRegex = /CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?["']?(\w+)["']?\s*\([^;]+\);?/gi;
185
+ let match;
186
+ while ((match = createTableRegex.exec(sql)) !== null) {
187
+ const parsed = parseCreateTable(match[0]);
188
+ if (parsed) {
189
+ tables.push(parsed);
190
+ }
191
+ }
192
+ return tables;
193
+ }
194
+ export function generateCode(tables) {
195
+ const parts = [
196
+ "import { defineTable, serial, integer, bigint, varchar, text, boolean, uuid, jsonb, json, timestamp, timestamptz, date, numeric, vector } from 'relq/schema-builder';",
197
+ '',
198
+ ];
199
+ for (const table of tables) {
200
+ const columns = table.columns.map(col => {
201
+ const mods = [col.typeCode];
202
+ if (col.isPrimaryKey)
203
+ mods.push('.primaryKey()');
204
+ if (!col.isNullable && !col.isPrimaryKey)
205
+ mods.push('.notNull()');
206
+ if (col.isUnique && !col.isPrimaryKey)
207
+ mods.push('.unique()');
208
+ if (col.references)
209
+ mods.push(`.references('${col.references.table}', '${col.references.column}')`);
210
+ if (col.defaultValue)
211
+ mods.push(`.default(${col.defaultValue})`);
212
+ return ` ${col.name}: ${mods.join('')},`;
213
+ });
214
+ parts.push(`export const ${table.name} = defineTable('${table.name}', {`);
215
+ parts.push(...columns);
216
+ parts.push('});');
217
+ parts.push('');
218
+ }
219
+ return parts.join('\n');
220
+ }
221
+ export async function introspect(sql) {
222
+ const tables = parseSqlToDefineTable(sql);
223
+ return generateCode(tables);
224
+ }
@@ -0,0 +1 @@
1
+ export { VacuumBuilder, AnalyzeBuilder } from "./vacuum-builder.js";
@@ -0,0 +1,158 @@
1
+ import format from "../../addons/pg-format.js";
2
+ export class VacuumBuilder {
3
+ tables = [];
4
+ columns = new Map();
5
+ options = {};
6
+ constructor(tables) {
7
+ if (tables) {
8
+ this.tables = Array.isArray(tables) ? tables : [tables];
9
+ }
10
+ }
11
+ full(enable = true) {
12
+ this.options.full = enable;
13
+ return this;
14
+ }
15
+ freeze(enable = true) {
16
+ this.options.freeze = enable;
17
+ return this;
18
+ }
19
+ verbose(enable = true) {
20
+ this.options.verbose = enable;
21
+ return this;
22
+ }
23
+ analyze(enable = true) {
24
+ this.options.analyze = enable;
25
+ return this;
26
+ }
27
+ disablePageSkipping(enable = true) {
28
+ this.options.disablePageSkipping = enable;
29
+ return this;
30
+ }
31
+ skipLocked(enable = true) {
32
+ this.options.skipLocked = enable;
33
+ return this;
34
+ }
35
+ indexCleanup(value = true) {
36
+ this.options.indexCleanup = value;
37
+ return this;
38
+ }
39
+ truncate(enable = true) {
40
+ this.options.truncate = enable;
41
+ return this;
42
+ }
43
+ parallel(workers) {
44
+ this.options.parallel = workers;
45
+ return this;
46
+ }
47
+ columnsFor(table, columns) {
48
+ this.columns.set(table, columns);
49
+ return this;
50
+ }
51
+ toString() {
52
+ const parts = [];
53
+ if (this.options.full) {
54
+ parts.push('FULL');
55
+ }
56
+ if (this.options.freeze) {
57
+ parts.push('FREEZE');
58
+ }
59
+ if (this.options.verbose) {
60
+ parts.push('VERBOSE');
61
+ }
62
+ if (this.options.analyze) {
63
+ parts.push('ANALYZE');
64
+ }
65
+ const optionsList = [];
66
+ if (this.options.disablePageSkipping) {
67
+ optionsList.push('DISABLE_PAGE_SKIPPING');
68
+ }
69
+ if (this.options.skipLocked) {
70
+ optionsList.push('SKIP_LOCKED');
71
+ }
72
+ if (this.options.indexCleanup !== undefined) {
73
+ const value = typeof this.options.indexCleanup === 'boolean'
74
+ ? (this.options.indexCleanup ? 'ON' : 'OFF')
75
+ : this.options.indexCleanup;
76
+ optionsList.push(`INDEX_CLEANUP ${value}`);
77
+ }
78
+ if (this.options.truncate !== undefined) {
79
+ optionsList.push(`TRUNCATE ${this.options.truncate ? 'ON' : 'OFF'}`);
80
+ }
81
+ if (this.options.parallel !== undefined) {
82
+ optionsList.push(`PARALLEL ${this.options.parallel}`);
83
+ }
84
+ let sql = 'VACUUM';
85
+ if (parts.length > 0) {
86
+ sql += ' ' + parts.join(' ');
87
+ }
88
+ if (optionsList.length > 0) {
89
+ sql += ` (${optionsList.join(', ')})`;
90
+ }
91
+ if (this.tables.length > 0) {
92
+ const tableStrs = this.tables.map(table => {
93
+ let tableStr = format.ident(table);
94
+ if (this.columns.has(table)) {
95
+ const cols = this.columns.get(table);
96
+ tableStr += ` (${cols.map(c => format.ident(c)).join(', ')})`;
97
+ }
98
+ return tableStr;
99
+ });
100
+ sql += ' ' + tableStrs.join(', ');
101
+ }
102
+ return sql;
103
+ }
104
+ }
105
+ export class AnalyzeBuilder {
106
+ tables = [];
107
+ columnsMap = new Map();
108
+ verboseFlag = false;
109
+ skipLockedFlag = false;
110
+ constructor(tables) {
111
+ if (tables) {
112
+ this.tables = Array.isArray(tables) ? tables : [tables];
113
+ }
114
+ }
115
+ verbose(enable = true) {
116
+ this.verboseFlag = enable;
117
+ return this;
118
+ }
119
+ skipLocked(enable = true) {
120
+ this.skipLockedFlag = enable;
121
+ return this;
122
+ }
123
+ columns(columns) {
124
+ if (this.tables.length > 0) {
125
+ this.columnsMap.set(this.tables[0], columns);
126
+ }
127
+ return this;
128
+ }
129
+ columnsFor(table, columns) {
130
+ this.columnsMap.set(table, columns);
131
+ return this;
132
+ }
133
+ toString() {
134
+ let sql = 'ANALYZE';
135
+ if (this.verboseFlag) {
136
+ sql += ' VERBOSE';
137
+ }
138
+ const options = [];
139
+ if (this.skipLockedFlag) {
140
+ options.push('SKIP_LOCKED');
141
+ }
142
+ if (options.length > 0) {
143
+ sql += ` (${options.join(', ')})`;
144
+ }
145
+ if (this.tables.length > 0) {
146
+ const tableStrs = this.tables.map(table => {
147
+ let tableStr = format.ident(table);
148
+ if (this.columnsMap.has(table)) {
149
+ const cols = this.columnsMap.get(table);
150
+ tableStr += ` (${cols.map(c => format.ident(c)).join(', ')})`;
151
+ }
152
+ return tableStr;
153
+ });
154
+ sql += ' ' + tableStrs.join(', ');
155
+ }
156
+ return sql;
157
+ }
158
+ }
@@ -0,0 +1 @@
1
+ export { ListenBuilder, UnlistenBuilder, NotifyBuilder } from "./listen-notify-builder.js";
@@ -0,0 +1,48 @@
1
+ import format from "../../addons/pg-format.js";
2
+ export class ListenBuilder {
3
+ channelName;
4
+ constructor(channelName) {
5
+ this.channelName = channelName;
6
+ }
7
+ toString() {
8
+ return `LISTEN ${format.ident(this.channelName)}`;
9
+ }
10
+ }
11
+ export class UnlistenBuilder {
12
+ channelName;
13
+ constructor(channelName = '*') {
14
+ this.channelName = channelName;
15
+ }
16
+ all() {
17
+ this.channelName = '*';
18
+ return this;
19
+ }
20
+ toString() {
21
+ if (this.channelName === '*') {
22
+ return 'UNLISTEN *';
23
+ }
24
+ return `UNLISTEN ${format.ident(this.channelName)}`;
25
+ }
26
+ }
27
+ export class NotifyBuilder {
28
+ channelName;
29
+ payload;
30
+ constructor(channelName, payload) {
31
+ this.channelName = channelName;
32
+ this.payload = payload;
33
+ }
34
+ withPayload(payload) {
35
+ this.payload = payload;
36
+ return this;
37
+ }
38
+ toString() {
39
+ let sql = `NOTIFY ${format.ident(this.channelName)}`;
40
+ if (this.payload !== undefined) {
41
+ const payloadStr = typeof this.payload === 'string'
42
+ ? this.payload
43
+ : JSON.stringify(this.payload);
44
+ sql += `, ${format.literal(payloadStr)}`;
45
+ }
46
+ return sql;
47
+ }
48
+ }
@@ -0,0 +1,173 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Client } from "../../addons/pg.js";
3
+ import format from "../../addons/pg-format.js";
4
+ export class ListenerConnection extends EventEmitter {
5
+ client = null;
6
+ config;
7
+ connected = false;
8
+ connecting = false;
9
+ subscriptions = new Map();
10
+ reconnectTimeout = null;
11
+ isClosing = false;
12
+ reconnectDelay = 1000;
13
+ maxReconnectDelay = 30000;
14
+ constructor(config) {
15
+ super();
16
+ this.config = config;
17
+ }
18
+ async subscribe(channel) {
19
+ if (!this.connected && !this.connecting) {
20
+ await this.connect();
21
+ }
22
+ else if (this.connecting) {
23
+ await this.waitForConnection();
24
+ }
25
+ let channelSub = this.subscriptions.get(channel);
26
+ if (!channelSub) {
27
+ channelSub = { subscribers: [], retryCount: 0 };
28
+ this.subscriptions.set(channel, channelSub);
29
+ await this.executeListen(channel);
30
+ }
31
+ const emitter = new EventEmitter();
32
+ channelSub.subscribers.push(emitter);
33
+ const subscription = {
34
+ on(event, listener) {
35
+ emitter.on(event, listener);
36
+ return this;
37
+ },
38
+ close: async () => {
39
+ emitter.removeAllListeners();
40
+ const sub = this.subscriptions.get(channel);
41
+ if (sub) {
42
+ const index = sub.subscribers.indexOf(emitter);
43
+ if (index !== -1) {
44
+ sub.subscribers.splice(index, 1);
45
+ }
46
+ if (sub.subscribers.length === 0) {
47
+ this.subscriptions.delete(channel);
48
+ if (this.connected) {
49
+ try {
50
+ await this.client?.query(`UNLISTEN ${format.ident(channel)}`);
51
+ }
52
+ catch (err) {
53
+ }
54
+ }
55
+ if (this.subscriptions.size === 0) {
56
+ await this.close();
57
+ }
58
+ }
59
+ }
60
+ }
61
+ };
62
+ return subscription;
63
+ }
64
+ async connect() {
65
+ if (this.connected || this.connecting)
66
+ return;
67
+ this.connecting = true;
68
+ this.isClosing = false;
69
+ try {
70
+ this.client = new Client(this.config);
71
+ this.client.on('notification', (msg) => {
72
+ if (msg.channel && msg.payload) {
73
+ this.dispatch(msg.channel, msg.payload);
74
+ }
75
+ });
76
+ this.client.on('error', (err) => {
77
+ this.emit('error', err);
78
+ if (!this.isClosing) {
79
+ this.handleDisconnect();
80
+ }
81
+ });
82
+ this.client.on('end', () => {
83
+ if (!this.isClosing) {
84
+ this.handleDisconnect();
85
+ }
86
+ });
87
+ await this.client.connect();
88
+ this.connected = true;
89
+ this.connecting = false;
90
+ this.reconnectDelay = 1000;
91
+ await this.resubscribeAll();
92
+ this.emit('connect');
93
+ }
94
+ catch (err) {
95
+ this.connecting = false;
96
+ this.handleDisconnect();
97
+ throw err;
98
+ }
99
+ }
100
+ async waitForConnection() {
101
+ return new Promise((resolve, reject) => {
102
+ const onConnect = () => {
103
+ cleanup();
104
+ resolve();
105
+ };
106
+ const onError = (err) => {
107
+ cleanup();
108
+ reject(err);
109
+ };
110
+ const cleanup = () => {
111
+ this.removeListener('connect', onConnect);
112
+ this.removeListener('error', onError);
113
+ };
114
+ this.on('connect', onConnect);
115
+ this.on('error', onError);
116
+ });
117
+ }
118
+ dispatch(channel, payload) {
119
+ const sub = this.subscriptions.get(channel);
120
+ if (sub) {
121
+ let data = payload;
122
+ try {
123
+ data = JSON.parse(payload);
124
+ }
125
+ catch {
126
+ }
127
+ sub.subscribers.forEach(emitter => emitter.emit('notification', data));
128
+ }
129
+ }
130
+ async executeListen(channel) {
131
+ if (this.client && this.connected) {
132
+ await this.client.query(`LISTEN ${format.ident(channel)}`);
133
+ }
134
+ }
135
+ async resubscribeAll() {
136
+ for (const channel of this.subscriptions.keys()) {
137
+ try {
138
+ await this.executeListen(channel);
139
+ }
140
+ catch (err) {
141
+ console.error(`Failed to resubscribe to ${channel}:`, err);
142
+ }
143
+ }
144
+ }
145
+ handleDisconnect() {
146
+ this.connected = false;
147
+ this.client = null;
148
+ if (this.isClosing)
149
+ return;
150
+ if (this.reconnectTimeout)
151
+ clearTimeout(this.reconnectTimeout);
152
+ this.reconnectTimeout = setTimeout(() => {
153
+ console.log('Reconnecting listener...');
154
+ this.connect().catch(err => {
155
+ console.error('Reconnect failed:', err);
156
+ this.reconnectDelay = Math.min(this.reconnectDelay * 2, this.maxReconnectDelay);
157
+ this.handleDisconnect();
158
+ });
159
+ }, this.reconnectDelay);
160
+ this.reconnectTimeout.unref();
161
+ }
162
+ async close() {
163
+ this.isClosing = true;
164
+ if (this.reconnectTimeout)
165
+ clearTimeout(this.reconnectTimeout);
166
+ if (this.client) {
167
+ await this.client.end();
168
+ this.client = null;
169
+ }
170
+ this.connected = false;
171
+ this.subscriptions.clear();
172
+ }
173
+ }
@@ -0,0 +1 @@
1
+ export { RawQueryBuilder } from "./raw-query-builder.js";
@@ -0,0 +1,20 @@
1
+ import format from "../../addons/pg-format.js";
2
+ import { RelqQueryError } from "../errors/relq-errors.js";
3
+ export class RawQueryBuilder {
4
+ query;
5
+ params;
6
+ constructor(query, params = []) {
7
+ this.query = query;
8
+ this.params = params;
9
+ }
10
+ toString() {
11
+ if (!this.query || typeof this.query !== 'string' || this.query.trim() === '') {
12
+ throw new RelqQueryError(`Invalid query provided: ${this.query === null ? 'null' : this.query === undefined ? 'undefined' : 'empty string'}`, { hint: 'Provide a valid SQL query string' });
13
+ }
14
+ const result = this.params.length > 0 ? format(this.query, ...this.params) : this.query;
15
+ if (!result || typeof result !== 'string') {
16
+ throw new RelqQueryError(`format() returned invalid result: ${result === null ? 'null' : result === undefined ? 'undefined' : typeof result}`, { sql: this.query });
17
+ }
18
+ return result;
19
+ }
20
+ }
@@ -0,0 +1 @@
1
+ export { CreateSchemaBuilder, DropSchemaBuilder, GrantBuilder, RevokeBuilder, CreateRoleBuilder, AlterRoleBuilder, DropRoleBuilder, SetRoleBuilder, ReassignOwnedBuilder, DropOwnedBuilder, DefaultPrivilegesBuilder } from "./schema-builder.js";