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,179 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AggregateQueryBuilder = void 0;
7
+ const pg_format_1 = __importDefault(require("../../addons/pg-format.js"));
8
+ const condition_collector_1 = require("../condition/condition-collector.cjs");
9
+ const NUMERIC_FUNCS = new Set(['COUNT', 'SUM', 'AVG', 'MIN', 'MAX']);
10
+ class AggregateQueryBuilder {
11
+ tableName;
12
+ groupByColumns = [];
13
+ entries = [];
14
+ whereConditions = [];
15
+ havingConditions = [];
16
+ orderByColumns = [];
17
+ limitValue;
18
+ offsetValue;
19
+ constructor(tableName) {
20
+ this.tableName = tableName;
21
+ }
22
+ groupBy(...columns) {
23
+ this.groupByColumns.push(...columns);
24
+ return this;
25
+ }
26
+ count(alias = 'count') {
27
+ this.entries.push({ func: 'COUNT', column: '*', alias });
28
+ return this;
29
+ }
30
+ countColumn(column, alias) {
31
+ this.entries.push({ func: 'COUNT', column, alias: alias || column });
32
+ return this;
33
+ }
34
+ countDistinct(column, alias) {
35
+ this.entries.push({ func: 'COUNT', column, alias: alias || column, distinct: true });
36
+ return this;
37
+ }
38
+ sum(column, alias, filter) {
39
+ const entry = { func: 'SUM', column, alias: alias || column };
40
+ if (filter) {
41
+ const collector = new condition_collector_1.ConditionCollector();
42
+ filter(collector);
43
+ entry.filterConditions = collector.getConditions();
44
+ }
45
+ this.entries.push(entry);
46
+ return this;
47
+ }
48
+ avg(column, alias, filter) {
49
+ const entry = { func: 'AVG', column, alias: alias || column };
50
+ if (filter) {
51
+ const collector = new condition_collector_1.ConditionCollector();
52
+ filter(collector);
53
+ entry.filterConditions = collector.getConditions();
54
+ }
55
+ this.entries.push(entry);
56
+ return this;
57
+ }
58
+ min(column, alias) {
59
+ this.entries.push({ func: 'MIN', column, alias: alias || column });
60
+ return this;
61
+ }
62
+ max(column, alias) {
63
+ this.entries.push({ func: 'MAX', column, alias: alias || column });
64
+ return this;
65
+ }
66
+ arrayAgg(column, alias) {
67
+ this.entries.push({ func: 'ARRAY_AGG', column, alias: alias || column });
68
+ return this;
69
+ }
70
+ stringAgg(column, delimiter = ', ', alias) {
71
+ this.entries.push({ func: 'STRING_AGG', column, alias: alias || column, delimiter });
72
+ return this;
73
+ }
74
+ jsonAgg(column, alias) {
75
+ this.entries.push({ func: 'JSON_AGG', column, alias: alias || column });
76
+ return this;
77
+ }
78
+ jsonbAgg(column, alias) {
79
+ this.entries.push({ func: 'JSONB_AGG', column, alias: alias || column });
80
+ return this;
81
+ }
82
+ boolAnd(column, alias) {
83
+ this.entries.push({ func: 'BOOL_AND', column, alias: alias || column });
84
+ return this;
85
+ }
86
+ boolOr(column, alias) {
87
+ this.entries.push({ func: 'BOOL_OR', column, alias: alias || column });
88
+ return this;
89
+ }
90
+ where(callback) {
91
+ const collector = new condition_collector_1.ConditionCollector();
92
+ callback(collector);
93
+ this.whereConditions.push(...collector.getConditions());
94
+ return this;
95
+ }
96
+ having(callback) {
97
+ const collector = new condition_collector_1.ConditionCollector();
98
+ callback(collector);
99
+ this.havingConditions.push(...collector.getConditions());
100
+ return this;
101
+ }
102
+ orderBy(column, direction = 'ASC') {
103
+ this.orderByColumns.push({ column, direction });
104
+ return this;
105
+ }
106
+ limit(count) {
107
+ this.limitValue = count;
108
+ return this;
109
+ }
110
+ offset(count) {
111
+ this.offsetValue = count;
112
+ return this;
113
+ }
114
+ getNumericAliases() {
115
+ return this.entries
116
+ .filter(e => NUMERIC_FUNCS.has(e.func))
117
+ .map(e => e.alias);
118
+ }
119
+ getGroupByColumns() {
120
+ return this.groupByColumns;
121
+ }
122
+ toString() {
123
+ const selectParts = [];
124
+ for (const col of this.groupByColumns) {
125
+ selectParts.push(pg_format_1.default.ident(col));
126
+ }
127
+ for (const entry of this.entries) {
128
+ selectParts.push(this.buildEntrySQL(entry));
129
+ }
130
+ if (selectParts.length === 0) {
131
+ selectParts.push('COUNT(*) AS count');
132
+ }
133
+ let query = (0, pg_format_1.default)('SELECT %s FROM %I', selectParts.join(', '), this.tableName);
134
+ if (this.whereConditions.length > 0) {
135
+ query += ' WHERE ' + (0, condition_collector_1.buildConditionsSQL)(this.whereConditions);
136
+ }
137
+ if (this.groupByColumns.length > 0) {
138
+ query += ' GROUP BY ' + this.groupByColumns.map(col => pg_format_1.default.ident(col)).join(', ');
139
+ }
140
+ if (this.havingConditions.length > 0) {
141
+ query += ' HAVING ' + (0, condition_collector_1.buildConditionsSQL)(this.havingConditions);
142
+ }
143
+ if (this.orderByColumns.length > 0) {
144
+ query += ' ORDER BY ' + this.orderByColumns
145
+ .map(o => `${pg_format_1.default.ident(o.column)} ${o.direction}`)
146
+ .join(', ');
147
+ }
148
+ if (this.limitValue !== undefined) {
149
+ query += (0, pg_format_1.default)(' LIMIT %s', this.limitValue);
150
+ }
151
+ if (this.offsetValue !== undefined) {
152
+ query += (0, pg_format_1.default)(' OFFSET %s', this.offsetValue);
153
+ }
154
+ return query;
155
+ }
156
+ buildEntrySQL(entry) {
157
+ let columnExpr;
158
+ if (entry.column === '*') {
159
+ columnExpr = '*';
160
+ }
161
+ else if (entry.distinct) {
162
+ columnExpr = `DISTINCT ${pg_format_1.default.ident(entry.column)}`;
163
+ }
164
+ else if (entry.func === 'STRING_AGG' && entry.delimiter) {
165
+ columnExpr = `${pg_format_1.default.ident(entry.column)}, ${pg_format_1.default.literal(entry.delimiter)}`;
166
+ }
167
+ else {
168
+ columnExpr = pg_format_1.default.ident(entry.column);
169
+ }
170
+ let sql = `${entry.func}(${columnExpr})`;
171
+ if (entry.filterConditions && entry.filterConditions.length > 0) {
172
+ const filterSQL = (0, condition_collector_1.buildConditionsSQL)(entry.filterConditions);
173
+ sql += ` FILTER (WHERE ${filterSQL})`;
174
+ }
175
+ sql += ` AS ${pg_format_1.default.ident(entry.alias)}`;
176
+ return sql;
177
+ }
178
+ }
179
+ exports.AggregateQueryBuilder = AggregateQueryBuilder;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SelectBuilder = void 0;
4
+ var select_builder_1 = require("./select-builder.cjs");
5
+ Object.defineProperty(exports, "SelectBuilder", { enumerable: true, get: function () { return select_builder_1.SelectBuilder; } });
@@ -0,0 +1,233 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SelectBuilder = void 0;
7
+ const pg_format_1 = __importDefault(require("../../addons/pg-format.js"));
8
+ const condition_collector_1 = require("../condition/condition-collector.cjs");
9
+ class SelectBuilder {
10
+ tableName;
11
+ selectColumns = ['*'];
12
+ whereConditions = [];
13
+ havingConditions = [];
14
+ joinClauses = [];
15
+ groupByColumns = [];
16
+ orderByColumns = [];
17
+ limitValue;
18
+ offsetValue;
19
+ distinctOnColumns = [];
20
+ lockingClause;
21
+ unionQueries = [];
22
+ constructor(tableName, columns) {
23
+ this.tableName = tableName;
24
+ if (columns) {
25
+ this.selectColumns = Array.isArray(columns) ? columns : [columns];
26
+ }
27
+ }
28
+ distinct() {
29
+ this.selectColumns = this.selectColumns.map(col => {
30
+ if (typeof col === 'string') {
31
+ return col.startsWith('DISTINCT') ? col : `DISTINCT ${col}`;
32
+ }
33
+ return col;
34
+ });
35
+ return this;
36
+ }
37
+ where(callback) {
38
+ const conditionBuilder = new condition_collector_1.ConditionCollector();
39
+ callback(conditionBuilder);
40
+ this.whereConditions.push(...conditionBuilder.getConditions());
41
+ return this;
42
+ }
43
+ join(table, condition) {
44
+ this.joinClauses.push(`JOIN ${pg_format_1.default.ident(table)} ON ${condition}`);
45
+ return this;
46
+ }
47
+ leftJoin(table, condition) {
48
+ this.joinClauses.push(`LEFT JOIN ${pg_format_1.default.ident(table)} ON ${condition}`);
49
+ return this;
50
+ }
51
+ rightJoin(table, condition) {
52
+ this.joinClauses.push(`RIGHT JOIN ${pg_format_1.default.ident(table)} ON ${condition}`);
53
+ return this;
54
+ }
55
+ innerJoin(table, condition) {
56
+ this.joinClauses.push(`INNER JOIN ${pg_format_1.default.ident(table)} ON ${condition}`);
57
+ return this;
58
+ }
59
+ groupBy(...columns) {
60
+ this.groupByColumns.push(...columns);
61
+ return this;
62
+ }
63
+ having(callback) {
64
+ const conditionBuilder = new condition_collector_1.ConditionCollector();
65
+ callback(conditionBuilder);
66
+ this.havingConditions.push(...conditionBuilder.getConditions());
67
+ return this;
68
+ }
69
+ orderBy(column, direction = 'ASC') {
70
+ this.orderByColumns.push({ column, direction });
71
+ return this;
72
+ }
73
+ orderAsc(column) {
74
+ return this.orderBy(column, 'ASC');
75
+ }
76
+ orderDesc(column) {
77
+ return this.orderBy(column, 'DESC');
78
+ }
79
+ limit(count) {
80
+ this.limitValue = count;
81
+ return this;
82
+ }
83
+ offset(count) {
84
+ this.offsetValue = count;
85
+ return this;
86
+ }
87
+ distinctOn(...columns) {
88
+ this.distinctOnColumns.push(...columns);
89
+ return this;
90
+ }
91
+ forUpdate() {
92
+ this.lockingClause = 'FOR UPDATE';
93
+ return this;
94
+ }
95
+ forUpdateNoWait() {
96
+ this.lockingClause = 'FOR UPDATE NOWAIT';
97
+ return this;
98
+ }
99
+ forUpdateSkipLocked() {
100
+ this.lockingClause = 'FOR UPDATE SKIP LOCKED';
101
+ return this;
102
+ }
103
+ forShare() {
104
+ this.lockingClause = 'FOR SHARE';
105
+ return this;
106
+ }
107
+ forShareNoWait() {
108
+ this.lockingClause = 'FOR SHARE NOWAIT';
109
+ return this;
110
+ }
111
+ forShareSkipLocked() {
112
+ this.lockingClause = 'FOR SHARE SKIP LOCKED';
113
+ return this;
114
+ }
115
+ union(query) {
116
+ this.unionQueries.push({
117
+ query: typeof query === 'string' ? query : query.toString(),
118
+ type: 'UNION'
119
+ });
120
+ return this;
121
+ }
122
+ unionAll(query) {
123
+ this.unionQueries.push({
124
+ query: typeof query === 'string' ? query : query.toString(),
125
+ type: 'UNION ALL'
126
+ });
127
+ return this;
128
+ }
129
+ intersect(query) {
130
+ this.unionQueries.push({
131
+ query: typeof query === 'string' ? query : query.toString(),
132
+ type: 'INTERSECT'
133
+ });
134
+ return this;
135
+ }
136
+ except(query) {
137
+ this.unionQueries.push({
138
+ query: typeof query === 'string' ? query : query.toString(),
139
+ type: 'EXCEPT'
140
+ });
141
+ return this;
142
+ }
143
+ fullOuterJoin(table, condition) {
144
+ this.joinClauses.push(`FULL OUTER JOIN ${pg_format_1.default.ident(table)} ON ${condition}`);
145
+ return this;
146
+ }
147
+ crossJoin(table) {
148
+ this.joinClauses.push(`CROSS JOIN ${pg_format_1.default.ident(table)}`);
149
+ return this;
150
+ }
151
+ lateralJoin(subquery, left = false) {
152
+ const joinType = left ? 'LEFT JOIN LATERAL' : 'JOIN LATERAL';
153
+ this.joinClauses.push(`${joinType} ${subquery}`);
154
+ return this;
155
+ }
156
+ toString() {
157
+ const columns = this.selectColumns.map(col => {
158
+ if (col === '*')
159
+ return '*';
160
+ if (Array.isArray(col)) {
161
+ const [column, alias] = col;
162
+ return (0, pg_format_1.default)('%I AS %I', column, alias);
163
+ }
164
+ if (col.includes('(') || col.includes('DISTINCT') || col.includes(' AS ')) {
165
+ return col;
166
+ }
167
+ return pg_format_1.default.ident(col);
168
+ }).join(', ');
169
+ let query = 'SELECT';
170
+ if (this.distinctOnColumns.length > 0) {
171
+ const distinctCols = this.distinctOnColumns.map(col => pg_format_1.default.ident(col)).join(', ');
172
+ query += (0, pg_format_1.default)(' DISTINCT ON (%s)', distinctCols);
173
+ }
174
+ query += (0, pg_format_1.default)(' %s FROM %I', columns, this.tableName);
175
+ if (this.joinClauses.length > 0) {
176
+ query += ' ' + this.joinClauses.join(' ');
177
+ }
178
+ if (this.whereConditions.length > 0) {
179
+ query += ' WHERE ' + (0, condition_collector_1.buildConditionsSQL)(this.whereConditions);
180
+ }
181
+ if (this.groupByColumns.length > 0) {
182
+ query += ' GROUP BY ' + this.groupByColumns.map(col => pg_format_1.default.ident(col)).join(', ');
183
+ }
184
+ if (this.havingConditions.length > 0) {
185
+ query += ' HAVING ' + (0, condition_collector_1.buildConditionsSQL)(this.havingConditions);
186
+ }
187
+ if (this.orderByColumns.length > 0) {
188
+ query += ' ORDER BY ' + this.orderByColumns
189
+ .map(order => (0, pg_format_1.default)('%I %s', order.column, order.direction))
190
+ .join(', ');
191
+ }
192
+ if (this.limitValue !== undefined) {
193
+ query += (0, pg_format_1.default)(' LIMIT %s', this.limitValue);
194
+ }
195
+ if (this.offsetValue !== undefined) {
196
+ query += (0, pg_format_1.default)(' OFFSET %s', this.offsetValue);
197
+ }
198
+ if (this.lockingClause) {
199
+ query += ' ' + this.lockingClause;
200
+ }
201
+ if (this.unionQueries.length > 0) {
202
+ for (const unionQuery of this.unionQueries) {
203
+ query += ` ${unionQuery.type} ${unionQuery.query}`;
204
+ }
205
+ }
206
+ return query;
207
+ }
208
+ whereRaw(condition) {
209
+ this.whereConditions.push({
210
+ method: 'raw',
211
+ column: condition,
212
+ values: undefined
213
+ });
214
+ return this;
215
+ }
216
+ toCountSQL() {
217
+ let query = `SELECT COUNT(*) as count FROM ${pg_format_1.default.ident(this.tableName)}`;
218
+ if (this.joinClauses.length > 0) {
219
+ query += ' ' + this.joinClauses.join(' ');
220
+ }
221
+ if (this.whereConditions.length > 0) {
222
+ query += ' WHERE ' + (0, condition_collector_1.buildConditionsSQL)(this.whereConditions);
223
+ }
224
+ if (this.groupByColumns.length > 0) {
225
+ query += ' GROUP BY ' + this.groupByColumns.map(col => pg_format_1.default.ident(col)).join(', ');
226
+ }
227
+ if (this.havingConditions.length > 0) {
228
+ query += ' HAVING ' + (0, condition_collector_1.buildConditionsSQL)(this.havingConditions);
229
+ }
230
+ return query;
231
+ }
232
+ }
233
+ exports.SelectBuilder = SelectBuilder;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DropSequenceBuilder = exports.AlterSequenceBuilder = exports.CreateSequenceBuilder = void 0;
4
+ var sequence_builder_1 = require("./sequence-builder.cjs");
5
+ Object.defineProperty(exports, "CreateSequenceBuilder", { enumerable: true, get: function () { return sequence_builder_1.CreateSequenceBuilder; } });
6
+ Object.defineProperty(exports, "AlterSequenceBuilder", { enumerable: true, get: function () { return sequence_builder_1.AlterSequenceBuilder; } });
7
+ Object.defineProperty(exports, "DropSequenceBuilder", { enumerable: true, get: function () { return sequence_builder_1.DropSequenceBuilder; } });
@@ -0,0 +1,264 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DropSequenceBuilder = exports.AlterSequenceBuilder = exports.CreateSequenceBuilder = void 0;
7
+ const pg_format_1 = __importDefault(require("../../addons/pg-format.js"));
8
+ class CreateSequenceBuilder {
9
+ sequenceName;
10
+ options = {};
11
+ ifNotExistsFlag = false;
12
+ temporaryFlag = false;
13
+ unloggedFlag = false;
14
+ constructor(sequenceName) {
15
+ this.sequenceName = sequenceName;
16
+ }
17
+ ifNotExists() {
18
+ this.ifNotExistsFlag = true;
19
+ return this;
20
+ }
21
+ temporary() {
22
+ this.temporaryFlag = true;
23
+ return this;
24
+ }
25
+ unlogged() {
26
+ this.unloggedFlag = true;
27
+ return this;
28
+ }
29
+ as(type) {
30
+ this.options.as = type;
31
+ return this;
32
+ }
33
+ start(value) {
34
+ this.options.start = value;
35
+ return this;
36
+ }
37
+ increment(value) {
38
+ this.options.increment = value;
39
+ return this;
40
+ }
41
+ minValue(value) {
42
+ this.options.minValue = value;
43
+ return this;
44
+ }
45
+ maxValue(value) {
46
+ this.options.maxValue = value;
47
+ return this;
48
+ }
49
+ noMinValue() {
50
+ this.options.minValue = undefined;
51
+ return this;
52
+ }
53
+ noMaxValue() {
54
+ this.options.maxValue = undefined;
55
+ return this;
56
+ }
57
+ cache(count) {
58
+ this.options.cache = count;
59
+ return this;
60
+ }
61
+ cycle() {
62
+ this.options.cycle = true;
63
+ return this;
64
+ }
65
+ noCycle() {
66
+ this.options.cycle = false;
67
+ return this;
68
+ }
69
+ ownedBy(tableColumn) {
70
+ this.options.ownedBy = tableColumn;
71
+ return this;
72
+ }
73
+ ownedByNone() {
74
+ this.options.ownedBy = 'NONE';
75
+ return this;
76
+ }
77
+ toString() {
78
+ let sql = 'CREATE';
79
+ if (this.temporaryFlag) {
80
+ sql += ' TEMPORARY';
81
+ }
82
+ if (this.unloggedFlag) {
83
+ sql += ' UNLOGGED';
84
+ }
85
+ sql += ' SEQUENCE';
86
+ if (this.ifNotExistsFlag) {
87
+ sql += ' IF NOT EXISTS';
88
+ }
89
+ sql += ' ' + pg_format_1.default.ident(this.sequenceName);
90
+ if (this.options.as) {
91
+ sql += ` AS ${this.options.as}`;
92
+ }
93
+ if (this.options.increment !== undefined) {
94
+ sql += ` INCREMENT BY ${this.options.increment}`;
95
+ }
96
+ if (this.options.minValue !== undefined) {
97
+ sql += ` MINVALUE ${this.options.minValue}`;
98
+ }
99
+ if (this.options.maxValue !== undefined) {
100
+ sql += ` MAXVALUE ${this.options.maxValue}`;
101
+ }
102
+ if (this.options.start !== undefined) {
103
+ sql += ` START WITH ${this.options.start}`;
104
+ }
105
+ if (this.options.cache !== undefined) {
106
+ sql += ` CACHE ${this.options.cache}`;
107
+ }
108
+ if (this.options.cycle === true) {
109
+ sql += ' CYCLE';
110
+ }
111
+ else if (this.options.cycle === false) {
112
+ sql += ' NO CYCLE';
113
+ }
114
+ if (this.options.ownedBy) {
115
+ sql += ` OWNED BY ${this.options.ownedBy}`;
116
+ }
117
+ return sql;
118
+ }
119
+ }
120
+ exports.CreateSequenceBuilder = CreateSequenceBuilder;
121
+ class AlterSequenceBuilder {
122
+ sequenceName;
123
+ options = {};
124
+ ifExistsFlag = false;
125
+ constructor(sequenceName) {
126
+ this.sequenceName = sequenceName;
127
+ }
128
+ ifExists() {
129
+ this.ifExistsFlag = true;
130
+ return this;
131
+ }
132
+ restart(value) {
133
+ this.options.restart = value ?? true;
134
+ return this;
135
+ }
136
+ as(type) {
137
+ this.options.as = type;
138
+ return this;
139
+ }
140
+ start(value) {
141
+ this.options.start = value;
142
+ return this;
143
+ }
144
+ increment(value) {
145
+ this.options.increment = value;
146
+ return this;
147
+ }
148
+ minValue(value) {
149
+ this.options.minValue = value;
150
+ return this;
151
+ }
152
+ maxValue(value) {
153
+ this.options.maxValue = value;
154
+ return this;
155
+ }
156
+ noMinValue() {
157
+ this.options.minValue = undefined;
158
+ return this;
159
+ }
160
+ noMaxValue() {
161
+ this.options.maxValue = undefined;
162
+ return this;
163
+ }
164
+ cache(count) {
165
+ this.options.cache = count;
166
+ return this;
167
+ }
168
+ cycle() {
169
+ this.options.cycle = true;
170
+ return this;
171
+ }
172
+ noCycle() {
173
+ this.options.cycle = false;
174
+ return this;
175
+ }
176
+ ownedBy(tableColumn) {
177
+ this.options.ownedBy = tableColumn;
178
+ return this;
179
+ }
180
+ ownedByNone() {
181
+ this.options.ownedBy = 'NONE';
182
+ return this;
183
+ }
184
+ toString() {
185
+ let sql = 'ALTER SEQUENCE';
186
+ if (this.ifExistsFlag) {
187
+ sql += ' IF EXISTS';
188
+ }
189
+ sql += ' ' + pg_format_1.default.ident(this.sequenceName);
190
+ if (this.options.as) {
191
+ sql += ` AS ${this.options.as}`;
192
+ }
193
+ if (this.options.increment !== undefined) {
194
+ sql += ` INCREMENT BY ${this.options.increment}`;
195
+ }
196
+ if (this.options.minValue !== undefined) {
197
+ sql += ` MINVALUE ${this.options.minValue}`;
198
+ }
199
+ if (this.options.maxValue !== undefined) {
200
+ sql += ` MAXVALUE ${this.options.maxValue}`;
201
+ }
202
+ if (this.options.start !== undefined) {
203
+ sql += ` START WITH ${this.options.start}`;
204
+ }
205
+ if (this.options.cache !== undefined) {
206
+ sql += ` CACHE ${this.options.cache}`;
207
+ }
208
+ if (this.options.cycle === true) {
209
+ sql += ' CYCLE';
210
+ }
211
+ else if (this.options.cycle === false) {
212
+ sql += ' NO CYCLE';
213
+ }
214
+ if (this.options.ownedBy) {
215
+ sql += ` OWNED BY ${this.options.ownedBy}`;
216
+ }
217
+ if (this.options.restart === true) {
218
+ sql += ' RESTART';
219
+ }
220
+ else if (typeof this.options.restart === 'number') {
221
+ sql += ` RESTART WITH ${this.options.restart}`;
222
+ }
223
+ return sql;
224
+ }
225
+ }
226
+ exports.AlterSequenceBuilder = AlterSequenceBuilder;
227
+ class DropSequenceBuilder {
228
+ sequenceNames;
229
+ ifExistsFlag = false;
230
+ cascadeFlag = false;
231
+ restrictFlag = false;
232
+ constructor(sequenceNames) {
233
+ this.sequenceNames = Array.isArray(sequenceNames) ? sequenceNames : [sequenceNames];
234
+ }
235
+ ifExists() {
236
+ this.ifExistsFlag = true;
237
+ return this;
238
+ }
239
+ cascade() {
240
+ this.cascadeFlag = true;
241
+ this.restrictFlag = false;
242
+ return this;
243
+ }
244
+ restrict() {
245
+ this.restrictFlag = true;
246
+ this.cascadeFlag = false;
247
+ return this;
248
+ }
249
+ toString() {
250
+ let sql = 'DROP SEQUENCE';
251
+ if (this.ifExistsFlag) {
252
+ sql += ' IF EXISTS';
253
+ }
254
+ sql += ' ' + this.sequenceNames.map(name => pg_format_1.default.ident(name)).join(', ');
255
+ if (this.cascadeFlag) {
256
+ sql += ' CASCADE';
257
+ }
258
+ else if (this.restrictFlag) {
259
+ sql += ' RESTRICT';
260
+ }
261
+ return sql;
262
+ }
263
+ }
264
+ exports.DropSequenceBuilder = DropSequenceBuilder;