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,1150 @@
1
+ import format from "../../addons/pg-format.js";
2
+ import { RelqBuilderError } from "../errors/relq-errors.js";
3
+ export class CreateSchemaBuilder {
4
+ schemaName;
5
+ ifNotExistsFlag = false;
6
+ authorizationUser;
7
+ constructor(schemaName) {
8
+ this.schemaName = schemaName;
9
+ }
10
+ ifNotExists() {
11
+ this.ifNotExistsFlag = true;
12
+ return this;
13
+ }
14
+ authorization(user) {
15
+ this.authorizationUser = user;
16
+ return this;
17
+ }
18
+ toString() {
19
+ let sql = 'CREATE SCHEMA';
20
+ if (this.ifNotExistsFlag) {
21
+ sql += ' IF NOT EXISTS';
22
+ }
23
+ sql += ` ${format.ident(this.schemaName)}`;
24
+ if (this.authorizationUser) {
25
+ sql += ` AUTHORIZATION ${format.ident(this.authorizationUser)}`;
26
+ }
27
+ return sql;
28
+ }
29
+ }
30
+ export class DropSchemaBuilder {
31
+ schemaName;
32
+ ifExistsFlag = false;
33
+ cascadeFlag = false;
34
+ constructor(schemaName) {
35
+ this.schemaName = schemaName;
36
+ }
37
+ ifExists() {
38
+ this.ifExistsFlag = true;
39
+ return this;
40
+ }
41
+ cascade() {
42
+ this.cascadeFlag = true;
43
+ return this;
44
+ }
45
+ restrict() {
46
+ this.cascadeFlag = false;
47
+ return this;
48
+ }
49
+ toString() {
50
+ let sql = 'DROP SCHEMA';
51
+ if (this.ifExistsFlag) {
52
+ sql += ' IF EXISTS';
53
+ }
54
+ sql += ` ${format.ident(this.schemaName)}`;
55
+ if (this.cascadeFlag) {
56
+ sql += ' CASCADE';
57
+ }
58
+ return sql;
59
+ }
60
+ }
61
+ export class GrantBuilder {
62
+ privileges = [];
63
+ columnPrivileges = [];
64
+ objectType;
65
+ objectNames = [];
66
+ schemaName;
67
+ allInSchema = false;
68
+ grantees = [];
69
+ withGrantOption = false;
70
+ grantedByRole;
71
+ isRoleGrant = false;
72
+ roleNames = [];
73
+ withAdminOption = false;
74
+ withInheritOption;
75
+ withSetOption;
76
+ grant(...privileges) {
77
+ this.privileges.push(...privileges);
78
+ return this;
79
+ }
80
+ select(...columns) {
81
+ if (columns.length > 0) {
82
+ this.columnPrivileges.push({ privilege: 'SELECT', columns });
83
+ }
84
+ else {
85
+ this.privileges.push('SELECT');
86
+ }
87
+ return this;
88
+ }
89
+ insert(...columns) {
90
+ if (columns.length > 0) {
91
+ this.columnPrivileges.push({ privilege: 'INSERT', columns });
92
+ }
93
+ else {
94
+ this.privileges.push('INSERT');
95
+ }
96
+ return this;
97
+ }
98
+ update(...columns) {
99
+ if (columns.length > 0) {
100
+ this.columnPrivileges.push({ privilege: 'UPDATE', columns });
101
+ }
102
+ else {
103
+ this.privileges.push('UPDATE');
104
+ }
105
+ return this;
106
+ }
107
+ references(...columns) {
108
+ if (columns.length > 0) {
109
+ this.columnPrivileges.push({ privilege: 'REFERENCES', columns });
110
+ }
111
+ else {
112
+ this.privileges.push('REFERENCES');
113
+ }
114
+ return this;
115
+ }
116
+ all() {
117
+ this.privileges.push('ALL PRIVILEGES');
118
+ return this;
119
+ }
120
+ onTable(...tableNames) {
121
+ this.objectType = 'TABLE';
122
+ this.objectNames = tableNames;
123
+ return this;
124
+ }
125
+ onSequence(...sequenceNames) {
126
+ this.objectType = 'SEQUENCE';
127
+ this.objectNames = sequenceNames;
128
+ return this;
129
+ }
130
+ onFunction(...functionSignatures) {
131
+ this.objectType = 'FUNCTION';
132
+ this.objectNames = functionSignatures;
133
+ return this;
134
+ }
135
+ onProcedure(...procedureSignatures) {
136
+ this.objectType = 'PROCEDURE';
137
+ this.objectNames = procedureSignatures;
138
+ return this;
139
+ }
140
+ onDatabase(...databaseNames) {
141
+ this.objectType = 'DATABASE';
142
+ this.objectNames = databaseNames;
143
+ return this;
144
+ }
145
+ onSchema(...schemaNames) {
146
+ this.objectType = 'SCHEMA';
147
+ this.objectNames = schemaNames;
148
+ return this;
149
+ }
150
+ onTablespace(...tablespaceNames) {
151
+ this.objectType = 'TABLESPACE';
152
+ this.objectNames = tablespaceNames;
153
+ return this;
154
+ }
155
+ onDomain(...domainNames) {
156
+ this.objectType = 'DOMAIN';
157
+ this.objectNames = domainNames;
158
+ return this;
159
+ }
160
+ onType(...typeNames) {
161
+ this.objectType = 'TYPE';
162
+ this.objectNames = typeNames;
163
+ return this;
164
+ }
165
+ onLanguage(...languageNames) {
166
+ this.objectType = 'LANGUAGE';
167
+ this.objectNames = languageNames;
168
+ return this;
169
+ }
170
+ onForeignDataWrapper(...fdwNames) {
171
+ this.objectType = 'FOREIGN DATA WRAPPER';
172
+ this.objectNames = fdwNames;
173
+ return this;
174
+ }
175
+ onForeignServer(...serverNames) {
176
+ this.objectType = 'FOREIGN SERVER';
177
+ this.objectNames = serverNames;
178
+ return this;
179
+ }
180
+ onLargeObject(...oids) {
181
+ this.objectType = 'LARGE OBJECT';
182
+ this.objectNames = oids.map(String);
183
+ return this;
184
+ }
185
+ on(objectType, ...objectNames) {
186
+ this.objectType = objectType.toUpperCase();
187
+ this.objectNames = objectNames;
188
+ return this;
189
+ }
190
+ onAllTablesInSchema(schemaName) {
191
+ this.objectType = 'ALL TABLES IN SCHEMA';
192
+ this.schemaName = schemaName;
193
+ this.allInSchema = true;
194
+ return this;
195
+ }
196
+ onAllSequencesInSchema(schemaName) {
197
+ this.objectType = 'ALL SEQUENCES IN SCHEMA';
198
+ this.schemaName = schemaName;
199
+ this.allInSchema = true;
200
+ return this;
201
+ }
202
+ onAllFunctionsInSchema(schemaName) {
203
+ this.objectType = 'ALL FUNCTIONS IN SCHEMA';
204
+ this.schemaName = schemaName;
205
+ this.allInSchema = true;
206
+ return this;
207
+ }
208
+ onAllProceduresInSchema(schemaName) {
209
+ this.objectType = 'ALL PROCEDURES IN SCHEMA';
210
+ this.schemaName = schemaName;
211
+ this.allInSchema = true;
212
+ return this;
213
+ }
214
+ onAllRoutinesInSchema(schemaName) {
215
+ this.objectType = 'ALL ROUTINES IN SCHEMA';
216
+ this.schemaName = schemaName;
217
+ this.allInSchema = true;
218
+ return this;
219
+ }
220
+ to(...grantees) {
221
+ this.grantees.push(...grantees);
222
+ return this;
223
+ }
224
+ toPublic() {
225
+ this.grantees.push('PUBLIC');
226
+ return this;
227
+ }
228
+ toCurrentUser() {
229
+ this.grantees.push('CURRENT_USER');
230
+ return this;
231
+ }
232
+ toSessionUser() {
233
+ this.grantees.push('SESSION_USER');
234
+ return this;
235
+ }
236
+ toCurrentRole() {
237
+ this.grantees.push('CURRENT_ROLE');
238
+ return this;
239
+ }
240
+ withGrant() {
241
+ this.withGrantOption = true;
242
+ return this;
243
+ }
244
+ grantedBy(role) {
245
+ this.grantedByRole = role;
246
+ return this;
247
+ }
248
+ roles(...roleNames) {
249
+ this.isRoleGrant = true;
250
+ this.roleNames = roleNames;
251
+ return this;
252
+ }
253
+ withAdmin() {
254
+ this.withAdminOption = true;
255
+ return this;
256
+ }
257
+ withInherit(value = true) {
258
+ this.withInheritOption = value;
259
+ return this;
260
+ }
261
+ withSet(value = true) {
262
+ this.withSetOption = value;
263
+ return this;
264
+ }
265
+ toString() {
266
+ if (this.isRoleGrant) {
267
+ return this.buildRoleGrant();
268
+ }
269
+ return this.buildPrivilegeGrant();
270
+ }
271
+ buildRoleGrant() {
272
+ if (this.roleNames.length === 0) {
273
+ throw new RelqBuilderError('No roles specified for role grant', { builder: 'GrantBuilder', missing: 'roles', hint: 'Use .roles()' });
274
+ }
275
+ if (this.grantees.length === 0) {
276
+ throw new RelqBuilderError('No grantees specified', { builder: 'GrantBuilder', missing: 'grantees', hint: 'Use .to()' });
277
+ }
278
+ const roles = this.roleNames.map(r => format.ident(r)).join(', ');
279
+ const grantees = this.formatGrantees();
280
+ let sql = `GRANT ${roles} TO ${grantees}`;
281
+ const options = [];
282
+ if (this.withAdminOption) {
283
+ options.push('ADMIN OPTION');
284
+ }
285
+ if (this.withInheritOption !== undefined) {
286
+ options.push(this.withInheritOption ? 'INHERIT TRUE' : 'INHERIT FALSE');
287
+ }
288
+ if (this.withSetOption !== undefined) {
289
+ options.push(this.withSetOption ? 'SET TRUE' : 'SET FALSE');
290
+ }
291
+ if (options.length > 0) {
292
+ sql += ` WITH ${options.join(', ')}`;
293
+ }
294
+ if (this.grantedByRole) {
295
+ sql += ` GRANTED BY ${format.ident(this.grantedByRole)}`;
296
+ }
297
+ return sql;
298
+ }
299
+ buildPrivilegeGrant() {
300
+ const allPrivs = this.buildPrivilegeList();
301
+ if (allPrivs.length === 0) {
302
+ throw new RelqBuilderError('No privileges specified', { builder: 'GrantBuilder', missing: 'privileges', hint: 'Use .grant() or column-specific methods' });
303
+ }
304
+ if (!this.objectType) {
305
+ throw new RelqBuilderError('Object type required', { builder: 'GrantBuilder', missing: 'objectType', hint: 'Use .onTable(), .onSchema(), etc.' });
306
+ }
307
+ if (this.grantees.length === 0) {
308
+ throw new RelqBuilderError('No grantees specified', { builder: 'GrantBuilder', missing: 'grantees', hint: 'Use .to()' });
309
+ }
310
+ let sql = `GRANT ${allPrivs}`;
311
+ if (this.allInSchema) {
312
+ sql += ` ON ${this.objectType} ${format.ident(this.schemaName)}`;
313
+ }
314
+ else {
315
+ const objects = this.objectNames.map(n => {
316
+ if (this.objectType === 'LARGE OBJECT') {
317
+ return n;
318
+ }
319
+ if (this.objectType === 'FUNCTION' || this.objectType === 'PROCEDURE' || this.objectType === 'ROUTINE') {
320
+ return n;
321
+ }
322
+ return format.ident(n);
323
+ }).join(', ');
324
+ sql += ` ON ${this.objectType} ${objects}`;
325
+ }
326
+ sql += ` TO ${this.formatGrantees()}`;
327
+ if (this.withGrantOption) {
328
+ sql += ' WITH GRANT OPTION';
329
+ }
330
+ if (this.grantedByRole) {
331
+ sql += ` GRANTED BY ${format.ident(this.grantedByRole)}`;
332
+ }
333
+ return sql;
334
+ }
335
+ buildPrivilegeList() {
336
+ const parts = [...this.privileges];
337
+ for (const cp of this.columnPrivileges) {
338
+ const cols = cp.columns.map(c => format.ident(c)).join(', ');
339
+ parts.push(`${cp.privilege} (${cols})`);
340
+ }
341
+ return parts.join(', ');
342
+ }
343
+ formatGrantees() {
344
+ return this.grantees.map(g => {
345
+ const upper = g.toUpperCase();
346
+ if (upper === 'PUBLIC' || upper === 'CURRENT_USER' || upper === 'SESSION_USER' || upper === 'CURRENT_ROLE') {
347
+ return upper;
348
+ }
349
+ return format.ident(g);
350
+ }).join(', ');
351
+ }
352
+ }
353
+ export class RevokeBuilder {
354
+ privileges = [];
355
+ columnPrivileges = [];
356
+ objectType;
357
+ objectNames = [];
358
+ schemaName;
359
+ allInSchema = false;
360
+ grantees = [];
361
+ cascadeFlag = false;
362
+ restrictFlag = false;
363
+ grantOptionFor = false;
364
+ adminOptionFor = false;
365
+ inheritOptionFor = false;
366
+ setOptionFor = false;
367
+ grantedByRole;
368
+ isRoleRevoke = false;
369
+ roleNames = [];
370
+ revoke(...privileges) {
371
+ this.privileges.push(...privileges);
372
+ return this;
373
+ }
374
+ select(...columns) {
375
+ if (columns.length > 0) {
376
+ this.columnPrivileges.push({ privilege: 'SELECT', columns });
377
+ }
378
+ else {
379
+ this.privileges.push('SELECT');
380
+ }
381
+ return this;
382
+ }
383
+ insert(...columns) {
384
+ if (columns.length > 0) {
385
+ this.columnPrivileges.push({ privilege: 'INSERT', columns });
386
+ }
387
+ else {
388
+ this.privileges.push('INSERT');
389
+ }
390
+ return this;
391
+ }
392
+ update(...columns) {
393
+ if (columns.length > 0) {
394
+ this.columnPrivileges.push({ privilege: 'UPDATE', columns });
395
+ }
396
+ else {
397
+ this.privileges.push('UPDATE');
398
+ }
399
+ return this;
400
+ }
401
+ references(...columns) {
402
+ if (columns.length > 0) {
403
+ this.columnPrivileges.push({ privilege: 'REFERENCES', columns });
404
+ }
405
+ else {
406
+ this.privileges.push('REFERENCES');
407
+ }
408
+ return this;
409
+ }
410
+ all() {
411
+ this.privileges.push('ALL PRIVILEGES');
412
+ return this;
413
+ }
414
+ onTable(...tableNames) {
415
+ this.objectType = 'TABLE';
416
+ this.objectNames = tableNames;
417
+ return this;
418
+ }
419
+ onSequence(...sequenceNames) {
420
+ this.objectType = 'SEQUENCE';
421
+ this.objectNames = sequenceNames;
422
+ return this;
423
+ }
424
+ onFunction(...functionSignatures) {
425
+ this.objectType = 'FUNCTION';
426
+ this.objectNames = functionSignatures;
427
+ return this;
428
+ }
429
+ onProcedure(...procedureSignatures) {
430
+ this.objectType = 'PROCEDURE';
431
+ this.objectNames = procedureSignatures;
432
+ return this;
433
+ }
434
+ onDatabase(...databaseNames) {
435
+ this.objectType = 'DATABASE';
436
+ this.objectNames = databaseNames;
437
+ return this;
438
+ }
439
+ onSchema(...schemaNames) {
440
+ this.objectType = 'SCHEMA';
441
+ this.objectNames = schemaNames;
442
+ return this;
443
+ }
444
+ onTablespace(...tablespaceNames) {
445
+ this.objectType = 'TABLESPACE';
446
+ this.objectNames = tablespaceNames;
447
+ return this;
448
+ }
449
+ onDomain(...domainNames) {
450
+ this.objectType = 'DOMAIN';
451
+ this.objectNames = domainNames;
452
+ return this;
453
+ }
454
+ onType(...typeNames) {
455
+ this.objectType = 'TYPE';
456
+ this.objectNames = typeNames;
457
+ return this;
458
+ }
459
+ onLanguage(...languageNames) {
460
+ this.objectType = 'LANGUAGE';
461
+ this.objectNames = languageNames;
462
+ return this;
463
+ }
464
+ onForeignDataWrapper(...fdwNames) {
465
+ this.objectType = 'FOREIGN DATA WRAPPER';
466
+ this.objectNames = fdwNames;
467
+ return this;
468
+ }
469
+ onForeignServer(...serverNames) {
470
+ this.objectType = 'FOREIGN SERVER';
471
+ this.objectNames = serverNames;
472
+ return this;
473
+ }
474
+ onLargeObject(...oids) {
475
+ this.objectType = 'LARGE OBJECT';
476
+ this.objectNames = oids.map(String);
477
+ return this;
478
+ }
479
+ on(objectType, ...objectNames) {
480
+ this.objectType = objectType.toUpperCase();
481
+ this.objectNames = objectNames;
482
+ return this;
483
+ }
484
+ onAllTablesInSchema(schemaName) {
485
+ this.objectType = 'ALL TABLES IN SCHEMA';
486
+ this.schemaName = schemaName;
487
+ this.allInSchema = true;
488
+ return this;
489
+ }
490
+ onAllSequencesInSchema(schemaName) {
491
+ this.objectType = 'ALL SEQUENCES IN SCHEMA';
492
+ this.schemaName = schemaName;
493
+ this.allInSchema = true;
494
+ return this;
495
+ }
496
+ onAllFunctionsInSchema(schemaName) {
497
+ this.objectType = 'ALL FUNCTIONS IN SCHEMA';
498
+ this.schemaName = schemaName;
499
+ this.allInSchema = true;
500
+ return this;
501
+ }
502
+ onAllProceduresInSchema(schemaName) {
503
+ this.objectType = 'ALL PROCEDURES IN SCHEMA';
504
+ this.schemaName = schemaName;
505
+ this.allInSchema = true;
506
+ return this;
507
+ }
508
+ onAllRoutinesInSchema(schemaName) {
509
+ this.objectType = 'ALL ROUTINES IN SCHEMA';
510
+ this.schemaName = schemaName;
511
+ this.allInSchema = true;
512
+ return this;
513
+ }
514
+ from(...grantees) {
515
+ this.grantees.push(...grantees);
516
+ return this;
517
+ }
518
+ fromPublic() {
519
+ this.grantees.push('PUBLIC');
520
+ return this;
521
+ }
522
+ fromCurrentUser() {
523
+ this.grantees.push('CURRENT_USER');
524
+ return this;
525
+ }
526
+ fromSessionUser() {
527
+ this.grantees.push('SESSION_USER');
528
+ return this;
529
+ }
530
+ fromCurrentRole() {
531
+ this.grantees.push('CURRENT_ROLE');
532
+ return this;
533
+ }
534
+ cascade() {
535
+ this.cascadeFlag = true;
536
+ this.restrictFlag = false;
537
+ return this;
538
+ }
539
+ restrict() {
540
+ this.restrictFlag = true;
541
+ this.cascadeFlag = false;
542
+ return this;
543
+ }
544
+ grantOption() {
545
+ this.grantOptionFor = true;
546
+ return this;
547
+ }
548
+ adminOption() {
549
+ this.adminOptionFor = true;
550
+ return this;
551
+ }
552
+ inheritOption() {
553
+ this.inheritOptionFor = true;
554
+ return this;
555
+ }
556
+ setOption() {
557
+ this.setOptionFor = true;
558
+ return this;
559
+ }
560
+ grantedBy(role) {
561
+ this.grantedByRole = role;
562
+ return this;
563
+ }
564
+ roles(...roleNames) {
565
+ this.isRoleRevoke = true;
566
+ this.roleNames = roleNames;
567
+ return this;
568
+ }
569
+ toString() {
570
+ if (this.isRoleRevoke) {
571
+ return this.buildRoleRevoke();
572
+ }
573
+ return this.buildPrivilegeRevoke();
574
+ }
575
+ buildRoleRevoke() {
576
+ if (this.roleNames.length === 0) {
577
+ throw new RelqBuilderError('No roles specified for role revoke', { builder: 'RevokeBuilder', missing: 'roles', hint: 'Use .roles()' });
578
+ }
579
+ if (this.grantees.length === 0) {
580
+ throw new RelqBuilderError('No grantees specified', { builder: 'RevokeBuilder', missing: 'grantees', hint: 'Use .from()' });
581
+ }
582
+ let sql = 'REVOKE';
583
+ if (this.adminOptionFor) {
584
+ sql += ' ADMIN OPTION FOR';
585
+ }
586
+ else if (this.inheritOptionFor) {
587
+ sql += ' INHERIT OPTION FOR';
588
+ }
589
+ else if (this.setOptionFor) {
590
+ sql += ' SET OPTION FOR';
591
+ }
592
+ const roles = this.roleNames.map(r => format.ident(r)).join(', ');
593
+ sql += ` ${roles} FROM ${this.formatGrantees()}`;
594
+ if (this.grantedByRole) {
595
+ sql += ` GRANTED BY ${format.ident(this.grantedByRole)}`;
596
+ }
597
+ if (this.cascadeFlag) {
598
+ sql += ' CASCADE';
599
+ }
600
+ else if (this.restrictFlag) {
601
+ sql += ' RESTRICT';
602
+ }
603
+ return sql;
604
+ }
605
+ buildPrivilegeRevoke() {
606
+ const allPrivs = this.buildPrivilegeList();
607
+ if (allPrivs.length === 0) {
608
+ throw new RelqBuilderError('No privileges specified', { builder: 'RevokeBuilder', missing: 'privileges', hint: 'Use .revoke() or column-specific methods' });
609
+ }
610
+ if (!this.objectType) {
611
+ throw new RelqBuilderError('Object type required', { builder: 'RevokeBuilder', missing: 'objectType', hint: 'Use .onTable(), .onSchema(), etc.' });
612
+ }
613
+ if (this.grantees.length === 0) {
614
+ throw new RelqBuilderError('No grantees specified', { builder: 'RevokeBuilder', missing: 'grantees', hint: 'Use .from()' });
615
+ }
616
+ let sql = 'REVOKE';
617
+ if (this.grantOptionFor) {
618
+ sql += ' GRANT OPTION FOR';
619
+ }
620
+ sql += ` ${allPrivs}`;
621
+ if (this.allInSchema) {
622
+ sql += ` ON ${this.objectType} ${format.ident(this.schemaName)}`;
623
+ }
624
+ else {
625
+ const objects = this.objectNames.map(n => {
626
+ if (this.objectType === 'LARGE OBJECT') {
627
+ return n;
628
+ }
629
+ if (this.objectType === 'FUNCTION' || this.objectType === 'PROCEDURE' || this.objectType === 'ROUTINE') {
630
+ return n;
631
+ }
632
+ return format.ident(n);
633
+ }).join(', ');
634
+ sql += ` ON ${this.objectType} ${objects}`;
635
+ }
636
+ sql += ` FROM ${this.formatGrantees()}`;
637
+ if (this.grantedByRole) {
638
+ sql += ` GRANTED BY ${format.ident(this.grantedByRole)}`;
639
+ }
640
+ if (this.cascadeFlag) {
641
+ sql += ' CASCADE';
642
+ }
643
+ else if (this.restrictFlag) {
644
+ sql += ' RESTRICT';
645
+ }
646
+ return sql;
647
+ }
648
+ buildPrivilegeList() {
649
+ const parts = [...this.privileges];
650
+ for (const cp of this.columnPrivileges) {
651
+ const cols = cp.columns.map(c => format.ident(c)).join(', ');
652
+ parts.push(`${cp.privilege} (${cols})`);
653
+ }
654
+ return parts.join(', ');
655
+ }
656
+ formatGrantees() {
657
+ return this.grantees.map(g => {
658
+ const upper = g.toUpperCase();
659
+ if (upper === 'PUBLIC' || upper === 'CURRENT_USER' || upper === 'SESSION_USER' || upper === 'CURRENT_ROLE') {
660
+ return upper;
661
+ }
662
+ return format.ident(g);
663
+ }).join(', ');
664
+ }
665
+ }
666
+ export class CreateRoleBuilder {
667
+ roleName;
668
+ ifNotExistsFlag = false;
669
+ options = {};
670
+ constructor(roleName) {
671
+ this.roleName = roleName;
672
+ }
673
+ ifNotExists() {
674
+ this.ifNotExistsFlag = true;
675
+ return this;
676
+ }
677
+ superuser(value = true) {
678
+ this.options.superuser = value;
679
+ return this;
680
+ }
681
+ createdb(value = true) {
682
+ this.options.createdb = value;
683
+ return this;
684
+ }
685
+ createrole(value = true) {
686
+ this.options.createrole = value;
687
+ return this;
688
+ }
689
+ inherit(value = true) {
690
+ this.options.inherit = value;
691
+ return this;
692
+ }
693
+ login(value = true) {
694
+ this.options.login = value;
695
+ return this;
696
+ }
697
+ replication(value = true) {
698
+ this.options.replication = value;
699
+ return this;
700
+ }
701
+ bypassRls(value = true) {
702
+ this.options.bypassRls = value;
703
+ return this;
704
+ }
705
+ connectionLimit(limit) {
706
+ this.options.connectionLimit = limit;
707
+ return this;
708
+ }
709
+ password(pwd) {
710
+ this.options.password = pwd;
711
+ return this;
712
+ }
713
+ validUntil(timestamp) {
714
+ this.options.validUntil = timestamp;
715
+ return this;
716
+ }
717
+ inRole(...roles) {
718
+ this.options.inRole = roles;
719
+ return this;
720
+ }
721
+ role(...roles) {
722
+ this.options.role = roles;
723
+ return this;
724
+ }
725
+ admin(...roles) {
726
+ this.options.admin = roles;
727
+ return this;
728
+ }
729
+ toString() {
730
+ let sql = 'CREATE ROLE';
731
+ if (this.ifNotExistsFlag) {
732
+ sql += ' IF NOT EXISTS';
733
+ }
734
+ sql += ` ${format.ident(this.roleName)}`;
735
+ const opts = this.buildOptions();
736
+ if (opts.length > 0) {
737
+ sql += ` WITH ${opts.join(' ')}`;
738
+ }
739
+ return sql;
740
+ }
741
+ buildOptions() {
742
+ const opts = [];
743
+ if (this.options.superuser !== undefined) {
744
+ opts.push(this.options.superuser ? 'SUPERUSER' : 'NOSUPERUSER');
745
+ }
746
+ if (this.options.createdb !== undefined) {
747
+ opts.push(this.options.createdb ? 'CREATEDB' : 'NOCREATEDB');
748
+ }
749
+ if (this.options.createrole !== undefined) {
750
+ opts.push(this.options.createrole ? 'CREATEROLE' : 'NOCREATEROLE');
751
+ }
752
+ if (this.options.inherit !== undefined) {
753
+ opts.push(this.options.inherit ? 'INHERIT' : 'NOINHERIT');
754
+ }
755
+ if (this.options.login !== undefined) {
756
+ opts.push(this.options.login ? 'LOGIN' : 'NOLOGIN');
757
+ }
758
+ if (this.options.replication !== undefined) {
759
+ opts.push(this.options.replication ? 'REPLICATION' : 'NOREPLICATION');
760
+ }
761
+ if (this.options.bypassRls !== undefined) {
762
+ opts.push(this.options.bypassRls ? 'BYPASSRLS' : 'NOBYPASSRLS');
763
+ }
764
+ if (this.options.connectionLimit !== undefined) {
765
+ opts.push(`CONNECTION LIMIT ${this.options.connectionLimit}`);
766
+ }
767
+ if (this.options.password !== undefined) {
768
+ if (this.options.password === null) {
769
+ opts.push('PASSWORD NULL');
770
+ }
771
+ else {
772
+ opts.push(format('PASSWORD %L', this.options.password));
773
+ }
774
+ }
775
+ if (this.options.validUntil) {
776
+ opts.push(format('VALID UNTIL %L', this.options.validUntil));
777
+ }
778
+ if (this.options.inRole && this.options.inRole.length > 0) {
779
+ opts.push(`IN ROLE ${this.options.inRole.map(r => format.ident(r)).join(', ')}`);
780
+ }
781
+ if (this.options.role && this.options.role.length > 0) {
782
+ opts.push(`ROLE ${this.options.role.map(r => format.ident(r)).join(', ')}`);
783
+ }
784
+ if (this.options.admin && this.options.admin.length > 0) {
785
+ opts.push(`ADMIN ${this.options.admin.map(r => format.ident(r)).join(', ')}`);
786
+ }
787
+ return opts;
788
+ }
789
+ }
790
+ export class AlterRoleBuilder {
791
+ roleName;
792
+ options = {};
793
+ renameToValue;
794
+ setConfig = new Map();
795
+ resetConfig = [];
796
+ inDatabaseName;
797
+ constructor(roleName) {
798
+ this.roleName = roleName;
799
+ }
800
+ superuser(value = true) {
801
+ this.options.superuser = value;
802
+ return this;
803
+ }
804
+ createdb(value = true) {
805
+ this.options.createdb = value;
806
+ return this;
807
+ }
808
+ createrole(value = true) {
809
+ this.options.createrole = value;
810
+ return this;
811
+ }
812
+ inherit(value = true) {
813
+ this.options.inherit = value;
814
+ return this;
815
+ }
816
+ login(value = true) {
817
+ this.options.login = value;
818
+ return this;
819
+ }
820
+ replication(value = true) {
821
+ this.options.replication = value;
822
+ return this;
823
+ }
824
+ bypassRls(value = true) {
825
+ this.options.bypassRls = value;
826
+ return this;
827
+ }
828
+ connectionLimit(limit) {
829
+ this.options.connectionLimit = limit;
830
+ return this;
831
+ }
832
+ password(pwd) {
833
+ this.options.password = pwd;
834
+ return this;
835
+ }
836
+ validUntil(timestamp) {
837
+ this.options.validUntil = timestamp;
838
+ return this;
839
+ }
840
+ renameTo(newName) {
841
+ this.renameToValue = newName;
842
+ return this;
843
+ }
844
+ set(parameter, value) {
845
+ this.setConfig.set(parameter, value);
846
+ return this;
847
+ }
848
+ setDefault(parameter) {
849
+ this.setConfig.set(parameter, null);
850
+ return this;
851
+ }
852
+ reset(parameter) {
853
+ this.resetConfig.push(parameter);
854
+ return this;
855
+ }
856
+ resetAll() {
857
+ this.resetConfig.push('ALL');
858
+ return this;
859
+ }
860
+ inDatabase(database) {
861
+ this.inDatabaseName = database;
862
+ return this;
863
+ }
864
+ toString() {
865
+ if (this.renameToValue) {
866
+ return `ALTER ROLE ${format.ident(this.roleName)} RENAME TO ${format.ident(this.renameToValue)}`;
867
+ }
868
+ const roleSpec = this.inDatabaseName
869
+ ? `ALTER ROLE ${format.ident(this.roleName)} IN DATABASE ${format.ident(this.inDatabaseName)}`
870
+ : `ALTER ROLE ${format.ident(this.roleName)}`;
871
+ if (this.setConfig.size > 0) {
872
+ const entries = Array.from(this.setConfig.entries());
873
+ const [param, value] = entries[0];
874
+ if (value === null) {
875
+ return `${roleSpec} SET ${param} TO DEFAULT`;
876
+ }
877
+ return `${roleSpec} SET ${param} TO ${format('%L', value)}`;
878
+ }
879
+ if (this.resetConfig.length > 0) {
880
+ return `${roleSpec} RESET ${this.resetConfig[0]}`;
881
+ }
882
+ const opts = this.buildOptions();
883
+ if (opts.length > 0) {
884
+ return `ALTER ROLE ${format.ident(this.roleName)} WITH ${opts.join(' ')}`;
885
+ }
886
+ return `ALTER ROLE ${format.ident(this.roleName)}`;
887
+ }
888
+ buildOptions() {
889
+ const opts = [];
890
+ if (this.options.superuser !== undefined) {
891
+ opts.push(this.options.superuser ? 'SUPERUSER' : 'NOSUPERUSER');
892
+ }
893
+ if (this.options.createdb !== undefined) {
894
+ opts.push(this.options.createdb ? 'CREATEDB' : 'NOCREATEDB');
895
+ }
896
+ if (this.options.createrole !== undefined) {
897
+ opts.push(this.options.createrole ? 'CREATEROLE' : 'NOCREATEROLE');
898
+ }
899
+ if (this.options.inherit !== undefined) {
900
+ opts.push(this.options.inherit ? 'INHERIT' : 'NOINHERIT');
901
+ }
902
+ if (this.options.login !== undefined) {
903
+ opts.push(this.options.login ? 'LOGIN' : 'NOLOGIN');
904
+ }
905
+ if (this.options.replication !== undefined) {
906
+ opts.push(this.options.replication ? 'REPLICATION' : 'NOREPLICATION');
907
+ }
908
+ if (this.options.bypassRls !== undefined) {
909
+ opts.push(this.options.bypassRls ? 'BYPASSRLS' : 'NOBYPASSRLS');
910
+ }
911
+ if (this.options.connectionLimit !== undefined) {
912
+ opts.push(`CONNECTION LIMIT ${this.options.connectionLimit}`);
913
+ }
914
+ if (this.options.password !== undefined) {
915
+ if (this.options.password === null) {
916
+ opts.push('PASSWORD NULL');
917
+ }
918
+ else {
919
+ opts.push(format('PASSWORD %L', this.options.password));
920
+ }
921
+ }
922
+ if (this.options.validUntil) {
923
+ opts.push(format('VALID UNTIL %L', this.options.validUntil));
924
+ }
925
+ return opts;
926
+ }
927
+ }
928
+ export class DropRoleBuilder {
929
+ roleNames;
930
+ ifExistsFlag = false;
931
+ constructor(roleNames) {
932
+ this.roleNames = Array.isArray(roleNames) ? roleNames : [roleNames];
933
+ }
934
+ ifExists() {
935
+ this.ifExistsFlag = true;
936
+ return this;
937
+ }
938
+ toString() {
939
+ let sql = 'DROP ROLE';
940
+ if (this.ifExistsFlag) {
941
+ sql += ' IF EXISTS';
942
+ }
943
+ sql += ` ${this.roleNames.map(r => format.ident(r)).join(', ')}`;
944
+ return sql;
945
+ }
946
+ }
947
+ export class SetRoleBuilder {
948
+ roleName;
949
+ resetFlag = false;
950
+ localFlag = false;
951
+ sessionFlag = false;
952
+ role(name) {
953
+ this.roleName = name;
954
+ return this;
955
+ }
956
+ none() {
957
+ this.roleName = 'NONE';
958
+ return this;
959
+ }
960
+ reset() {
961
+ this.resetFlag = true;
962
+ return this;
963
+ }
964
+ local() {
965
+ this.localFlag = true;
966
+ return this;
967
+ }
968
+ session() {
969
+ this.sessionFlag = true;
970
+ return this;
971
+ }
972
+ toString() {
973
+ if (this.resetFlag) {
974
+ return 'RESET ROLE';
975
+ }
976
+ let sql = 'SET';
977
+ if (this.localFlag) {
978
+ sql += ' LOCAL';
979
+ }
980
+ else if (this.sessionFlag) {
981
+ sql += ' SESSION';
982
+ }
983
+ sql += ' ROLE';
984
+ if (this.roleName === 'NONE') {
985
+ sql += ' NONE';
986
+ }
987
+ else if (this.roleName) {
988
+ sql += ` ${format.ident(this.roleName)}`;
989
+ }
990
+ return sql;
991
+ }
992
+ }
993
+ export class ReassignOwnedBuilder {
994
+ oldRoles = [];
995
+ newRole;
996
+ by(...roles) {
997
+ this.oldRoles = roles;
998
+ return this;
999
+ }
1000
+ to(role) {
1001
+ this.newRole = role;
1002
+ return this;
1003
+ }
1004
+ toString() {
1005
+ if (this.oldRoles.length === 0) {
1006
+ throw new RelqBuilderError('No old roles specified', { builder: 'ReassignOwnedBuilder', missing: 'oldRoles', hint: 'Use .by()' });
1007
+ }
1008
+ if (!this.newRole) {
1009
+ throw new RelqBuilderError('No new role specified', { builder: 'ReassignOwnedBuilder', missing: 'newRole', hint: 'Use .to()' });
1010
+ }
1011
+ const oldRolesStr = this.oldRoles.map(r => format.ident(r)).join(', ');
1012
+ return `REASSIGN OWNED BY ${oldRolesStr} TO ${format.ident(this.newRole)}`;
1013
+ }
1014
+ }
1015
+ export class DropOwnedBuilder {
1016
+ roles = [];
1017
+ cascadeFlag = false;
1018
+ restrictFlag = false;
1019
+ by(...roles) {
1020
+ this.roles = roles;
1021
+ return this;
1022
+ }
1023
+ cascade() {
1024
+ this.cascadeFlag = true;
1025
+ this.restrictFlag = false;
1026
+ return this;
1027
+ }
1028
+ restrict() {
1029
+ this.restrictFlag = true;
1030
+ this.cascadeFlag = false;
1031
+ return this;
1032
+ }
1033
+ toString() {
1034
+ if (this.roles.length === 0) {
1035
+ throw new RelqBuilderError('No roles specified', { builder: 'DropOwnedBuilder', missing: 'roles', hint: 'Use .by()' });
1036
+ }
1037
+ const rolesStr = this.roles.map(r => format.ident(r)).join(', ');
1038
+ let sql = `DROP OWNED BY ${rolesStr}`;
1039
+ if (this.cascadeFlag) {
1040
+ sql += ' CASCADE';
1041
+ }
1042
+ else if (this.restrictFlag) {
1043
+ sql += ' RESTRICT';
1044
+ }
1045
+ return sql;
1046
+ }
1047
+ }
1048
+ export class DefaultPrivilegesBuilder {
1049
+ forRoles = [];
1050
+ inSchemas = [];
1051
+ isGrant = true;
1052
+ privileges = [];
1053
+ targetType;
1054
+ grantees = [];
1055
+ withGrantOption = false;
1056
+ cascadeFlag = false;
1057
+ forRole(...roles) {
1058
+ this.forRoles = roles;
1059
+ return this;
1060
+ }
1061
+ inSchema(...schemas) {
1062
+ this.inSchemas = schemas;
1063
+ return this;
1064
+ }
1065
+ grant(...privileges) {
1066
+ this.isGrant = true;
1067
+ this.privileges = privileges;
1068
+ return this;
1069
+ }
1070
+ revoke(...privileges) {
1071
+ this.isGrant = false;
1072
+ this.privileges = privileges;
1073
+ return this;
1074
+ }
1075
+ onTables() {
1076
+ this.targetType = 'TABLES';
1077
+ return this;
1078
+ }
1079
+ onSequences() {
1080
+ this.targetType = 'SEQUENCES';
1081
+ return this;
1082
+ }
1083
+ onFunctions() {
1084
+ this.targetType = 'FUNCTIONS';
1085
+ return this;
1086
+ }
1087
+ onRoutines() {
1088
+ this.targetType = 'ROUTINES';
1089
+ return this;
1090
+ }
1091
+ onTypes() {
1092
+ this.targetType = 'TYPES';
1093
+ return this;
1094
+ }
1095
+ onSchemas() {
1096
+ this.targetType = 'SCHEMAS';
1097
+ return this;
1098
+ }
1099
+ to(...grantees) {
1100
+ this.grantees = grantees;
1101
+ return this;
1102
+ }
1103
+ from(...grantees) {
1104
+ this.grantees = grantees;
1105
+ return this;
1106
+ }
1107
+ withGrant() {
1108
+ this.withGrantOption = true;
1109
+ return this;
1110
+ }
1111
+ cascade() {
1112
+ this.cascadeFlag = true;
1113
+ return this;
1114
+ }
1115
+ toString() {
1116
+ let sql = 'ALTER DEFAULT PRIVILEGES';
1117
+ if (this.forRoles.length > 0) {
1118
+ const roles = this.forRoles.map(r => format.ident(r)).join(', ');
1119
+ sql += ` FOR ROLE ${roles}`;
1120
+ }
1121
+ if (this.inSchemas.length > 0) {
1122
+ const schemas = this.inSchemas.map(s => format.ident(s)).join(', ');
1123
+ sql += ` IN SCHEMA ${schemas}`;
1124
+ }
1125
+ if (this.isGrant) {
1126
+ sql += ` GRANT ${this.privileges.join(', ')} ON ${this.targetType}`;
1127
+ sql += ` TO ${this.formatGrantees()}`;
1128
+ if (this.withGrantOption) {
1129
+ sql += ' WITH GRANT OPTION';
1130
+ }
1131
+ }
1132
+ else {
1133
+ sql += ` REVOKE ${this.privileges.join(', ')} ON ${this.targetType}`;
1134
+ sql += ` FROM ${this.formatGrantees()}`;
1135
+ if (this.cascadeFlag) {
1136
+ sql += ' CASCADE';
1137
+ }
1138
+ }
1139
+ return sql;
1140
+ }
1141
+ formatGrantees() {
1142
+ return this.grantees.map(g => {
1143
+ const upper = g.toUpperCase();
1144
+ if (upper === 'PUBLIC' || upper === 'CURRENT_USER' || upper === 'SESSION_USER' || upper === 'CURRENT_ROLE') {
1145
+ return upper;
1146
+ }
1147
+ return format.ident(g);
1148
+ }).join(', ');
1149
+ }
1150
+ }