alinea 2.0.0-preview.7 → 2.0.0-preview.8

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 (283) hide show
  1. package/dist/.init/content/pages/welcome.json +1 -1
  2. package/dist/LICENSES.md +120 -120
  3. package/dist/adapter/next/cms.d.ts +18 -4
  4. package/dist/adapter/next/cms.js +125 -36
  5. package/dist/adapter/next/handler.d.ts +5 -1
  6. package/dist/adapter/next/handler.js +14 -7
  7. package/dist/adapter/next/node.d.ts +5 -0
  8. package/dist/adapter/next/node.js +16 -0
  9. package/dist/adapter/next/previews.d.ts +4 -1
  10. package/dist/adapter/next/previews.js +13 -1
  11. package/dist/adapter/next/renderStats.d.ts +24 -0
  12. package/dist/adapter/next/renderStats.js +74 -0
  13. package/dist/adapter/next/syncCheck.d.ts +8 -2
  14. package/dist/adapter/next/syncCheck.js +3 -3
  15. package/dist/adapter/next/with-alinea.js +0 -18
  16. package/dist/backend/Handler.d.ts +2 -2
  17. package/dist/backend/Handler.js +33 -12
  18. package/dist/backend/api/CreateBackend.js +15 -3
  19. package/dist/backend/api/DatabaseApi.js +22 -135
  20. package/dist/backend/api/database.js +15 -3
  21. package/dist/backend/resolver/ParsePreview.d.ts +2 -2
  22. package/dist/backend/router/NodeHandler.js +2 -0
  23. package/dist/backend/store/GeneratedDatabase.d.ts +5 -0
  24. package/dist/backend/store/GeneratedDatabase.js +31 -0
  25. package/dist/backend/store/GeneratedDatabaseNode.d.ts +4 -0
  26. package/dist/backend/store/GeneratedDatabaseNode.js +35 -0
  27. package/dist/backend/util/ORM.js +4 -2
  28. package/dist/bundled.d.ts +1470 -442
  29. package/dist/chunks/bun-sqlite-ZZDB7YQQ.js +18 -0
  30. package/dist/chunks/chunk-2BJ2IFQM.js +1085 -0
  31. package/dist/chunks/chunk-2PIFHEYR.js +364 -0
  32. package/dist/chunks/{chunk-X47OVDF3.js → chunk-2U43BB6P.js} +356 -1000
  33. package/dist/chunks/chunk-76VMZTLN.js +55 -0
  34. package/dist/chunks/chunk-A42DP5T7.js +94 -0
  35. package/dist/chunks/chunk-ATF42T2B.js +35 -0
  36. package/dist/chunks/{chunk-NWQXN4SB.js → chunk-ETBQTP2S.js} +40 -29
  37. package/dist/chunks/chunk-IL7FJQAY.js +94 -0
  38. package/dist/chunks/chunk-K62JP6HR.js +22 -0
  39. package/dist/chunks/chunk-K7LHQXI4.js +103 -0
  40. package/dist/chunks/chunk-M7KDJEGH.js +495 -0
  41. package/dist/chunks/chunk-N47Y62CI.js +396 -0
  42. package/dist/chunks/chunk-NLZLRQ5U.js +100 -0
  43. package/dist/chunks/chunk-QBPYWU6C.js +24 -0
  44. package/dist/chunks/{chunk-6P4UVRPH.js → chunk-RWWYUNVX.js} +2 -1
  45. package/dist/chunks/chunk-TFMUSWGG.js +605 -0
  46. package/dist/chunks/{chunk-I4GC3E65.js → chunk-UPY2RUZF.js} +0 -1
  47. package/dist/chunks/chunk-XCZXR3C2.js +112 -0
  48. package/dist/chunks/chunk-XTWYBWP2.js +81 -0
  49. package/dist/chunks/node-sqlite-7CSGL27V.js +18 -0
  50. package/dist/cli/Generate.js +74 -64
  51. package/dist/cli/Serve.js +1 -1
  52. package/dist/cli/bin.js +2 -1
  53. package/dist/cli/generate/CopyStaticFiles.js +17 -14
  54. package/dist/cli/generate/DevDB.d.ts +22 -5
  55. package/dist/cli/generate/DevDB.js +136 -24
  56. package/dist/cli/serve/CreateLocalServer.js +22 -4
  57. package/dist/cli/serve/LiveReload.d.ts +1 -1
  58. package/dist/cli/serve/LiveReload.js +3 -0
  59. package/dist/cloud/CloudRemote.js +2 -1
  60. package/dist/components/Calendar.js +6 -6
  61. package/dist/components/Checkbox.js +3 -3
  62. package/dist/components/ColorSwatchPicker.js +2 -2
  63. package/dist/components/ComboBox.js +9 -9
  64. package/dist/components/DateField.js +1 -1
  65. package/dist/components/DatePicker.js +6 -6
  66. package/dist/components/DateRangePicker.js +6 -6
  67. package/dist/components/Disclosure.js +9 -9
  68. package/dist/components/Label.js +5 -5
  69. package/dist/components/List.js +33 -33
  70. package/dist/components/Menu.js +3 -3
  71. package/dist/components/Modal.js +3 -3
  72. package/dist/components/MultipleSelect.js +25 -25
  73. package/dist/components/NumberField.js +6 -6
  74. package/dist/components/ProgressCircle.js +3 -3
  75. package/dist/components/RadioGroup.js +2 -2
  76. package/dist/components/SearchField.js +5 -5
  77. package/dist/components/Select.js +7 -7
  78. package/dist/components/Surface.js +2 -2
  79. package/dist/components/Table.js +6 -6
  80. package/dist/components/Tabs.js +2 -2
  81. package/dist/components/TextField.js +6 -6
  82. package/dist/components/Toggle.js +2 -2
  83. package/dist/components/Toolbar.js +2 -2
  84. package/dist/components/Tooltip.js +4 -4
  85. package/dist/components/Tree.d.ts +2 -1
  86. package/dist/components/Tree.js +5 -5
  87. package/dist/core/Document.js +15 -3
  88. package/dist/core/Entry.d.ts +3 -0
  89. package/dist/core/Entry.js +4 -0
  90. package/dist/core/EntrySeed.d.ts +19 -0
  91. package/dist/core/EntrySeed.js +136 -0
  92. package/dist/core/Field.d.ts +7 -0
  93. package/dist/core/Field.js +4 -0
  94. package/dist/core/Filter.d.ts +40 -2
  95. package/dist/core/Filter.js +145 -0
  96. package/dist/core/Graph.d.ts +9 -0
  97. package/dist/core/ListRow.d.ts +1 -0
  98. package/dist/core/ListRow.js +1 -0
  99. package/dist/core/Reference.d.ts +1 -0
  100. package/dist/core/Root.js +1 -1
  101. package/dist/core/Scope.d.ts +2 -0
  102. package/dist/core/Scope.js +16 -1
  103. package/dist/core/Type.d.ts +2 -1
  104. package/dist/core/Type.js +13 -1
  105. package/dist/core/db/EntryAliases.js +1 -11
  106. package/dist/core/db/EntryReference.d.ts +0 -6
  107. package/dist/core/db/IndexEvent.d.ts +2 -1
  108. package/dist/core/db/IndexEvent.js +11 -3
  109. package/dist/core/db/LinkResolver.d.ts +6 -6
  110. package/dist/core/db/LinkResolver.js +0 -52
  111. package/dist/core/db/LocalStore.d.ts +25 -0
  112. package/dist/core/field/ListField.js +19 -0
  113. package/dist/core/field/RecordField.js +8 -0
  114. package/dist/core/field/RichTextField.js +42 -0
  115. package/dist/core/field/UnionField.js +8 -0
  116. package/dist/core/source/FSSource.d.ts +14 -2
  117. package/dist/core/source/FSSource.js +58 -13
  118. package/dist/core/source/GitUtils.d.ts +4 -0
  119. package/dist/core/source/GitUtils.js +9 -10
  120. package/dist/core/source/GithubSource.js +22 -11
  121. package/dist/core/source/IndexedDBSource.d.ts +2 -1
  122. package/dist/core/source/IndexedDBSource.js +67 -53
  123. package/dist/core/source/MemorySource.js +5 -3
  124. package/dist/core/source/OverlaySource.d.ts +5 -2
  125. package/dist/core/source/OverlaySource.js +31 -3
  126. package/dist/core/source/Source.d.ts +3 -0
  127. package/dist/core/source/Source.js +15 -2
  128. package/dist/core/source/Tree.d.ts +10 -7
  129. package/dist/core/source/Tree.js +118 -70
  130. package/dist/core/source/Utils.js +6 -3
  131. package/dist/core/util/Arrays.d.ts +1 -0
  132. package/dist/core/util/Arrays.js +10 -0
  133. package/dist/core/util/Async.d.ts +7 -0
  134. package/dist/core/util/Async.js +15 -0
  135. package/dist/core/util/Checks.js +1 -1
  136. package/dist/core/util/EntryFilenames.d.ts +22 -1
  137. package/dist/core/util/EntryFilenames.js +56 -2
  138. package/dist/core/util/IndexedDB.d.ts +4 -0
  139. package/dist/core/util/IndexedDB.js +20 -0
  140. package/dist/core/util/Paths.js +5 -1
  141. package/dist/dashboard/App.js +3 -3
  142. package/dist/dashboard/ViewKeys.d.ts +1 -0
  143. package/dist/dashboard/ViewKeys.js +1 -0
  144. package/dist/dashboard/app/AccessDenied.js +4 -4
  145. package/dist/dashboard/app/ActivityStatus.js +11 -11
  146. package/dist/dashboard/app/AppShell.js +2 -2
  147. package/dist/dashboard/app/AuthView.js +9 -9
  148. package/dist/dashboard/app/Badge.js +2 -2
  149. package/dist/dashboard/app/CompactField.js +10 -10
  150. package/dist/dashboard/app/DashboardErrorBoundary.js +6 -6
  151. package/dist/dashboard/app/EntryFields.js +2 -2
  152. package/dist/dashboard/app/EntryHeader.js +10 -10
  153. package/dist/dashboard/app/EntryReferences.js +8 -8
  154. package/dist/dashboard/app/EntrySidebar.js +8 -8
  155. package/dist/dashboard/app/EntrySidebarPreview.js +11 -11
  156. package/dist/dashboard/app/EntryTranslationBanner.js +6 -6
  157. package/dist/dashboard/app/Explorer.js +44 -44
  158. package/dist/dashboard/app/ExplorerCards.js +30 -30
  159. package/dist/dashboard/app/ExplorerFileCard.js +17 -17
  160. package/dist/dashboard/app/ExplorerList.js +14 -14
  161. package/dist/dashboard/app/ExplorerModal.js +5 -5
  162. package/dist/dashboard/app/ExplorerTable.js +47 -47
  163. package/dist/dashboard/app/LinkPicker.js +2 -2
  164. package/dist/dashboard/app/LocaleMenu.js +6 -6
  165. package/dist/dashboard/app/SidebarTree.js +76 -40
  166. package/dist/dashboard/app/WorkspaceMenu.js +10 -10
  167. package/dist/dashboard/app/WorkspaceRoots.js +27 -27
  168. package/dist/dashboard/app/editor/FileEditor.js +24 -24
  169. package/dist/dashboard/app/editor/FilePreview.js +6 -6
  170. package/dist/dashboard/app/modals/CreateEntry.js +4 -4
  171. package/dist/dashboard/app/pages/EntryPage.js +15 -15
  172. package/dist/dashboard/app/pages/RootPage.js +3 -3
  173. package/dist/dashboard/app/pages/SplashPage.js +47 -47
  174. package/dist/dashboard/app/pages/UsersPage.js +36 -36
  175. package/dist/dashboard/app/ui/DashboardModal.js +17 -17
  176. package/dist/dashboard/app/ui/Rail.js +4 -4
  177. package/dist/dashboard/atoms/config.d.ts +4 -4
  178. package/dist/dashboard/atoms/entry.d.ts +8 -9
  179. package/dist/dashboard/atoms/entry.js +34 -4
  180. package/dist/dashboard/atoms/explorer.d.ts +2 -0
  181. package/dist/dashboard/atoms/explorer.js +7 -2
  182. package/dist/dashboard/boot/Boot.js +2 -3
  183. package/dist/dashboard/boot/DashboardWorker.d.ts +3 -3
  184. package/dist/dashboard/boot/DashboardWorker.js +78 -27
  185. package/dist/dashboard/boot/LoadWorker.js +2 -3
  186. package/dist/dashboard/icons.d.ts +2 -0
  187. package/dist/dashboard/icons.js +40 -0
  188. package/dist/dashboard/plugins/alineaFixturePlugin.js +14 -3
  189. package/dist/database/BrowserEntryStore.d.ts +30 -0
  190. package/dist/database/BrowserEntryStore.js +168 -0
  191. package/dist/database/DatabaseSource.d.ts +13 -0
  192. package/dist/database/DatabaseSource.js +25 -0
  193. package/dist/database/DatabaseTables.d.ts +38 -0
  194. package/dist/database/DatabaseTables.js +53 -0
  195. package/dist/database/EntryDatabase.d.ts +21 -0
  196. package/dist/database/EntryDatabase.js +138 -0
  197. package/dist/database/EntryLayer.d.ts +93 -0
  198. package/dist/database/EntryLayer.js +459 -0
  199. package/dist/database/EntryStore.d.ts +60 -0
  200. package/dist/database/EntryStore.js +269 -0
  201. package/dist/database/EntryTransaction.d.ts +33 -0
  202. package/dist/database/EntryTransaction.js +889 -0
  203. package/dist/database/EntryUrlAliases.d.ts +2 -0
  204. package/dist/database/EntryUrlAliases.js +69 -0
  205. package/dist/database/LocalDB.d.ts +39 -0
  206. package/dist/database/LocalDB.js +79 -0
  207. package/dist/database/Version.d.ts +9 -0
  208. package/dist/database/Version.js +13 -0
  209. package/dist/database/driver/RuntimeDatabase.d.ts +8 -0
  210. package/dist/database/driver/RuntimeDatabase.js +33 -0
  211. package/dist/database/driver/WasmDatabase.d.ts +8 -0
  212. package/dist/database/driver/WasmDatabase.js +33 -0
  213. package/dist/database/entry/EntryData.d.ts +13 -0
  214. package/dist/database/entry/EntryData.js +51 -0
  215. package/dist/database/entry/EntryTable.d.ts +173 -0
  216. package/dist/database/entry/EntryTable.js +137 -0
  217. package/dist/database/entry/EntryView.d.ts +22 -0
  218. package/dist/database/entry/EntryView.js +106 -0
  219. package/dist/database/query/Condition.d.ts +8 -0
  220. package/dist/database/query/Condition.js +117 -0
  221. package/dist/database/query/EntryQuery.d.ts +43 -0
  222. package/dist/database/query/EntryQuery.js +414 -0
  223. package/dist/database/query/EntryReferences.d.ts +6 -0
  224. package/dist/database/query/EntryReferences.js +68 -0
  225. package/dist/database/query/Relation.d.ts +11 -0
  226. package/dist/database/query/Relation.js +113 -0
  227. package/dist/database/query/ResolveQuery.d.ts +15 -0
  228. package/dist/database/query/ResolveQuery.js +154 -0
  229. package/dist/database/query/Search.d.ts +47 -0
  230. package/dist/database/query/Search.js +169 -0
  231. package/dist/database/sync/Derive.d.ts +8 -0
  232. package/dist/database/sync/Derive.js +270 -0
  233. package/dist/database/sync/EntryParser.d.ts +4 -0
  234. package/dist/database/sync/EntryParser.js +87 -0
  235. package/dist/database/sync/EntrySyncer.d.ts +37 -0
  236. package/dist/database/sync/EntrySyncer.js +117 -0
  237. package/dist/database/sync/Ingest.d.ts +21 -0
  238. package/dist/database/sync/Ingest.js +136 -0
  239. package/dist/database/sync/SyncQueries.d.ts +157 -0
  240. package/dist/database/sync/SyncQueries.js +192 -0
  241. package/dist/field/code/CodeField.view.js +12 -12
  242. package/dist/field/link/LinkField.js +23 -0
  243. package/dist/field/link/LinkField.view.js +41 -40
  244. package/dist/field/list/ListField.view.js +177 -155
  245. package/dist/field/metadata/MetadataField.d.ts +28 -3
  246. package/dist/field/metadata/MetadataField.js +44 -7
  247. package/dist/field/metadata/MetadataField.view.d.ts +5 -0
  248. package/dist/field/metadata/MetadataField.view.js +34 -19
  249. package/dist/field/richtext/RichTextBlock.js +2 -2
  250. package/dist/field/richtext/RichTextDocument.js +4 -3
  251. package/dist/field/richtext/RichTextExtensions.d.ts +1 -1
  252. package/dist/field/richtext/RichTextExtensions.js +32 -28
  253. package/dist/field/richtext/RichTextField.view.js +15 -6
  254. package/dist/field/views.js +2 -0
  255. package/dist/next.cjs +0 -18
  256. package/dist/next.d.ts +4 -3
  257. package/dist/next.edge.d.ts +1 -1
  258. package/dist/next.js +1 -2
  259. package/dist/picker/entry/EntryPicker.js +2 -1
  260. package/dist/picker/url/UrlPicker.js +1 -0
  261. package/dist/preview/widget.d.ts +16 -0
  262. package/dist/preview/widget.js +70 -1
  263. package/dist/query.d.ts +28 -0
  264. package/package.json +2 -1
  265. package/dist/backend/store/GeneratedSource.d.ts +0 -2
  266. package/dist/backend/store/GeneratedSource.js +0 -21
  267. package/dist/chunks/chunk-G6E35DMJ.js +0 -1997
  268. package/dist/core/db/EntryDB.d.ts +0 -19
  269. package/dist/core/db/EntryDB.js +0 -48
  270. package/dist/core/db/EntryIndex.d.ts +0 -143
  271. package/dist/core/db/EntryIndex.js +0 -2773
  272. package/dist/core/db/EntryReferenceIndex.d.ts +0 -21
  273. package/dist/core/db/EntryReferenceIndex.js +0 -131
  274. package/dist/core/db/EntryResolver.d.ts +0 -56
  275. package/dist/core/db/EntryResolver.js +0 -699
  276. package/dist/core/db/EntryTarget.d.ts +0 -5
  277. package/dist/core/db/EntryTransaction.d.ts +0 -31
  278. package/dist/core/db/EntryTransaction.js +0 -956
  279. package/dist/core/db/LocalDB.d.ts +0 -42
  280. package/dist/core/db/LocalDB.js +0 -127
  281. package/dist/core/db/TestDB.d.ts +0 -19
  282. package/dist/core/db/TestDB.js +0 -50
  283. /package/dist/core/db/{EntryTarget.js → LocalStore.js} +0 -0
@@ -1,1997 +0,0 @@
1
- import {
2
- Sql,
3
- getConstraint,
4
- getData,
5
- getEnum,
6
- getField,
7
- getQuery,
8
- getResolver,
9
- getSelection,
10
- getSql,
11
- getTable,
12
- getTarget,
13
- hasConstraint,
14
- hasData,
15
- hasEnum,
16
- hasField,
17
- hasQuery,
18
- hasSelection,
19
- hasSql,
20
- hasTable,
21
- hasTarget,
22
- input,
23
- internalConstraint,
24
- internalCreate,
25
- internalData,
26
- internalDrop,
27
- internalField,
28
- internalQuery,
29
- internalSelection,
30
- internalSql,
31
- internalTable,
32
- internalTarget,
33
- mapToColumn,
34
- sql
35
- } from "./chunk-NWQXN4SB.js";
36
-
37
- // node_modules/rado/dist/core/expr/Conditions.js
38
- function bool(impl) {
39
- return impl.mapWith(Boolean);
40
- }
41
- function binop(operator) {
42
- return (left, right) => bool(
43
- sql`${input(left, right)} ${sql.unsafe(operator)} ${input(right, left)}`
44
- );
45
- }
46
- var eq = binop("=");
47
- var ne = binop("<>");
48
- var gt = binop(">");
49
- var gte = binop(">=");
50
- var lt = binop("<");
51
- var lte = binop("<=");
52
- var like = binop("like");
53
- var notLike = binop("not like");
54
- var ilike = binop("ilike");
55
- var notILike = binop("not ilike");
56
- var arrayContains = binop("@>");
57
- var arrayContained = binop("<@");
58
- var arrayOverlaps = binop("&&");
59
- function and(...conditions) {
60
- const inputs = conditions.filter((v) => v !== void 0).map((i) => input(i));
61
- if (inputs.length === 0) return bool(sql`true`);
62
- if (inputs.length === 1) return bool(inputs[0]);
63
- return bool(sql`(${sql.join(inputs, sql` and `)})`);
64
- }
65
- function or(...conditions) {
66
- const inputs = conditions.filter((v) => v !== void 0).map((i) => input(i));
67
- if (inputs.length === 0) return bool(sql`true`);
68
- if (inputs.length === 1) return bool(inputs[0]);
69
- return bool(sql`(${sql.join(inputs, sql` or `)})`);
70
- }
71
- function not(condition) {
72
- return bool(sql`not ${input(condition)}`);
73
- }
74
- function isNull(value) {
75
- return bool(sql`${input(value)} is null`);
76
- }
77
- function isNotNull(value) {
78
- return bool(sql`${input(value)} is not null`);
79
- }
80
- function asc(input2) {
81
- return sql`${input2} asc`;
82
- }
83
- function when(...cases) {
84
- const compare = cases.length > 1 && !Array.isArray(cases[0]) ? cases.shift() : void 0;
85
- return sql.join([
86
- sql`case`,
87
- compare && input(compare),
88
- sql.join(
89
- cases.map((pair, index) => {
90
- if (Array.isArray(pair)) {
91
- const [condition, result] = pair;
92
- return sql`when ${input(condition)} then ${input(result)}`;
93
- }
94
- if (index === cases.length - 1) return sql`else ${input(pair)}`;
95
- throw new Error("Unexpected else condition");
96
- })
97
- ),
98
- sql`end`
99
- ]);
100
- }
101
-
102
- // node_modules/rado/dist/core/Queries.js
103
- var Executable = class {
104
- #execute;
105
- constructor(exec) {
106
- this.#execute = exec;
107
- }
108
- *[Symbol.iterator]() {
109
- const interim = this.#execute();
110
- const isAsync = interim instanceof Promise;
111
- if (!isAsync) return interim;
112
- let result;
113
- yield interim.then((v) => result = v);
114
- return result;
115
- }
116
- run() {
117
- return this.#execute();
118
- }
119
- async then(onfulfilled, onrejected) {
120
- try {
121
- const result = await this.#execute();
122
- return onfulfilled ? onfulfilled(result) : result;
123
- } catch (error) {
124
- return onrejected ? onrejected(error) : Promise.reject(error);
125
- }
126
- }
127
- catch(onrejected) {
128
- return this.then().catch(onrejected);
129
- }
130
- finally(onfinally) {
131
- return this.then().finally(onfinally);
132
- }
133
- };
134
- var BatchQuery = class extends Executable {
135
- constructor(queryResolver, queries) {
136
- super(() => {
137
- return queryResolver.batch(queries).execute();
138
- });
139
- }
140
- };
141
- var SingleQuery = class extends Executable {
142
- [internalData];
143
- constructor(data) {
144
- super(() => this.#exec(void 0));
145
- this[internalData] = data;
146
- }
147
- #exec(method, db) {
148
- const data = getData(this);
149
- const resolver = db ? getResolver(db) : data.resolver;
150
- if (!resolver) throw new Error("Query has no resolver");
151
- const isSelection = hasSelection(this);
152
- const isFirst = data.first;
153
- const prepared = resolver.prepare(this, "");
154
- const resultType = method ?? (isSelection ? isFirst ? "get" : "all" : "run");
155
- try {
156
- const result = prepared[resultType]();
157
- if (result instanceof Promise)
158
- return result.then((res) => res ?? null).finally(prepared.free.bind(prepared));
159
- prepared.free();
160
- return result ?? null;
161
- } catch (error) {
162
- prepared.free();
163
- throw error;
164
- }
165
- }
166
- all(db) {
167
- return this.#exec("all", db);
168
- }
169
- get(db) {
170
- return this.#exec("get", db);
171
- }
172
- run(db) {
173
- return this.#exec("run", db);
174
- }
175
- async execute(inputs, db) {
176
- const data = getData(this);
177
- const resolver = db ? getResolver(db) : data.resolver;
178
- if (!resolver) throw new Error("Query has no resolver");
179
- const prepared = resolver.prepare(this, "");
180
- try {
181
- return await prepared.execute(inputs);
182
- } finally {
183
- prepared.free();
184
- }
185
- }
186
- prepare(name) {
187
- return getData(this).resolver.prepare(this, name);
188
- }
189
- toSQL(db) {
190
- const resolver = db ? getResolver(db) : getData(this).resolver;
191
- if (!resolver) throw new Error("Query has no resolver");
192
- return resolver.toSQL(this);
193
- }
194
- };
195
-
196
- // node_modules/rado/dist/core/expr/Field.js
197
- var Field = class {
198
- [internalField];
199
- [internalSql];
200
- constructor(targetName, fieldName, source = {}) {
201
- const field = { targetName, fieldName, source };
202
- this[internalField] = field;
203
- const expr = sql.field(field).as(fieldName).mapWith(source);
204
- this[internalSql] = expr;
205
- }
206
- };
207
-
208
- // node_modules/rado/dist/core/Virtual.js
209
- function virtualFields(alias, source) {
210
- return Object.fromEntries(
211
- Object.entries(source).map(([key, value]) => {
212
- if (value && typeof value === "object" && !hasSql(value))
213
- return [key, virtualFields(alias, value)];
214
- return [key, new Field(alias, key, getSql(value))];
215
- })
216
- );
217
- }
218
- function virtualTarget(alias, source) {
219
- const target = {
220
- [internalTarget]: sql.identifier(alias)
221
- };
222
- if (!source || typeof source !== "object")
223
- throw new Error("Cannot alias a non-object");
224
- if (hasSql(source)) {
225
- const expr = getSql(source);
226
- const name = expr.alias;
227
- if (!name) throw new Error("Cannot alias a virtual field without a name");
228
- return Object.assign(new Field(alias, name, expr), target);
229
- }
230
- return {
231
- ...target,
232
- ...virtualFields(alias, source)
233
- };
234
- }
235
-
236
- // node_modules/rado/dist/core/Selection.js
237
- var SqlColumn = class {
238
- constructor(sql2, targetName) {
239
- this.sql = sql2;
240
- this.targetName = targetName;
241
- }
242
- sql;
243
- targetName;
244
- result(ctx) {
245
- const value = ctx.values[ctx.index++];
246
- if (!this.sql.mapFromDriverValue) return value;
247
- return this.sql.mapFromDriverValue(value, ctx.specs);
248
- }
249
- };
250
- var ObjectColumn = class {
251
- constructor(nullable, entries2) {
252
- this.nullable = nullable;
253
- this.entries = entries2;
254
- }
255
- nullable;
256
- entries;
257
- result(ctx) {
258
- const result = {};
259
- let isNullable = this.nullable.size > 0;
260
- for (const entry of this.entries) {
261
- const name = entry[0];
262
- const col = entry[1];
263
- const value = col.result(ctx);
264
- result[name] = value;
265
- if (isNullable) {
266
- if (value === null) {
267
- if (col.targetName && !this.nullable.has(col.targetName))
268
- isNullable = false;
269
- } else {
270
- isNullable = false;
271
- }
272
- }
273
- }
274
- if (isNullable) return null;
275
- return result;
276
- }
277
- };
278
- function firstTargetName(input2) {
279
- const expr = getSql(input2);
280
- if (expr) return hasField(input2) ? getField(input2).targetName : void 0;
281
- if (!input2 || typeof input2 !== "object") return;
282
- for (const value of Object.values(input2)) {
283
- const name = firstTargetName(value);
284
- if (name) return name;
285
- }
286
- }
287
- var Selection = class _Selection {
288
- constructor(input2, nullable = /* @__PURE__ */ new Set()) {
289
- this.input = input2;
290
- this.nullable = nullable;
291
- const root = this.#defineColumn(nullable, input2);
292
- this.mapRow = root.result.bind(root);
293
- }
294
- input;
295
- nullable;
296
- mapRow;
297
- makeVirtual(name) {
298
- return virtualTarget(name, this.input);
299
- }
300
- #defineColumn(nullable, input2) {
301
- const expr = getSql(input2);
302
- if (expr) return new SqlColumn(expr, getField(input2)?.targetName);
303
- return new ObjectColumn(
304
- nullable,
305
- Object.entries(input2).map(([name, value]) => [
306
- name,
307
- this.#defineColumn(nullable, value)
308
- ])
309
- );
310
- }
311
- fieldNames() {
312
- return this.#fieldNames(this.input, /* @__PURE__ */ new Set());
313
- }
314
- aliasOf(input2) {
315
- return this.#aliasOf(this.input, /* @__PURE__ */ new Set(), getSql(input2));
316
- }
317
- #aliasOf(input2, names, target, name) {
318
- const expr = getSql(input2);
319
- if (expr) {
320
- let exprName = name ?? expr.alias;
321
- if (!exprName) return;
322
- while (names.has(exprName)) exprName = `${exprName}_`;
323
- names.add(exprName);
324
- if (expr === target) return exprName;
325
- return;
326
- }
327
- for (const [name2, value] of Object.entries(input2)) {
328
- const alias = this.#aliasOf(value, names, target, name2);
329
- if (alias) return alias;
330
- }
331
- }
332
- #fieldNames(input2, names, name) {
333
- const expr = getSql(input2);
334
- if (expr) {
335
- let exprName = name ?? expr.alias;
336
- if (!exprName) throw new Error("Missing field name");
337
- while (names.has(exprName)) exprName = `${exprName}_`;
338
- return [exprName];
339
- }
340
- return Object.entries(input2).flatMap(
341
- ([name2, value]) => this.#fieldNames(value, names, name2)
342
- );
343
- }
344
- #selectionToSql(input2, names, name) {
345
- const expr = getSql(input2);
346
- if (expr) {
347
- let exprName = name ?? expr.alias;
348
- if (exprName) {
349
- while (names.has(exprName)) exprName = `${exprName}_`;
350
- names.add(exprName);
351
- if (hasField(input2)) {
352
- const field = getField(input2);
353
- if (field.fieldName === exprName) return [expr];
354
- }
355
- return [sql`${expr.forSelection()} as ${sql.identifier(exprName)}`];
356
- }
357
- return [expr];
358
- }
359
- return Object.entries(input2).flatMap(
360
- ([name2, value]) => this.#selectionToSql(value, names, name2)
361
- );
362
- }
363
- get [internalSql]() {
364
- return sql.join(this.#selectionToSql(this.input, /* @__PURE__ */ new Set()), sql`, `);
365
- }
366
- #collectTargetNames(input2, names) {
367
- const expr = getSql(input2);
368
- if (expr) {
369
- if (hasField(input2)) names.add(getField(input2).targetName);
370
- return;
371
- }
372
- for (const value of Object.values(input2))
373
- this.#collectTargetNames(value, names);
374
- }
375
- #targetNames() {
376
- const names = /* @__PURE__ */ new Set();
377
- this.#collectTargetNames(this.input, names);
378
- return names;
379
- }
380
- join(right, operator) {
381
- if (hasSelection(right)) {
382
- const selected2 = getSelection(right);
383
- const alias = firstTargetName(selected2.input);
384
- if (!alias) return this;
385
- const nullable2 = new Set(this.nullable);
386
- if (operator === "leftJoin" || operator === "fullJoin")
387
- nullable2.add(alias);
388
- if (operator === "rightJoin" || operator === "fullJoin")
389
- for (const name of this.#targetNames()) nullable2.add(name);
390
- return new _Selection(this.input, nullable2);
391
- }
392
- if (!hasTable(right)) return this;
393
- const rightTable = getTable(right);
394
- const nullable = new Set(this.nullable);
395
- if (operator === "leftJoin" || operator === "fullJoin")
396
- nullable.add(rightTable.aliased);
397
- if (operator === "rightJoin" || operator === "fullJoin")
398
- for (const name of this.#targetNames()) nullable.add(name);
399
- return new _Selection(this.input, nullable);
400
- }
401
- };
402
- var TableSelection = class extends Selection {
403
- constructor(table2) {
404
- super(table2, /* @__PURE__ */ new Set());
405
- this.table = table2;
406
- }
407
- table;
408
- join(right, operator) {
409
- const leftTable = getTable(this.table);
410
- if (hasSelection(right)) {
411
- const selected2 = getSelection(right);
412
- const alias = firstTargetName(selected2.input);
413
- if (!alias) return this;
414
- const nullable2 = new Set(this.nullable);
415
- if (operator === "rightJoin" || operator === "fullJoin")
416
- nullable2.add(leftTable.aliased);
417
- if (operator === "leftJoin" || operator === "fullJoin")
418
- nullable2.add(alias);
419
- return new Selection(
420
- { [leftTable.aliased]: this.table, [alias]: selected2.input },
421
- nullable2
422
- );
423
- }
424
- if (!hasTable(right)) return this;
425
- const rightTable = getTable(right);
426
- const nullable = new Set(this.nullable);
427
- if (operator === "rightJoin" || operator === "fullJoin")
428
- nullable.add(leftTable.aliased);
429
- if (operator === "leftJoin" || operator === "fullJoin")
430
- nullable.add(rightTable.aliased);
431
- return new JoinSelection([this.table, right], nullable);
432
- }
433
- };
434
- var JoinSelection = class _JoinSelection extends Selection {
435
- constructor(tables, nullable) {
436
- super(
437
- Object.fromEntries(tables.map((table2) => [getTable(table2).aliased, table2])),
438
- nullable
439
- );
440
- this.tables = tables;
441
- }
442
- tables;
443
- join(right, operator) {
444
- if (!hasTable(right)) return this;
445
- const rightTable = getTable(right);
446
- const nullable = new Set(this.nullable);
447
- if (operator === "rightJoin" || operator === "fullJoin")
448
- for (const table2 of this.tables) nullable.add(getTable(table2).aliased);
449
- if (operator === "leftJoin" || operator === "fullJoin")
450
- nullable.add(rightTable.aliased);
451
- return new _JoinSelection([...this.tables, right], nullable);
452
- }
453
- };
454
- var StarSelection = class extends Selection {
455
- get [internalSql]() {
456
- return sql`*`;
457
- }
458
- };
459
- var selected = /* @__PURE__ */ new WeakMap();
460
- function selection(input2) {
461
- if (input2 instanceof Selection) return input2;
462
- if (selected.has(input2)) return selected.get(input2);
463
- const selection2 = hasTable(input2) ? new TableSelection(input2) : new Selection(input2);
464
- selected.set(input2, selection2);
465
- return selection2;
466
- }
467
-
468
- // node_modules/rado/dist/core/query/CTE.js
469
- function formatCTE(query) {
470
- const isRecursive = query.withRecursive;
471
- const definitions = isRecursive ? query.withRecursive : query.with;
472
- if (!definitions) return;
473
- return sql.query({
474
- [isRecursive ? "withRecursive" : "with"]: sql.join(
475
- definitions.map((cte) => {
476
- const query2 = getQuery(cte);
477
- const target = getTarget(cte);
478
- return sql`${target} as (${query2})`;
479
- }),
480
- sql`, `
481
- )
482
- });
483
- }
484
-
485
- // node_modules/rado/dist/core/query/Shared.js
486
- function formatModifiers(modifiers, selected2) {
487
- const { orderBy, limit, offset } = modifiers;
488
- const normalizedLimit = typeof limit === "number" && limit < 0 ? void 0 : limit;
489
- if (!orderBy && normalizedLimit === void 0 && offset === void 0)
490
- return void 0;
491
- return sql.query({
492
- orderBy: orderBy && sql.join(
493
- orderBy.map((part) => {
494
- const inner = getSql(part);
495
- const alias = selected2?.aliasOf(part) ?? inner.alias;
496
- if (alias) return sql.identifier(alias);
497
- return part;
498
- }),
499
- sql`, `
500
- ),
501
- limit: normalizedLimit !== void 0 && input(normalizedLimit),
502
- offset: offset !== void 0 && input(offset)
503
- });
504
- }
505
-
506
- // node_modules/rado/dist/core/query/Delete.js
507
- var Delete = class _Delete extends SingleQuery {
508
- [internalData];
509
- constructor(data) {
510
- super(data);
511
- this[internalData] = data;
512
- if (data.returning) this[internalSelection] = selection(data.returning);
513
- }
514
- get [internalQuery]() {
515
- return deleteQuery(getData(this));
516
- }
517
- limit(limit) {
518
- return new _Delete({ ...getData(this), limit });
519
- }
520
- offset(offset) {
521
- return new _Delete({ ...getData(this), offset });
522
- }
523
- orderBy(...orderBy) {
524
- return new _Delete({ ...getData(this), orderBy });
525
- }
526
- };
527
- var DeleteFrom = class _DeleteFrom extends Delete {
528
- where(...where) {
529
- return new _DeleteFrom({ ...getData(this), where: and(...where) });
530
- }
531
- returning(returning) {
532
- const data = getData(this);
533
- return new Delete({
534
- ...data,
535
- returning: returning ?? data.delete
536
- });
537
- }
538
- };
539
- function deleteQuery(query) {
540
- const { delete: from, where, returning } = query;
541
- const table2 = getTable(from);
542
- return sql.query(
543
- formatCTE(query),
544
- {
545
- deleteFrom: table2.identifier(),
546
- where,
547
- returning: returning && selection(returning)
548
- },
549
- formatModifiers(query)
550
- );
551
- }
552
-
553
- // node_modules/rado/dist/core/query/Select.js
554
- function isUnionQuery(query) {
555
- return Array.isArray(query.select);
556
- }
557
- function mapScalarSelection(query, selected2) {
558
- if (hasSql(selected2)) return query.mapWith(getSql(selected2));
559
- if (selected2 && typeof selected2 === "object") {
560
- const values = Object.values(selected2);
561
- if (values.length === 1) {
562
- const first = values[0];
563
- if (first && typeof first === "object" && hasSql(first))
564
- return query.mapWith(getSql(first));
565
- }
566
- }
567
- return query;
568
- }
569
- var SelectFirst = class extends SingleQuery {
570
- [internalData];
571
- constructor(data) {
572
- const inner = { ...data, first: true };
573
- super(inner);
574
- this[internalData] = inner;
575
- }
576
- get [internalSelection]() {
577
- return querySelection(getData(this));
578
- }
579
- get [internalQuery]() {
580
- return selectQuery(getData(this));
581
- }
582
- get [internalSql]() {
583
- return mapScalarSelection(
584
- sql`(${getQuery(this)})`,
585
- getSelection(this).input
586
- );
587
- }
588
- };
589
- var UnionBase = class extends SingleQuery {
590
- [internalData];
591
- constructor(data) {
592
- super(data);
593
- this[internalData] = data;
594
- }
595
- as(alias) {
596
- const selected2 = getSelection(this);
597
- const fields = selected2.makeVirtual(alias);
598
- return Object.assign({}, fields, {
599
- [internalSelection]: selection(fields),
600
- [internalSql]: mapScalarSelection(
601
- sql`(${getQuery(this)})`,
602
- selected2.input
603
- ),
604
- [internalTarget]: sql`(${getQuery(this)}) as ${sql.identifier(
605
- alias
606
- )}`.inlineFields(true)
607
- });
608
- }
609
- #makeSelf() {
610
- const selected2 = getSelection(this);
611
- return selected2.makeVirtual(Sql.SELF_TARGET);
612
- }
613
- #getSelect(base) {
614
- const data = getData(base);
615
- if (!("compound" in data)) throw new Error("No compound defined");
616
- return data.compound;
617
- }
618
- #selectFields(select) {
619
- return querySelection(select).fieldNames();
620
- }
621
- #segmentSelect(segment) {
622
- const query = segment;
623
- if (isUnionQuery(query)) return query.select[0];
624
- if ("union" in segment) return this.#segmentSelect(segment.union);
625
- if ("unionAll" in segment) return this.#segmentSelect(segment.unionAll);
626
- if ("intersect" in segment) return this.#segmentSelect(segment.intersect);
627
- if ("intersectAll" in segment)
628
- return this.#segmentSelect(segment.intersectAll);
629
- if ("except" in segment) return this.#segmentSelect(segment.except);
630
- if ("exceptAll" in segment) return this.#segmentSelect(segment.exceptAll);
631
- return segment;
632
- }
633
- #unionSegment(op, query) {
634
- switch (op) {
635
- case "union":
636
- return { union: query };
637
- case "unionAll":
638
- return { unionAll: query };
639
- case "intersect":
640
- return { intersect: query };
641
- case "intersectAll":
642
- return { intersectAll: query };
643
- case "except":
644
- return { except: query };
645
- case "exceptAll":
646
- return { exceptAll: query };
647
- }
648
- }
649
- #assertMatchingFields(left, right) {
650
- const fields = this.#selectFields(left[0]);
651
- const assert = (query) => {
652
- const names = this.#selectFields(query);
653
- if (fields.length !== names.length)
654
- throw new Error("Union segments must have the same fields");
655
- for (let i = 0; i < fields.length; i++)
656
- if (fields[i] !== names[i])
657
- throw new Error("Union segments must have the same fields");
658
- };
659
- for (const segment of left.slice(1)) assert(this.#segmentSelect(segment));
660
- for (const segment of right) assert(this.#segmentSelect(segment));
661
- }
662
- #appendCompound(left, op, right) {
663
- const [firstRight, ...restRight] = right.select;
664
- if (restRight.length === 0)
665
- return [...left, this.#unionSegment(op, firstRight)];
666
- return [...left, this.#unionSegment(op, right)];
667
- }
668
- #compound(op, target) {
669
- const left = this.#getSelect(this);
670
- const rightBase = typeof target === "function" ? target(this.#makeSelf()) : target;
671
- const rightData = getData(rightBase);
672
- const rightSelect = this.#getSelect(rightBase);
673
- const right = { ...rightData, select: rightSelect };
674
- this.#assertMatchingFields(left, rightSelect);
675
- const select = this.#appendCompound(left, op, right);
676
- const {
677
- resolver,
678
- with: withDefs,
679
- withRecursive
680
- } = getData(this);
681
- return new Union({
682
- resolver,
683
- with: withDefs,
684
- withRecursive,
685
- select
686
- });
687
- }
688
- union(target) {
689
- return this.#compound("union", target);
690
- }
691
- unionAll(target) {
692
- return this.#compound("unionAll", target);
693
- }
694
- intersect(target) {
695
- return this.#compound("intersect", target);
696
- }
697
- intersectAll(target) {
698
- return this.#compound("intersectAll", target);
699
- }
700
- except(target) {
701
- return this.#compound("except", target);
702
- }
703
- exceptAll(target) {
704
- return this.#compound("exceptAll", target);
705
- }
706
- };
707
- var forKeywords = ["update", "no key update", "share", "key share"];
708
- var Select = class _Select extends UnionBase {
709
- [internalData];
710
- constructor(data) {
711
- const compound = [data];
712
- const withCompound = { ...data, compound };
713
- super(withCompound);
714
- this[internalData] = withCompound;
715
- }
716
- from(from) {
717
- return new _Select({ ...getData(this), from });
718
- }
719
- for(keyword, config = {}) {
720
- if (!forKeywords.includes(keyword))
721
- throw new Error(`Invalid FOR keyword: ${keyword}`);
722
- return new _Select({
723
- ...getData(this),
724
- for: sql.query(sql.unsafe(keyword), {
725
- of: config.of && sql.join([config.of].flat().map(getTarget), sql`, `),
726
- nowait: config.noWait,
727
- skipLocked: config.skipLocked
728
- })
729
- });
730
- }
731
- #fromTarget() {
732
- const { from } = getData(this);
733
- if (!from) throw new Error("No target defined");
734
- if (Array.isArray(from)) return from;
735
- return [from];
736
- }
737
- #join(join) {
738
- return new _Select({ ...getData(this), from: [...this.#fromTarget(), join] });
739
- }
740
- leftJoin(leftJoin, on) {
741
- return this.#join({ leftJoin, on });
742
- }
743
- leftJoinLateral(leftJoinLateral, on) {
744
- return this.#join({ leftJoinLateral, on });
745
- }
746
- rightJoin(rightJoin, on) {
747
- return this.#join({ rightJoin, on });
748
- }
749
- innerJoin(innerJoin, on) {
750
- return this.#join({ innerJoin, on });
751
- }
752
- innerJoinLateral(innerJoinLateral, on) {
753
- return this.#join({ innerJoinLateral, on });
754
- }
755
- fullJoin(fullJoin, on) {
756
- return this.#join({ fullJoin, on });
757
- }
758
- crossJoin(crossJoin) {
759
- return this.#join({ crossJoin });
760
- }
761
- crossJoinLateral(crossJoinLateral) {
762
- return this.#join({ crossJoinLateral });
763
- }
764
- where(...where) {
765
- return new _Select({ ...getData(this), where: and(...where) });
766
- }
767
- groupBy(...groupBy) {
768
- return new _Select({ ...getData(this), groupBy });
769
- }
770
- having(having) {
771
- return new _Select({ ...getData(this), having });
772
- }
773
- orderBy(...orderBy) {
774
- return new _Select({ ...getData(this), orderBy });
775
- }
776
- limit(limit) {
777
- return new _Select({ ...getData(this), limit });
778
- }
779
- offset(offset) {
780
- return new _Select({ ...getData(this), offset });
781
- }
782
- $dynamic() {
783
- return this;
784
- }
785
- $first() {
786
- return new SelectFirst(getData(this));
787
- }
788
- get [internalSelection]() {
789
- return querySelection(getData(this));
790
- }
791
- get [internalQuery]() {
792
- return selectQuery(getData(this));
793
- }
794
- get [internalSql]() {
795
- return sql`(${getQuery(this)})`;
796
- }
797
- };
798
- function collectReferencedTargets(input2, names) {
799
- if (hasSql(input2)) {
800
- if (hasField(input2)) {
801
- const field = getField(input2);
802
- const source = field.source;
803
- if (!(source && typeof source === "object" && hasSql(source)))
804
- names.add(field.targetName);
805
- }
806
- return;
807
- }
808
- if (!input2 || typeof input2 !== "object") return;
809
- for (const value of Object.values(input2))
810
- collectReferencedTargets(value, names);
811
- }
812
- function collectFromTargets(from, names) {
813
- if (!from) return;
814
- const collect = (target) => {
815
- if (hasTable(target)) {
816
- names.add(getTable(target).aliased);
817
- return;
818
- }
819
- if (!hasSelection(target)) return;
820
- collectReferencedTargets(getSelection(target).input, names);
821
- };
822
- if (Array.isArray(from)) {
823
- for (const entry of from) {
824
- if (hasTarget(entry)) collect(entry);
825
- else if (!hasSql(entry)) {
826
- const { target } = joinOp(entry);
827
- if (hasTarget(target)) collect(target);
828
- }
829
- }
830
- return;
831
- }
832
- if (hasTarget(from)) collect(from);
833
- }
834
- function hasUnnamedDerivedSource(input2) {
835
- if (hasField(input2)) {
836
- const source = getField(input2).source;
837
- if (source && typeof source === "object" && hasSql(source)) {
838
- const sourceSql = source;
839
- if (!hasField(sourceSql) && !getSql(sourceSql).alias) return true;
840
- }
841
- return false;
842
- }
843
- if (hasSql(input2)) return false;
844
- if (!input2 || typeof input2 !== "object") return false;
845
- return Object.values(input2).some(
846
- hasUnnamedDerivedSource
847
- );
848
- }
849
- function querySelection({ select, from }) {
850
- if (select) {
851
- if (from) {
852
- const selectedTargets = /* @__PURE__ */ new Set();
853
- const fromTargets = /* @__PURE__ */ new Set();
854
- collectReferencedTargets(select, selectedTargets);
855
- collectFromTargets(from, fromTargets);
856
- if (fromTargets.size > 0) {
857
- for (const targetName of selectedTargets)
858
- if (!fromTargets.has(targetName))
859
- throw new Error(`Unknown target in select: ${targetName}`);
860
- }
861
- }
862
- if (hasSql(select) && hasSelection(select)) {
863
- const selected2 = getSelection(select);
864
- if (!from || !Array.isArray(from)) return selected2;
865
- const [, ...joins2] = from;
866
- return applyJoins(selected2, joins2);
867
- }
868
- if (!from || !Array.isArray(from))
869
- return selection(select);
870
- const [, ...joins] = from;
871
- return applyJoins(selection(select), joins);
872
- }
873
- if (!from) throw new Error("No selection defined");
874
- if (Array.isArray(from)) {
875
- const [target, ...joins] = from;
876
- return applyJoins(selection(target), joins);
877
- }
878
- if (hasSelection(from)) {
879
- const selected2 = getSelection(from);
880
- if (hasUnnamedDerivedSource(selected2.input))
881
- throw new Error("Cannot select all from subquery without alias");
882
- return selected2;
883
- }
884
- return selection(sql`*`);
885
- }
886
- function applyJoins(base, joins) {
887
- let current = base;
888
- for (const join of joins) {
889
- const { target, op } = joinOp(join);
890
- current = current.join(target, op);
891
- }
892
- return current;
893
- }
894
- function joinOp(join) {
895
- const { on, ...rest } = join;
896
- const op = Object.keys(rest)[0];
897
- const target = rest[op];
898
- return { target, op, on };
899
- }
900
- function formatTarget(target) {
901
- if (hasTable(target)) return getTable(target).target();
902
- return getTarget(target);
903
- }
904
- function formatFrom(from) {
905
- if (!from) throw new Error("No target defined");
906
- if (Array.isArray(from)) {
907
- return sql.join(
908
- from.map((join) => {
909
- if (hasTarget(join)) return formatTarget(join);
910
- if (hasSql(join)) return getSql(join);
911
- const { target, op, on } = joinOp(join);
912
- return sql.query({
913
- [op]: hasTarget(target) ? formatTarget(target) : getSql(target),
914
- on
915
- });
916
- })
917
- );
918
- }
919
- return hasTarget(from) ? formatTarget(from) : getSql(from);
920
- }
921
- function selectQuery(query) {
922
- const { from, where, groupBy, having, distinct, distinctOn } = query;
923
- const prefix = distinctOn ? sql`distinct on (${sql.join(distinctOn, sql`, `)})` : distinct && sql`distinct`;
924
- const selected2 = querySelection(query);
925
- const select = sql.join([prefix, selected2]);
926
- return sql.query(
927
- formatCTE(query),
928
- {
929
- select,
930
- from: from && formatFrom(from),
931
- for: query.for,
932
- where,
933
- groupBy: groupBy && sql.join(groupBy, sql`, `),
934
- having: typeof having === "function" ? having(selected2.input) : having
935
- },
936
- formatModifiers(query, selected2)
937
- );
938
- }
939
- var Union = class _Union extends UnionBase {
940
- [internalData];
941
- constructor(data) {
942
- const compound = data.select;
943
- const withCompound = { ...data, compound };
944
- super(withCompound);
945
- this[internalData] = withCompound;
946
- }
947
- get [internalQuery]() {
948
- return unionQuery(getData(this));
949
- }
950
- get [internalSelection]() {
951
- const {
952
- select: [first]
953
- } = getData(this);
954
- return querySelection(first);
955
- }
956
- orderBy(...orderBy) {
957
- return new _Union({ ...getData(this), orderBy });
958
- }
959
- limit(limit) {
960
- return new _Union({ ...getData(this), limit });
961
- }
962
- offset(offset) {
963
- return new _Union({ ...getData(this), offset });
964
- }
965
- };
966
- function unionQuery(query) {
967
- const { select } = query;
968
- let fields;
969
- const firstSelect = (query2) => isUnionQuery(query2) ? query2.select[0] : query2;
970
- const segmentQuery = (query2) => {
971
- if (isUnionQuery(query2)) {
972
- const nested = unionQuery(query2);
973
- return sql.universal({
974
- sqlite: sql`select * from (${nested})`,
975
- default: sql`(${nested})`
976
- });
977
- }
978
- const inner = selectQuery(query2);
979
- return query2.orderBy || query2.limit !== void 0 || query2.offset !== void 0 ? sql`(${inner})` : inner;
980
- };
981
- const segments = sql.join(
982
- select.map((segment, i) => {
983
- if (i === 0) {
984
- fields = querySelection(segment).fieldNames();
985
- return segmentQuery(segment);
986
- }
987
- const op = Object.keys(segment)[0];
988
- const query2 = segment[op];
989
- const names = querySelection(firstSelect(query2)).fieldNames();
990
- if (fields.length !== names.length)
991
- throw new Error("Union segments must have the same fields");
992
- for (let i2 = 0; i2 < fields.length; i2++)
993
- if (fields[i2] !== names[i2])
994
- throw new Error("Union segments must have the same fields");
995
- return sql.query({ [op]: segmentQuery(query2) });
996
- })
997
- );
998
- return sql.query(formatCTE(query), segments, formatModifiers(query));
999
- }
1000
-
1001
- // node_modules/rado/dist/core/query/Insert.js
1002
- var Insert = class extends SingleQuery {
1003
- [internalData];
1004
- constructor(data) {
1005
- super(data);
1006
- this[internalData] = data;
1007
- if (data.returning) this[internalSelection] = selection(data.returning);
1008
- }
1009
- get [internalQuery]() {
1010
- return insertQuery(getData(this));
1011
- }
1012
- };
1013
- var InsertCanReturn = class extends Insert {
1014
- returning(returning) {
1015
- const data = getData(this);
1016
- return new Insert({
1017
- ...data,
1018
- returning: returning ?? data.insert
1019
- });
1020
- }
1021
- };
1022
- var InsertCanConflict = class _InsertCanConflict extends InsertCanReturn {
1023
- onConflictDoNothing(onConflictDoNothing) {
1024
- const { on = [], ...data } = getData(this);
1025
- return new _InsertCanConflict({
1026
- ...data,
1027
- on: [...on, { conflictDoNothing: onConflictDoNothing ?? true }]
1028
- });
1029
- }
1030
- onConflictDoUpdate(onConflict) {
1031
- const { on = [], ...data } = getData(this);
1032
- return new _InsertCanConflict({
1033
- ...data,
1034
- on: [...on, { conflictDoUpdate: onConflict }]
1035
- });
1036
- }
1037
- onDuplicateKeyUpdate(onDuplicateKeyUpdate) {
1038
- const { on = [], ...data } = getData(this);
1039
- return new _InsertCanConflict({
1040
- ...data,
1041
- on: [...on, { duplicateKeyUpdate: onDuplicateKeyUpdate }]
1042
- });
1043
- }
1044
- };
1045
- var InsertInto = class _InsertInto {
1046
- [internalData];
1047
- constructor(data) {
1048
- this[internalData] = data;
1049
- }
1050
- overridingSystemValue() {
1051
- return new _InsertInto({
1052
- ...getData(this),
1053
- overridingSystemValue: true
1054
- });
1055
- }
1056
- values(values) {
1057
- return new InsertCanConflict({
1058
- ...getData(this),
1059
- values
1060
- });
1061
- }
1062
- select(query) {
1063
- const insert = getTable(getData(this).insert);
1064
- const expected = Object.keys(insert.columns);
1065
- const actual = querySelection(getData(query)).fieldNames();
1066
- if (expected.length !== actual.length)
1067
- throw new Error("Insert select fields must match table columns");
1068
- for (let i = 0; i < expected.length; i++) {
1069
- if (expected[i] !== actual[i])
1070
- throw new Error("Insert select fields must match table columns");
1071
- }
1072
- return new InsertCanConflict({
1073
- ...getData(this),
1074
- ...getData(query)
1075
- });
1076
- }
1077
- };
1078
- function formatDefaultValue(value) {
1079
- return sql.universal({
1080
- sqlite: value,
1081
- default: sql`default`
1082
- });
1083
- }
1084
- var defaultKeyword = formatDefaultValue(sql`null`);
1085
- function formatValues(table2, rows) {
1086
- return sql.join(
1087
- rows.map((row) => {
1088
- return sql`(${sql.join(
1089
- Object.entries(table2.columns).map(([key, column2]) => {
1090
- const expr = row[key];
1091
- const columnApi = getData(column2);
1092
- const { $onUpdate, $default, defaultValue } = columnApi;
1093
- if (expr !== void 0) return mapToColumn(columnApi, expr);
1094
- if ($default) return $default();
1095
- if (defaultValue) return formatDefaultValue(defaultValue);
1096
- if ($onUpdate) return $onUpdate();
1097
- return defaultKeyword;
1098
- }),
1099
- sql`, `
1100
- )})`;
1101
- }),
1102
- sql`, `
1103
- );
1104
- }
1105
- function formatUpdates(update) {
1106
- return sql.join(
1107
- Object.entries(update).map(
1108
- ([key, value]) => sql`${sql.identifier(key)} = ${input(value)}`
1109
- ),
1110
- sql`, `
1111
- );
1112
- }
1113
- function formatConflict({
1114
- target,
1115
- targetWhere,
1116
- set,
1117
- where: setWhere
1118
- }) {
1119
- const update = set && formatUpdates(set);
1120
- return sql.query({
1121
- onConflict: sql.join([
1122
- target && sql`(${Array.isArray(target) ? sql.join(target, sql`, `) : target})`,
1123
- targetWhere && sql`where ${targetWhere}`,
1124
- update ? sql.join([
1125
- sql`do update set ${update}`,
1126
- setWhere && sql`where ${setWhere}`
1127
- ]) : sql`do nothing`
1128
- ])
1129
- });
1130
- }
1131
- function formatConflicts(on) {
1132
- return sql.join(
1133
- on.map((conflict) => {
1134
- if ("duplicateKeyUpdate" in conflict)
1135
- return sql.query({
1136
- onDuplicateKeyUpdate: formatUpdates(conflict.duplicateKeyUpdate.set)
1137
- });
1138
- if ("conflictDoUpdate" in conflict)
1139
- return formatConflict(conflict.conflictDoUpdate);
1140
- if ("conflictDoNothing" in conflict)
1141
- return formatConflict(
1142
- conflict.conflictDoNothing === true ? {} : conflict.conflictDoNothing
1143
- );
1144
- throw new Error("Unknown conflict type");
1145
- })
1146
- );
1147
- }
1148
- function insertQuery(query) {
1149
- const { insert, values, select, returning, overridingSystemValue } = query;
1150
- if (!values && !select) throw new Error("No values defined");
1151
- const table2 = getTable(insert);
1152
- const toInsert = values ? sql.query({
1153
- values: formatValues(table2, Array.isArray(values) ? values : [values])
1154
- }) : selectQuery(query);
1155
- const conflicts = query.on ? formatConflicts(query.on) : void 0;
1156
- return sql.query(
1157
- formatCTE(query),
1158
- { insertInto: sql`${table2.identifier()} (${table2.listColumns()})` },
1159
- { overridingSystemValue },
1160
- toInsert,
1161
- conflicts,
1162
- returning && sql.query({ returning: selection(returning) })
1163
- ).inlineFields(false);
1164
- }
1165
-
1166
- // node_modules/rado/dist/core/query/Update.js
1167
- var Update = class _Update extends SingleQuery {
1168
- [internalData];
1169
- constructor(data) {
1170
- super(data);
1171
- this[internalData] = data;
1172
- if (data.returning) this[internalSelection] = selection(data.returning);
1173
- }
1174
- get [internalQuery]() {
1175
- return updateQuery(getData(this));
1176
- }
1177
- limit(limit) {
1178
- return new _Update({ ...getData(this), limit });
1179
- }
1180
- offset(offset) {
1181
- return new _Update({ ...getData(this), offset });
1182
- }
1183
- orderBy(...orderBy) {
1184
- return new _Update({ ...getData(this), orderBy });
1185
- }
1186
- };
1187
- var UpdateTable = class _UpdateTable extends Update {
1188
- set(set) {
1189
- const data = getData(this);
1190
- const hasDefinedValue = Object.values(set).some(
1191
- (value) => value !== void 0
1192
- );
1193
- if (!hasDefinedValue) {
1194
- const hasOnUpdate = Object.values(getTable(data.update).columns).some(
1195
- (column2) => !!getData(column2).$onUpdate
1196
- );
1197
- if (!hasOnUpdate) throw new Error("No values to set");
1198
- }
1199
- return new _UpdateTable({ ...data, set });
1200
- }
1201
- where(...where) {
1202
- return new _UpdateTable({
1203
- ...getData(this),
1204
- where: and(...where)
1205
- });
1206
- }
1207
- returning(returning) {
1208
- const data = getData(this);
1209
- return new Update({ ...data, returning: returning ?? data.update });
1210
- }
1211
- };
1212
- function updateQuery(query) {
1213
- const { update: table2, set: values, where, returning } = query;
1214
- const tableApi = getTable(table2);
1215
- if (!values) throw new Error("Update values are required");
1216
- const assignments = Object.entries(tableApi.columns).flatMap(
1217
- ([key, column2]) => {
1218
- const columnApi = getData(column2);
1219
- const { name, $onUpdate } = columnApi;
1220
- const value = values[key];
1221
- let expr;
1222
- if (value === void 0) {
1223
- if ($onUpdate) expr = $onUpdate();
1224
- else return [];
1225
- } else {
1226
- expr = mapToColumn(columnApi, value);
1227
- }
1228
- const fieldName = name ?? key;
1229
- return [sql`${sql.identifier(fieldName)} = ${expr}`];
1230
- }
1231
- );
1232
- if (assignments.length === 0) throw new Error("No values to set");
1233
- const set = sql.join(assignments, sql`, `);
1234
- return sql.query(
1235
- formatCTE(query),
1236
- {
1237
- update: tableApi.identifier(),
1238
- set,
1239
- where,
1240
- returning: returning && selection(returning)
1241
- },
1242
- formatModifiers(query)
1243
- ).inlineFields(false);
1244
- }
1245
-
1246
- // node_modules/rado/dist/core/Builder.js
1247
- function aliasSql(input2, name) {
1248
- const expr = getSql(input2);
1249
- if (expr.alias) return expr;
1250
- const aliased = new Sql((emitter) => expr.emit(emitter));
1251
- aliased.alias = name;
1252
- aliased.mapFromDriverValue = expr.mapFromDriverValue;
1253
- return aliased;
1254
- }
1255
- function aliasUnnamedFields(input2, name) {
1256
- if (hasSql(input2)) {
1257
- if (!name) return input2;
1258
- return aliasSql(input2, name);
1259
- }
1260
- if (!input2 || typeof input2 !== "object") return input2;
1261
- return Object.fromEntries(
1262
- Object.entries(input2).map(([key, value]) => [
1263
- key,
1264
- aliasUnnamedFields(value, key)
1265
- ])
1266
- );
1267
- }
1268
- var BuilderBase = class {
1269
- [internalData];
1270
- constructor(data = {}) {
1271
- this[internalData] = data;
1272
- }
1273
- $query(query) {
1274
- const data = { ...getData(this), ...query };
1275
- if ("delete" in query) return new Delete(data);
1276
- if ("insert" in query) return new Insert(data);
1277
- if ("update" in query) return new Update(data);
1278
- return new Select(data);
1279
- }
1280
- select(select) {
1281
- return new Select({
1282
- ...getData(this),
1283
- select
1284
- });
1285
- }
1286
- selectDistinct(select) {
1287
- return new Select({
1288
- ...getData(this),
1289
- select,
1290
- distinct: true
1291
- });
1292
- }
1293
- selectDistinctOn(columns, select) {
1294
- return new Select({
1295
- ...getData(this),
1296
- select,
1297
- distinctOn: columns
1298
- });
1299
- }
1300
- update(table2) {
1301
- return new UpdateTable({ ...getData(this), update: table2 });
1302
- }
1303
- insert(into) {
1304
- return new InsertInto({ ...getData(this), insert: into });
1305
- }
1306
- delete(from) {
1307
- return new DeleteFrom({ ...getData(this), delete: from });
1308
- }
1309
- };
1310
- var Builder = class extends BuilderBase {
1311
- $with(cteName, columns) {
1312
- return {
1313
- as(query) {
1314
- let input2 = hasQuery(query) ? hasSelection(query) ? getSelection(query).input : columns : columns;
1315
- const data = hasQuery(query) && hasData(query) ? getData(query) : void 0;
1316
- if (input2 && data?.compound?.length > 1)
1317
- input2 = aliasUnnamedFields(input2);
1318
- const target = virtualTarget(cteName, input2 ?? {});
1319
- return {
1320
- ...target,
1321
- [internalSelection]: new StarSelection(target),
1322
- get [internalQuery]() {
1323
- return hasQuery(query) ? getQuery(query).nameSelf(cteName) : getSql(query);
1324
- }
1325
- };
1326
- }
1327
- };
1328
- }
1329
- with(...definitions) {
1330
- return new BuilderBase({ ...getData(this), with: definitions });
1331
- }
1332
- withRecursive(...definitions) {
1333
- return new BuilderBase({ ...getData(this), withRecursive: definitions });
1334
- }
1335
- };
1336
-
1337
- // node_modules/rado/dist/core/expr/Functions.js
1338
- function callFunction(name, args) {
1339
- return sql`${name}(${sql.join(args.map(input), sql`, `)})`;
1340
- }
1341
- function get(target, method) {
1342
- return target[method] ??= (...args) => {
1343
- return callFunction(sql.identifier(method), args);
1344
- };
1345
- }
1346
- var Functions = new Proxy(/* @__PURE__ */ Object.create(null), { get });
1347
-
1348
- // node_modules/rado/dist/core/Column.js
1349
- function formatType(kind, args) {
1350
- return args.length === 0 ? sql.unsafe(kind) : sql`${sql.unsafe(kind)}(${sql.join(args.map(sql.inline), sql`, `)})`;
1351
- }
1352
- var ColumnType = class {
1353
- constructor(kind, args, sql2 = formatType(kind, args)) {
1354
- this.kind = kind;
1355
- this.args = args;
1356
- this[internalSql] = sql2;
1357
- }
1358
- kind;
1359
- args;
1360
- [internalSql];
1361
- };
1362
- var Column = class _Column {
1363
- [internalData];
1364
- constructor(data) {
1365
- this[internalData] = data;
1366
- }
1367
- notNull() {
1368
- return new _Column({
1369
- ...getData(this),
1370
- notNull: true
1371
- });
1372
- }
1373
- $defaultFn(value) {
1374
- return this.$default(value);
1375
- }
1376
- $default(value) {
1377
- return new _Column({
1378
- ...getData(this),
1379
- $default: () => mapToColumn(
1380
- getData(this),
1381
- typeof value === "function" ? value() : value
1382
- )
1383
- });
1384
- }
1385
- $onUpdateFn(fn) {
1386
- return this.$onUpdate(fn);
1387
- }
1388
- $onUpdate(fn) {
1389
- return new _Column({
1390
- ...getData(this),
1391
- $onUpdate: () => mapToColumn(getData(this), fn())
1392
- });
1393
- }
1394
- default(value) {
1395
- return new _Column({
1396
- ...getData(this),
1397
- defaultValue: input(value)
1398
- });
1399
- }
1400
- defaultNow() {
1401
- return new _Column({
1402
- ...getData(this),
1403
- defaultValue: sql.unsafe("now()")
1404
- });
1405
- }
1406
- primaryKey(options) {
1407
- return new _Column({ ...getData(this), ...options, primary: true });
1408
- }
1409
- unique(name) {
1410
- return new _Column({ ...getData(this), isUnique: true });
1411
- }
1412
- references(foreignField, options) {
1413
- return new _Column({
1414
- ...getData(this),
1415
- references() {
1416
- return getField(
1417
- typeof foreignField === "function" ? foreignField() : foreignField
1418
- );
1419
- },
1420
- referenceOptions: options
1421
- });
1422
- }
1423
- $type() {
1424
- return this;
1425
- }
1426
- };
1427
- var JsonColumn = class extends Column {
1428
- constructor(data) {
1429
- super({ ...data, json: true });
1430
- }
1431
- };
1432
- var column = new Proxy(
1433
- {},
1434
- {
1435
- get(target, method) {
1436
- return target[method] ??= (...args) => {
1437
- while (args.length > 0)
1438
- if (args.at(-1) === void 0) args.pop();
1439
- else break;
1440
- return new ColumnType(method, args);
1441
- };
1442
- }
1443
- }
1444
- );
1445
- function columnConfig(args) {
1446
- if (args.length > 1)
1447
- return { name: args[0], options: args[1] };
1448
- if (typeof args[0] === "string")
1449
- return { name: args[0], options: args[1] };
1450
- return { name: void 0, options: args[0] };
1451
- }
1452
- function formatReferences(fields, options = {}) {
1453
- return sql.query(
1454
- callFunction(
1455
- sql.identifier(fields[0].targetName),
1456
- fields.map((field) => sql.identifier(field.fieldName))
1457
- ),
1458
- {
1459
- onDelete: options.onDelete && sql.unsafe(options.onDelete),
1460
- onUpdate: options.onUpdate && sql.unsafe(options.onUpdate)
1461
- }
1462
- );
1463
- }
1464
- function formatColumn(column2) {
1465
- return sql.query(
1466
- column2.type,
1467
- {
1468
- primaryKey: column2.primary,
1469
- notNull: column2.notNull,
1470
- unique: column2.isUnique,
1471
- nullsNotDistinct: column2.nullsNotDistinct,
1472
- autoincrement: column2.autoIncrement
1473
- },
1474
- column2.defaultValue !== void 0 ? sql`default (${column2.defaultValue})`.inlineValues() : void 0,
1475
- {
1476
- references: column2.references && formatReferences([column2.references()], column2.referenceOptions)
1477
- }
1478
- );
1479
- }
1480
-
1481
- // node_modules/rado/dist/postgres/columns.js
1482
- function parsePgArrayValue(input2) {
1483
- if (!input2.startsWith("{") || !input2.endsWith("}")) return [input2];
1484
- const source = input2.slice(1, -1);
1485
- const parts = [];
1486
- let token = "";
1487
- let depth = 0;
1488
- let inQuotes = false;
1489
- let wasQuoted = false;
1490
- const pushToken = () => {
1491
- if (!wasQuoted && token === "NULL") parts.push("\0NULL");
1492
- else parts.push(token);
1493
- token = "";
1494
- wasQuoted = false;
1495
- };
1496
- for (let i = 0; i < source.length; i++) {
1497
- const char2 = source[i];
1498
- if (inQuotes) {
1499
- if (char2 === "\\") {
1500
- const next = source[++i];
1501
- if (next !== void 0) token += next;
1502
- continue;
1503
- }
1504
- if (char2 === '"') {
1505
- inQuotes = false;
1506
- wasQuoted = true;
1507
- continue;
1508
- }
1509
- token += char2;
1510
- continue;
1511
- }
1512
- if (char2 === '"') {
1513
- inQuotes = true;
1514
- continue;
1515
- }
1516
- if (char2 === "{") {
1517
- depth++;
1518
- token += char2;
1519
- continue;
1520
- }
1521
- if (char2 === "}") {
1522
- depth--;
1523
- token += char2;
1524
- continue;
1525
- }
1526
- if (char2 === "," && depth === 0) {
1527
- pushToken();
1528
- continue;
1529
- }
1530
- token += char2;
1531
- }
1532
- pushToken();
1533
- return parts.map((part) => {
1534
- if (part === "\0NULL") return null;
1535
- if (part.startsWith("{") && part.endsWith("}"))
1536
- return parsePgArrayValue(part);
1537
- return part;
1538
- });
1539
- }
1540
- function serializePgArrayValue(value) {
1541
- return `{${value.map(serializePgArrayItem).join(",")}}`;
1542
- }
1543
- function serializePgArrayItem(value) {
1544
- if (Array.isArray(value)) return serializePgArrayValue(value);
1545
- if (value === null || value === void 0) return "NULL";
1546
- let normalized;
1547
- if (value instanceof Date) normalized = value.toISOString();
1548
- else if (typeof value === "string") normalized = value;
1549
- else if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint")
1550
- normalized = String(value);
1551
- else normalized = JSON.stringify(value);
1552
- const escaped = normalized.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1553
- return `"${escaped}"`;
1554
- }
1555
- function mapArrayItems(value, mapper) {
1556
- if (!Array.isArray(value)) return value;
1557
- return value.map((item) => mapper(item));
1558
- }
1559
- var PgColumn = class _PgColumn extends Column {
1560
- notNull() {
1561
- return new _PgColumn({
1562
- ...getData(this),
1563
- notNull: true
1564
- });
1565
- }
1566
- unique(name, config) {
1567
- return new _PgColumn({
1568
- ...getData(this),
1569
- isUnique: true,
1570
- nullsNotDistinct: config?.nulls === "not distinct",
1571
- name
1572
- });
1573
- }
1574
- array(size) {
1575
- const data = getData(this);
1576
- const mapTo = data.mapToDriverValue;
1577
- const mapFrom = data.mapFromDriverValue;
1578
- return new _PgColumn({
1579
- ...data,
1580
- type: new ColumnType("array", [data.type], sql`${data.type}[${size}]`),
1581
- mapToDriverValue(value) {
1582
- if (!Array.isArray(value)) return value;
1583
- const mapped = mapArrayItems(
1584
- value,
1585
- (item) => item !== null && item !== void 0 && mapTo ? mapTo(item) : item
1586
- );
1587
- return serializePgArrayValue(mapped);
1588
- },
1589
- mapFromDriverValue(value, specs) {
1590
- const parsed = typeof value === "string" ? parsePgArrayValue(value) : value;
1591
- if (!Array.isArray(parsed)) return parsed;
1592
- return mapArrayItems(
1593
- parsed,
1594
- (item) => item !== null && item !== void 0 && mapFrom ? mapFrom(item, specs) : item
1595
- );
1596
- }
1597
- });
1598
- }
1599
- generatedAlwaysAsIdentity() {
1600
- const data = getData(this);
1601
- return new _PgColumn({
1602
- ...data,
1603
- type: new ColumnType(
1604
- data.type.kind,
1605
- data.type.args,
1606
- sql`${data.type} generated always as identity`
1607
- )
1608
- });
1609
- }
1610
- generatedByDefaultAsIdentity() {
1611
- const data = getData(this);
1612
- return new _PgColumn({
1613
- ...data,
1614
- type: new ColumnType(
1615
- data.type.kind,
1616
- data.type.args,
1617
- sql`${data.type} generated by default as identity`
1618
- )
1619
- });
1620
- }
1621
- };
1622
- function integer(name) {
1623
- return new PgColumn({ name, type: column.integer() });
1624
- }
1625
- function oid(name) {
1626
- return new PgColumn({ name, type: column.oid() });
1627
- }
1628
- function boolean(name) {
1629
- return new PgColumn({ name, type: column.boolean() });
1630
- }
1631
- function text(name) {
1632
- return new PgColumn({ name, type: column.text() });
1633
- }
1634
-
1635
- // node_modules/rado/dist/postgres/enum.js
1636
- function enumQuery(enumInfo) {
1637
- const enumIdentifier = enumInfo.schema ? sql.join(
1638
- [sql.identifier(enumInfo.schema), sql.identifier(enumInfo.name)],
1639
- sql`.`
1640
- ) : sql.identifier(enumInfo.name);
1641
- return sql`do $$ begin create type ${enumIdentifier} as enum (${sql.join(
1642
- enumInfo.values.map(sql.inline),
1643
- sql`, `
1644
- )}); exception when duplicate_object then null; end $$`;
1645
- }
1646
- function collectEnumQuery(table2) {
1647
- const enums = [];
1648
- const seen = /* @__PURE__ */ new Set();
1649
- for (const column2 of Object.values(table2.columns)) {
1650
- const data = getData(column2);
1651
- if (!hasEnum(data)) continue;
1652
- const info = getEnum(data);
1653
- const enumKey = info.schema ? `${info.schema}.${info.name}` : info.name;
1654
- if (seen.has(enumKey)) continue;
1655
- seen.add(enumKey);
1656
- enums.push(enumQuery(info));
1657
- }
1658
- return enums;
1659
- }
1660
-
1661
- // node_modules/rado/dist/core/Constraint.js
1662
- var UniqueConstraint = class _UniqueConstraint {
1663
- [internalData];
1664
- constructor(data) {
1665
- this[internalData] = data;
1666
- }
1667
- on(...columns) {
1668
- const fields = columns.map(getField);
1669
- return new _UniqueConstraint({ ...getData(this), fields });
1670
- }
1671
- nullsNotDistinct() {
1672
- return new _UniqueConstraint({ ...getData(this), nullsNotDistinct: true });
1673
- }
1674
- get [internalConstraint]() {
1675
- const { fields, nullsNotDistinct } = getData(this);
1676
- return sql.join([
1677
- sql`unique`,
1678
- nullsNotDistinct ? sql`nulls not distinct` : void 0,
1679
- sql`(${sql.join(
1680
- fields.map((field) => sql.identifier(field.fieldName)),
1681
- sql`, `
1682
- )})`
1683
- ]);
1684
- }
1685
- };
1686
- var PrimaryKeyConstraint = class {
1687
- constructor(data) {
1688
- this.data = data;
1689
- this[internalData] = data;
1690
- }
1691
- data;
1692
- [internalData];
1693
- get [internalConstraint]() {
1694
- const { fields } = getData(this);
1695
- return sql`primary key (${sql.join(
1696
- fields.map((field) => sql.identifier(field.fieldName)),
1697
- sql`, `
1698
- )})`;
1699
- }
1700
- };
1701
- var ForeignKeyConstraint = class _ForeignKeyConstraint {
1702
- [internalData];
1703
- constructor(data) {
1704
- this[internalData] = data;
1705
- }
1706
- references(...fields) {
1707
- return new _ForeignKeyConstraint({
1708
- ...getData(this),
1709
- references: fields.map(getField)
1710
- });
1711
- }
1712
- get [internalConstraint]() {
1713
- const { fields, references } = getData(this);
1714
- return sql`foreign key (${sql.join(
1715
- fields.map((field) => sql.identifier(field.fieldName)),
1716
- sql`, `
1717
- )}) references ${sql.identifier(references[0].targetName)} (${sql.join(
1718
- references.map((field) => sql.identifier(field.fieldName)),
1719
- sql`, `
1720
- )})`;
1721
- }
1722
- };
1723
-
1724
- // node_modules/rado/dist/core/expr/Json.js
1725
- var INDEX_PROPERTY = /^\d+$/;
1726
- function jsonExpr(e) {
1727
- return new Proxy(e, {
1728
- get(target, prop) {
1729
- if (typeof prop !== "string") return Reflect.get(target, prop);
1730
- const isNumber = INDEX_PROPERTY.test(prop);
1731
- return jsonExpr(
1732
- sql.jsonPath({
1733
- target: getSql(target),
1734
- segments: [isNumber ? Number(prop) : prop],
1735
- asSql: true
1736
- }).mapWith({
1737
- mapFromDriverValue(value, specs) {
1738
- return specs.parsesJson ? value : JSON.parse(value);
1739
- }
1740
- })
1741
- );
1742
- }
1743
- });
1744
- }
1745
- function jsonAggregateArray(...args) {
1746
- return callFunction(
1747
- sql.universal({
1748
- // Once sqlite 3.45+ is more commonplace we can use jsonb_group_array
1749
- sqlite: sql`json_group_array`,
1750
- postgres: sql`jsonb_agg`,
1751
- mysql: sql`json_arrayagg`
1752
- }),
1753
- args
1754
- );
1755
- }
1756
- function jsonArray(...args) {
1757
- return callFunction(
1758
- sql.universal({
1759
- postgres: sql`jsonb_build_array`,
1760
- default: sql`json_array`
1761
- }),
1762
- args
1763
- );
1764
- }
1765
-
1766
- // node_modules/rado/dist/core/Index.js
1767
- var IndexData = class {
1768
- fields;
1769
- concurrently;
1770
- unique;
1771
- only;
1772
- using;
1773
- order;
1774
- nulls;
1775
- where;
1776
- };
1777
- var IndexApi = class extends IndexData {
1778
- toSql(tableName, indexName, ifNotExists) {
1779
- const fields = this.fields.map(
1780
- (field) => sql.join([
1781
- field,
1782
- this.order && sql.unsafe(this.order),
1783
- this.nulls && sql`nulls ${sql.unsafe(this.nulls)}`
1784
- ])
1785
- );
1786
- return sql.join([
1787
- sql`create`,
1788
- this.unique && sql`unique`,
1789
- sql`index`,
1790
- this.concurrently && sql`concurrently`,
1791
- ifNotExists && sql`if not exists`,
1792
- sql.identifier(indexName),
1793
- sql`on`,
1794
- this.only && sql`only`,
1795
- sql.identifier(tableName),
1796
- this.using && sql`using ${this.using}`,
1797
- sql`(${sql.join(fields, sql`, `)})`,
1798
- this.where && sql`where ${this.where}`
1799
- ]).inlineFields(false);
1800
- }
1801
- };
1802
- var Index = class _Index {
1803
- [internalData];
1804
- constructor(data) {
1805
- this[internalData] = Object.assign(new IndexApi(), data);
1806
- }
1807
- on(...fields) {
1808
- return new _Index({ ...getData(this), fields });
1809
- }
1810
- concurrently() {
1811
- return new _Index({ ...getData(this), concurrently: true });
1812
- }
1813
- only() {
1814
- return new _Index({ ...getData(this), only: true });
1815
- }
1816
- using(using) {
1817
- return new _Index({ ...getData(this), using });
1818
- }
1819
- asc() {
1820
- return new _Index({ ...getData(this), order: "asc" });
1821
- }
1822
- desc() {
1823
- return new _Index({ ...getData(this), order: "desc" });
1824
- }
1825
- nullsFirst() {
1826
- return new _Index({ ...getData(this), nulls: "first" });
1827
- }
1828
- nullsLast() {
1829
- return new _Index({ ...getData(this), nulls: "last" });
1830
- }
1831
- where(where) {
1832
- return new _Index({ ...getData(this), where });
1833
- }
1834
- };
1835
-
1836
- // node_modules/rado/dist/core/Table.js
1837
- var { assign, fromEntries, entries, keys } = Object;
1838
- var TableData = class {
1839
- name;
1840
- alias;
1841
- schemaName;
1842
- columns;
1843
- config;
1844
- };
1845
- var TableApi = class extends TableData {
1846
- get aliased() {
1847
- return this.alias ?? this.name;
1848
- }
1849
- identifier(altName) {
1850
- return sql.join(
1851
- [
1852
- this.schemaName ? sql.identifier(this.schemaName) : void 0,
1853
- sql.identifier(altName ?? this.name)
1854
- ],
1855
- sql`.`
1856
- );
1857
- }
1858
- target() {
1859
- return sql.join([
1860
- this.identifier(),
1861
- this.alias ? sql`as ${sql.identifier(this.alias)}` : void 0
1862
- ]);
1863
- }
1864
- columnDefinition(name) {
1865
- const columnData = this.columns[name];
1866
- const column2 = getData(columnData);
1867
- const columnName = sql.identifier(column2.name ?? name);
1868
- return sql`${columnName} ${formatColumn(column2)}`;
1869
- }
1870
- createDefinition() {
1871
- const createColumns = keys(this.columns).map(
1872
- (name) => this.columnDefinition(name)
1873
- );
1874
- const createConstraints = entries(this.config ?? {}).filter(([, constraint]) => hasConstraint(constraint)).map(([key, constraint]) => {
1875
- const { name } = getData(constraint);
1876
- return sql`constraint ${sql.identifier(name ?? key)} ${getConstraint(
1877
- constraint
1878
- )}`;
1879
- });
1880
- return sql.join(createColumns.concat(createConstraints), sql`, `);
1881
- }
1882
- listColumns() {
1883
- return sql.join(
1884
- entries(this.columns).map(([name, column2]) => {
1885
- const columnApi = getData(column2);
1886
- const { name: givenName } = columnApi;
1887
- return sql.identifier(givenName ?? name);
1888
- }),
1889
- sql`, `
1890
- );
1891
- }
1892
- createTable(altName, ifNotExists = false) {
1893
- return sql.join([
1894
- sql`create table`,
1895
- ifNotExists ? sql`if not exists` : void 0,
1896
- this.identifier(altName),
1897
- sql`(${this.createDefinition()})`
1898
- ]);
1899
- }
1900
- createIndexes() {
1901
- return entries(this.indexes()).map(([name, index]) => {
1902
- const indexApi = getData(index);
1903
- return indexApi.toSql(this.name, name, false);
1904
- });
1905
- }
1906
- createEnums() {
1907
- return collectEnumQuery(this);
1908
- }
1909
- create() {
1910
- return [...this.createEnums(), this.createTable(), ...this.createIndexes()];
1911
- }
1912
- drop() {
1913
- return [sql`drop table if exists ${this.identifier()}`];
1914
- }
1915
- indexes() {
1916
- return fromEntries(
1917
- entries(this.config ?? {}).filter(([, config]) => config instanceof Index)
1918
- );
1919
- }
1920
- };
1921
- function tableFields(targetName, columns) {
1922
- return fromEntries(
1923
- entries(columns).map(([name, column2]) => {
1924
- const columnApi = getData(column2);
1925
- const { name: givenName } = columnApi;
1926
- const field = new Field(targetName, givenName ?? name, columnApi);
1927
- if (columnApi.json) return [name, jsonExpr(field)];
1928
- return [name, field];
1929
- })
1930
- );
1931
- }
1932
- function primaryKeyColumns(tableApi) {
1933
- const columns = /* @__PURE__ */ new Set();
1934
- for (const [name, column2] of entries(tableApi.columns)) {
1935
- const columnApi = getData(column2);
1936
- if (columnApi.primary) columns.add(columnApi.name ?? name);
1937
- }
1938
- for (const constraint of Object.values(tableApi.config ?? {})) {
1939
- if (!(constraint instanceof PrimaryKeyConstraint)) continue;
1940
- for (const field of getData(constraint).fields) columns.add(field.fieldName);
1941
- }
1942
- return columns;
1943
- }
1944
- function table(name, columns, config, schemaName) {
1945
- const api = assign(new TableApi(), {
1946
- name,
1947
- schemaName,
1948
- columns
1949
- });
1950
- const fields = tableFields(api.aliased, api.columns);
1951
- const table2 = {
1952
- [internalTable]: api,
1953
- [internalTarget]: api.identifier(),
1954
- [internalSelection]: selection(fields),
1955
- get [internalCreate]() {
1956
- return api.create();
1957
- },
1958
- get [internalDrop]() {
1959
- return api.drop();
1960
- },
1961
- ...fields
1962
- };
1963
- if (config) api.config = config(table2);
1964
- return table2;
1965
- }
1966
-
1967
- export {
1968
- Functions,
1969
- ColumnType,
1970
- Column,
1971
- JsonColumn,
1972
- column,
1973
- columnConfig,
1974
- formatColumn,
1975
- eq,
1976
- gt,
1977
- like,
1978
- and,
1979
- or,
1980
- not,
1981
- isNull,
1982
- isNotNull,
1983
- asc,
1984
- when,
1985
- BatchQuery,
1986
- querySelection,
1987
- selectQuery,
1988
- Builder,
1989
- integer,
1990
- oid,
1991
- boolean,
1992
- text,
1993
- jsonAggregateArray,
1994
- jsonArray,
1995
- primaryKeyColumns,
1996
- table
1997
- };