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