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
@@ -0,0 +1,1085 @@
1
+ import {
2
+ and
3
+ } from "./chunk-XCZXR3C2.js";
4
+ import {
5
+ Index,
6
+ StarSelection,
7
+ selection,
8
+ virtualTarget
9
+ } from "./chunk-2PIFHEYR.js";
10
+ import {
11
+ input,
12
+ mapToColumn
13
+ } from "./chunk-ATF42T2B.js";
14
+ import {
15
+ Sql,
16
+ cached,
17
+ getData,
18
+ getField,
19
+ getQuery,
20
+ getResolver,
21
+ getSelection,
22
+ getSql,
23
+ getTable,
24
+ getTarget,
25
+ hasData,
26
+ hasField,
27
+ hasQuery,
28
+ hasSelection,
29
+ hasSql,
30
+ hasTable,
31
+ hasTarget,
32
+ internalData,
33
+ internalQuery,
34
+ internalSelection,
35
+ internalSql,
36
+ internalTarget,
37
+ sql
38
+ } from "./chunk-ETBQTP2S.js";
39
+
40
+ // node_modules/rado/dist/core/Queries.js
41
+ var Executable = class {
42
+ #execute;
43
+ constructor(exec) {
44
+ this.#execute = exec;
45
+ }
46
+ *[Symbol.iterator]() {
47
+ const interim = this.#execute();
48
+ const isAsync = interim instanceof Promise;
49
+ if (!isAsync) return interim;
50
+ let result;
51
+ yield interim.then((v) => result = v);
52
+ return result;
53
+ }
54
+ run() {
55
+ return this.#execute();
56
+ }
57
+ async then(onfulfilled, onrejected) {
58
+ try {
59
+ const result = await this.#execute();
60
+ return onfulfilled ? onfulfilled(result) : result;
61
+ } catch (error) {
62
+ return onrejected ? onrejected(error) : Promise.reject(error);
63
+ }
64
+ }
65
+ catch(onrejected) {
66
+ return this.then().catch(onrejected);
67
+ }
68
+ finally(onfinally) {
69
+ return this.then().finally(onfinally);
70
+ }
71
+ };
72
+ var BatchQuery = class extends Executable {
73
+ constructor(queryResolver, queries) {
74
+ super(() => {
75
+ return queryResolver.batch(queries).execute();
76
+ });
77
+ }
78
+ };
79
+ var SingleQuery = class extends Executable {
80
+ [internalData];
81
+ constructor(data) {
82
+ super(() => this.#exec());
83
+ this[internalData] = data;
84
+ }
85
+ #resolver(db) {
86
+ const data = getData(this);
87
+ const resolver = db ? getResolver(db) : data.resolver;
88
+ if (!resolver) throw new Error("Query has no resolver");
89
+ return resolver;
90
+ }
91
+ #exec(db) {
92
+ const data = getData(this);
93
+ const resolver = this.#resolver(db);
94
+ if (!hasSelection(this)) return resolver.run(this);
95
+ return data.first ? resolver.get(this) : resolver.all(this);
96
+ }
97
+ all(db) {
98
+ return this.#resolver(db).all(this);
99
+ }
100
+ get(db) {
101
+ return this.#resolver(db).get(this);
102
+ }
103
+ run(db) {
104
+ return this.#resolver(db).run(this);
105
+ }
106
+ async execute(inputs, db) {
107
+ const data = getData(this);
108
+ const resolver = db ? getResolver(db) : data.resolver;
109
+ if (!resolver) throw new Error("Query has no resolver");
110
+ const prepared = resolver.prepare(this, "");
111
+ try {
112
+ return await prepared.execute(inputs);
113
+ } finally {
114
+ prepared[Symbol.dispose]();
115
+ }
116
+ }
117
+ prepare(name, db) {
118
+ const data = getData(this);
119
+ const resolver = db ? getResolver(db) : data.resolver;
120
+ if (!resolver) throw new Error("Query has no resolver");
121
+ return resolver.prepare(this, name);
122
+ }
123
+ toSQL(db) {
124
+ const resolver = db ? getResolver(db) : getData(this).resolver;
125
+ if (!resolver) throw new Error("Query has no resolver");
126
+ return resolver.toSQL(this);
127
+ }
128
+ };
129
+ var MutationQuery = class extends SingleQuery {
130
+ };
131
+
132
+ // node_modules/rado/dist/core/query/CTE.js
133
+ function formatCTE(query) {
134
+ const isRecursive = query.withRecursive;
135
+ const definitions = isRecursive ? query.withRecursive : query.with;
136
+ if (!definitions) return;
137
+ return sql.query({
138
+ [isRecursive ? "withRecursive" : "with"]: sql.join(
139
+ definitions.map((cte) => {
140
+ const query2 = getQuery(cte);
141
+ const target = getTarget(cte);
142
+ return sql`${target} as (${query2})`;
143
+ }),
144
+ sql`, `
145
+ )
146
+ });
147
+ }
148
+
149
+ // node_modules/rado/dist/core/query/Shared.js
150
+ function formatModifiers(modifiers, selected) {
151
+ const { orderBy, limit, offset } = modifiers;
152
+ const normalizedLimit = typeof limit === "number" && limit < 0 ? void 0 : limit;
153
+ if (!orderBy && normalizedLimit === void 0 && offset === void 0)
154
+ return void 0;
155
+ return sql.query({
156
+ orderBy: orderBy && sql.join(
157
+ orderBy.map((part) => {
158
+ const inner = getSql(part);
159
+ const alias = selected?.aliasOf(part) ?? inner.alias;
160
+ if (alias) return sql.identifier(alias);
161
+ return part;
162
+ }),
163
+ sql`, `
164
+ ),
165
+ limit: normalizedLimit !== void 0 && input(normalizedLimit),
166
+ offset: offset !== void 0 && input(offset)
167
+ });
168
+ }
169
+
170
+ // node_modules/rado/dist/core/query/Delete.js
171
+ var Delete = class _Delete extends MutationQuery {
172
+ [internalData];
173
+ constructor(data) {
174
+ super(data);
175
+ this[internalData] = data;
176
+ if (data.returning) this[internalSelection] = selection(data.returning);
177
+ }
178
+ get [internalQuery]() {
179
+ return cached(
180
+ this,
181
+ internalQuery,
182
+ () => deleteQuery(getData(this))
183
+ );
184
+ }
185
+ limit(limit) {
186
+ return new _Delete({ ...getData(this), limit });
187
+ }
188
+ offset(offset) {
189
+ return new _Delete({ ...getData(this), offset });
190
+ }
191
+ orderBy(...orderBy) {
192
+ return new _Delete({ ...getData(this), orderBy });
193
+ }
194
+ };
195
+ var DeleteFrom = class _DeleteFrom extends Delete {
196
+ where(...where) {
197
+ return new _DeleteFrom({ ...getData(this), where: and(...where) });
198
+ }
199
+ returning(returning) {
200
+ const data = getData(this);
201
+ return new Delete({
202
+ ...data,
203
+ returning: returning ?? data.delete
204
+ });
205
+ }
206
+ };
207
+ function deleteQuery(query) {
208
+ const { delete: from, where, returning } = query;
209
+ const table = getTable(from);
210
+ return sql.query(
211
+ formatCTE(query),
212
+ {
213
+ deleteFrom: table.identifier(),
214
+ where,
215
+ returning: returning && selection(returning)
216
+ },
217
+ formatModifiers(query)
218
+ );
219
+ }
220
+
221
+ // node_modules/rado/dist/core/query/Select.js
222
+ function isUnionQuery(query) {
223
+ return Array.isArray(query.select);
224
+ }
225
+ var SelectFirst = class extends SingleQuery {
226
+ [internalData];
227
+ constructor(data) {
228
+ const inner = { ...data, first: true };
229
+ super(inner);
230
+ this[internalData] = inner;
231
+ }
232
+ get [internalSelection]() {
233
+ return cached(this, internalSelection, () => querySelection(getData(this)));
234
+ }
235
+ get [internalQuery]() {
236
+ return cached(
237
+ this,
238
+ internalQuery,
239
+ () => selectQuery(getData(this))
240
+ );
241
+ }
242
+ get [internalSql]() {
243
+ return cached(
244
+ this,
245
+ internalSql,
246
+ () => getQuery(this).asScalar(getSelection(this).input)
247
+ );
248
+ }
249
+ };
250
+ var UnionBase = class extends SingleQuery {
251
+ [internalData];
252
+ constructor(data) {
253
+ super(data);
254
+ this[internalData] = data;
255
+ }
256
+ get [internalSql]() {
257
+ return cached(
258
+ this,
259
+ internalSql,
260
+ () => getQuery(this).asScalar(getSelection(this).input)
261
+ );
262
+ }
263
+ as(alias) {
264
+ const selected = getSelection(this);
265
+ const fields = selected.makeVirtual(alias);
266
+ return Object.assign({}, fields, {
267
+ [internalSelection]: selection(fields),
268
+ [internalSql]: getQuery(this).asScalar(selected.input),
269
+ [internalTarget]: sql`(${getQuery(this)}) as ${sql.identifier(
270
+ alias
271
+ )}`.inlineFields(true)
272
+ });
273
+ }
274
+ #makeSelf() {
275
+ const selected = getSelection(this);
276
+ return selected.makeVirtual(Sql.SELF_TARGET);
277
+ }
278
+ #getSelect(base) {
279
+ const data = getData(base);
280
+ if (!("compound" in data)) throw new Error("No compound defined");
281
+ return data.compound;
282
+ }
283
+ #selectFields(select) {
284
+ return querySelection(select).fieldNames();
285
+ }
286
+ #segmentSelect(segment) {
287
+ const query = segment;
288
+ if (isUnionQuery(query)) return query.select[0];
289
+ if ("union" in segment) return this.#segmentSelect(segment.union);
290
+ if ("unionAll" in segment) return this.#segmentSelect(segment.unionAll);
291
+ if ("intersect" in segment) return this.#segmentSelect(segment.intersect);
292
+ if ("intersectAll" in segment)
293
+ return this.#segmentSelect(segment.intersectAll);
294
+ if ("except" in segment) return this.#segmentSelect(segment.except);
295
+ if ("exceptAll" in segment) return this.#segmentSelect(segment.exceptAll);
296
+ return segment;
297
+ }
298
+ #unionSegment(op, query) {
299
+ switch (op) {
300
+ case "union":
301
+ return { union: query };
302
+ case "unionAll":
303
+ return { unionAll: query };
304
+ case "intersect":
305
+ return { intersect: query };
306
+ case "intersectAll":
307
+ return { intersectAll: query };
308
+ case "except":
309
+ return { except: query };
310
+ case "exceptAll":
311
+ return { exceptAll: query };
312
+ }
313
+ }
314
+ #assertMatchingFields(left, right) {
315
+ const fields = this.#selectFields(left[0]);
316
+ const assert = (query) => {
317
+ const names = this.#selectFields(query);
318
+ if (fields.length !== names.length)
319
+ throw new Error("Union segments must have the same fields");
320
+ for (let i = 0; i < fields.length; i++)
321
+ if (fields[i] !== names[i])
322
+ throw new Error("Union segments must have the same fields");
323
+ };
324
+ for (const segment of left.slice(1)) assert(this.#segmentSelect(segment));
325
+ for (const segment of right) assert(this.#segmentSelect(segment));
326
+ }
327
+ #appendCompound(left, op, right) {
328
+ const [firstRight, ...restRight] = right.select;
329
+ if (restRight.length === 0)
330
+ return [...left, this.#unionSegment(op, firstRight)];
331
+ return [...left, this.#unionSegment(op, right)];
332
+ }
333
+ #compound(op, target) {
334
+ const left = this.#getSelect(this);
335
+ const rightBase = typeof target === "function" ? target(this.#makeSelf()) : target;
336
+ const rightData = getData(rightBase);
337
+ const rightSelect = this.#getSelect(rightBase);
338
+ const right = { ...rightData, select: rightSelect };
339
+ this.#assertMatchingFields(left, rightSelect);
340
+ const select = this.#appendCompound(left, op, right);
341
+ const {
342
+ resolver,
343
+ with: withDefs,
344
+ withRecursive
345
+ } = getData(this);
346
+ return new Union({
347
+ resolver,
348
+ with: withDefs,
349
+ withRecursive,
350
+ select
351
+ });
352
+ }
353
+ union(target) {
354
+ return this.#compound("union", target);
355
+ }
356
+ unionAll(target) {
357
+ return this.#compound("unionAll", target);
358
+ }
359
+ intersect(target) {
360
+ return this.#compound("intersect", target);
361
+ }
362
+ intersectAll(target) {
363
+ return this.#compound("intersectAll", target);
364
+ }
365
+ except(target) {
366
+ return this.#compound("except", target);
367
+ }
368
+ exceptAll(target) {
369
+ return this.#compound("exceptAll", target);
370
+ }
371
+ };
372
+ var forKeywords = ["update", "no key update", "share", "key share"];
373
+ var Select = class _Select extends UnionBase {
374
+ [internalData];
375
+ constructor(data) {
376
+ const compound = [data];
377
+ const withCompound = { ...data, compound };
378
+ super(withCompound);
379
+ this[internalData] = withCompound;
380
+ }
381
+ from(from, config) {
382
+ return new _Select({ ...getData(this), from, ...config });
383
+ }
384
+ for(keyword, config = {}) {
385
+ if (!forKeywords.includes(keyword))
386
+ throw new Error(`Invalid FOR keyword: ${keyword}`);
387
+ return new _Select({
388
+ ...getData(this),
389
+ for: sql.query(sql.unsafe(keyword), {
390
+ of: config.of && sql.join([config.of].flat().map(getTarget), sql`, `),
391
+ nowait: config.noWait,
392
+ skipLocked: config.skipLocked
393
+ })
394
+ });
395
+ }
396
+ #fromTarget() {
397
+ const { from } = getData(this);
398
+ if (!from) throw new Error("No target defined");
399
+ if (Array.isArray(from)) return from;
400
+ return [from];
401
+ }
402
+ #join(join) {
403
+ return new _Select({ ...getData(this), from: [...this.#fromTarget(), join] });
404
+ }
405
+ leftJoin(leftJoin, on, config) {
406
+ return this.#join({ leftJoin, on, ...config });
407
+ }
408
+ leftJoinLateral(leftJoinLateral, on, config) {
409
+ return this.#join({ leftJoinLateral, on, ...config });
410
+ }
411
+ rightJoin(rightJoin, on, config) {
412
+ return this.#join({ rightJoin, on, ...config });
413
+ }
414
+ innerJoin(innerJoin, on, config) {
415
+ return this.#join({ innerJoin, on, ...config });
416
+ }
417
+ innerJoinLateral(innerJoinLateral, on, config) {
418
+ return this.#join({ innerJoinLateral, on, ...config });
419
+ }
420
+ fullJoin(fullJoin, on, config) {
421
+ return this.#join({ fullJoin, on, ...config });
422
+ }
423
+ crossJoin(crossJoin, config) {
424
+ return this.#join({ crossJoin, ...config });
425
+ }
426
+ crossJoinLateral(crossJoinLateral, config) {
427
+ return this.#join({ crossJoinLateral, ...config });
428
+ }
429
+ where(...where) {
430
+ return new _Select({ ...getData(this), where: and(...where) });
431
+ }
432
+ groupBy(...groupBy) {
433
+ return new _Select({ ...getData(this), groupBy });
434
+ }
435
+ having(having) {
436
+ return new _Select({ ...getData(this), having });
437
+ }
438
+ orderBy(...orderBy) {
439
+ return new _Select({ ...getData(this), orderBy });
440
+ }
441
+ limit(limit) {
442
+ return new _Select({ ...getData(this), limit });
443
+ }
444
+ offset(offset) {
445
+ return new _Select({ ...getData(this), offset });
446
+ }
447
+ $dynamic() {
448
+ return this;
449
+ }
450
+ $first() {
451
+ return new SelectFirst(getData(this));
452
+ }
453
+ get [internalSelection]() {
454
+ return cached(this, internalSelection, () => querySelection(getData(this)));
455
+ }
456
+ get [internalQuery]() {
457
+ return cached(
458
+ this,
459
+ internalQuery,
460
+ () => selectQuery(getData(this))
461
+ );
462
+ }
463
+ };
464
+ function collectReferencedTargets(input2, names) {
465
+ if (hasSql(input2)) {
466
+ if (hasField(input2)) {
467
+ const field = getField(input2);
468
+ const source = field.source;
469
+ if (!(source && typeof source === "object" && hasSql(source)))
470
+ names.add(field.targetName);
471
+ }
472
+ return;
473
+ }
474
+ if (!input2 || typeof input2 !== "object") return;
475
+ for (const value of Object.values(input2))
476
+ collectReferencedTargets(value, names);
477
+ }
478
+ function collectFromTargets(from, names) {
479
+ if (!from) return;
480
+ const collect = (target) => {
481
+ if (hasTable(target)) {
482
+ names.add(getTable(target).aliased);
483
+ return;
484
+ }
485
+ if (!hasSelection(target)) return;
486
+ collectReferencedTargets(getSelection(target).input, names);
487
+ };
488
+ if (Array.isArray(from)) {
489
+ for (const entry of from) {
490
+ if (hasTarget(entry)) collect(entry);
491
+ else if (!hasSql(entry)) {
492
+ const { target } = joinOp(entry);
493
+ if (hasTarget(target)) collect(target);
494
+ }
495
+ }
496
+ return;
497
+ }
498
+ if (hasTarget(from)) collect(from);
499
+ }
500
+ function hasUnnamedDerivedSource(input2) {
501
+ if (hasField(input2)) {
502
+ const source = getField(input2).source;
503
+ if (source && typeof source === "object" && hasSql(source)) {
504
+ const sourceSql = source;
505
+ if (!hasField(sourceSql) && !getSql(sourceSql).alias) return true;
506
+ }
507
+ return false;
508
+ }
509
+ if (hasSql(input2)) return false;
510
+ if (!input2 || typeof input2 !== "object") return false;
511
+ return Object.values(input2).some(
512
+ hasUnnamedDerivedSource
513
+ );
514
+ }
515
+ var selections = /* @__PURE__ */ new WeakMap();
516
+ function querySelection(query) {
517
+ let selected = selections.get(query);
518
+ if (!selected) selections.set(query, selected = createSelection(query));
519
+ return selected;
520
+ }
521
+ function createSelection({ select, from }) {
522
+ if (select) {
523
+ if (from) {
524
+ const selectedTargets = /* @__PURE__ */ new Set();
525
+ const fromTargets = /* @__PURE__ */ new Set();
526
+ collectReferencedTargets(select, selectedTargets);
527
+ collectFromTargets(from, fromTargets);
528
+ if (fromTargets.size > 0) {
529
+ for (const targetName of selectedTargets)
530
+ if (!fromTargets.has(targetName))
531
+ throw new Error(`Unknown target in select: ${targetName}`);
532
+ }
533
+ }
534
+ if (hasSql(select) && hasSelection(select)) {
535
+ const selected = getSelection(select);
536
+ if (!from || !Array.isArray(from)) return selected;
537
+ const [, ...joins2] = from;
538
+ return applyJoins(selected, joins2);
539
+ }
540
+ if (!from || !Array.isArray(from))
541
+ return selection(select);
542
+ const [, ...joins] = from;
543
+ return applyJoins(selection(select), joins);
544
+ }
545
+ if (!from) throw new Error("No selection defined");
546
+ if (Array.isArray(from)) {
547
+ const [target, ...joins] = from;
548
+ return applyJoins(selection(target), joins);
549
+ }
550
+ if (hasSelection(from)) {
551
+ const selected = getSelection(from);
552
+ if (hasUnnamedDerivedSource(selected.input))
553
+ throw new Error("Cannot select all from subquery without alias");
554
+ return selected;
555
+ }
556
+ return selection(sql`*`);
557
+ }
558
+ function applyJoins(base, joins) {
559
+ let current = base;
560
+ for (const join of joins) {
561
+ const { target, op } = joinOp(join);
562
+ current = current.join(target, op);
563
+ }
564
+ return current;
565
+ }
566
+ function joinOp(join) {
567
+ const { on, useIndex, forceIndex, ignoreIndex, indexedBy, ...rest } = join;
568
+ const op = Object.keys(rest)[0];
569
+ const target = rest[op];
570
+ return {
571
+ target,
572
+ op,
573
+ on,
574
+ config: { useIndex, forceIndex, ignoreIndex, indexedBy }
575
+ };
576
+ }
577
+ function indexHintName(hint) {
578
+ if (typeof hint === "string") return hint;
579
+ if (hint instanceof Index) {
580
+ const { name } = getData(hint);
581
+ if (!name) throw new Error("Index hint requires a named index");
582
+ return name;
583
+ }
584
+ throw new Error("Invalid index hint");
585
+ }
586
+ function formatIndexHint(hints) {
587
+ const values = hints ? [hints].flat() : [];
588
+ if (values.length === 0) return;
589
+ const names = sql.join(
590
+ values.map((index) => sql.identifier(indexHintName(index))),
591
+ sql`, `
592
+ );
593
+ return sql`(${names})`;
594
+ }
595
+ function formatIndexHints(config) {
596
+ if (!config) return;
597
+ return sql.query({
598
+ useIndex: formatIndexHint(config.useIndex),
599
+ forceIndex: formatIndexHint(config.forceIndex),
600
+ ignoreIndex: formatIndexHint(config.ignoreIndex),
601
+ indexedBy: config.indexedBy ? sql.identifier(indexHintName(config.indexedBy)) : void 0
602
+ });
603
+ }
604
+ function formatTarget(target, indexHints) {
605
+ const formattedTarget = hasTable(target) ? getTable(target).target() : getTarget(target);
606
+ if (!hasTable(target)) return formattedTarget;
607
+ return sql.join([formattedTarget, formatIndexHints(indexHints)]);
608
+ }
609
+ function formatFrom(from, baseConfig) {
610
+ if (!from) throw new Error("No target defined");
611
+ if (Array.isArray(from)) {
612
+ return sql.join(
613
+ from.map((join) => {
614
+ if (hasTarget(join)) return formatTarget(join, baseConfig);
615
+ if (hasSql(join)) return getSql(join);
616
+ const { target, op, on, config } = joinOp(join);
617
+ return sql.query({
618
+ [op]: hasTarget(target) ? formatTarget(target, config) : getSql(target),
619
+ on
620
+ });
621
+ })
622
+ );
623
+ }
624
+ return hasTarget(from) ? formatTarget(from, baseConfig) : getSql(from);
625
+ }
626
+ function selectQuery(query) {
627
+ const { from, where, groupBy, having, distinct, distinctOn } = query;
628
+ const prefix = distinctOn ? sql`distinct on (${sql.join(distinctOn, sql`, `)})` : distinct && sql`distinct`;
629
+ const selected = querySelection(query);
630
+ const select = sql.join([prefix, selected]);
631
+ return sql.query(
632
+ formatCTE(query),
633
+ {
634
+ select,
635
+ from: from && formatFrom(from, query),
636
+ for: query.for,
637
+ where,
638
+ groupBy: groupBy && sql.join(groupBy, sql`, `),
639
+ having: typeof having === "function" ? having(selected.input) : having
640
+ },
641
+ formatModifiers(query, selected)
642
+ );
643
+ }
644
+ var Union = class _Union extends UnionBase {
645
+ [internalData];
646
+ constructor(data) {
647
+ const compound = data.select;
648
+ const withCompound = { ...data, compound };
649
+ super(withCompound);
650
+ this[internalData] = withCompound;
651
+ }
652
+ get [internalQuery]() {
653
+ return cached(this, internalQuery, () => unionQuery(getData(this)));
654
+ }
655
+ get [internalSelection]() {
656
+ const {
657
+ select: [first]
658
+ } = getData(this);
659
+ return cached(this, internalSelection, () => querySelection(first));
660
+ }
661
+ orderBy(...orderBy) {
662
+ return new _Union({ ...getData(this), orderBy });
663
+ }
664
+ limit(limit) {
665
+ return new _Union({ ...getData(this), limit });
666
+ }
667
+ offset(offset) {
668
+ return new _Union({ ...getData(this), offset });
669
+ }
670
+ };
671
+ function unionQuery(query) {
672
+ const { select } = query;
673
+ let fields;
674
+ const firstSelect = (query2) => isUnionQuery(query2) ? query2.select[0] : query2;
675
+ const segmentQuery = (query2) => {
676
+ if (isUnionQuery(query2)) {
677
+ const nested = unionQuery(query2);
678
+ return sql.universal({
679
+ sqlite: sql`select * from (${nested})`,
680
+ default: sql`(${nested})`
681
+ });
682
+ }
683
+ const inner = selectQuery(query2);
684
+ return query2.orderBy || query2.limit !== void 0 || query2.offset !== void 0 ? sql`(${inner})` : inner;
685
+ };
686
+ const segments = sql.join(
687
+ select.map((segment, i) => {
688
+ if (i === 0) {
689
+ fields = querySelection(segment).fieldNames();
690
+ return segmentQuery(segment);
691
+ }
692
+ const op = Object.keys(segment)[0];
693
+ const query2 = segment[op];
694
+ const names = querySelection(firstSelect(query2)).fieldNames();
695
+ if (fields.length !== names.length)
696
+ throw new Error("Union segments must have the same fields");
697
+ for (let i2 = 0; i2 < fields.length; i2++)
698
+ if (fields[i2] !== names[i2])
699
+ throw new Error("Union segments must have the same fields");
700
+ return sql.query({ [op]: segmentQuery(query2) });
701
+ })
702
+ );
703
+ return sql.query(formatCTE(query), segments, formatModifiers(query));
704
+ }
705
+
706
+ // node_modules/rado/dist/core/query/Insert.js
707
+ var Insert = class extends MutationQuery {
708
+ [internalData];
709
+ constructor(data) {
710
+ super(data);
711
+ this[internalData] = data;
712
+ if (data.returning) this[internalSelection] = selection(data.returning);
713
+ }
714
+ get [internalQuery]() {
715
+ return cached(
716
+ this,
717
+ internalQuery,
718
+ () => insertQuery(getData(this))
719
+ );
720
+ }
721
+ };
722
+ var InsertCanReturn = class extends Insert {
723
+ returning(returning) {
724
+ const data = getData(this);
725
+ return new Insert({
726
+ ...data,
727
+ returning: returning ?? data.insert
728
+ });
729
+ }
730
+ };
731
+ var InsertCanConflict = class _InsertCanConflict extends InsertCanReturn {
732
+ onConflictDoNothing(onConflictDoNothing) {
733
+ const { on = [], ...data } = getData(this);
734
+ return new _InsertCanConflict({
735
+ ...data,
736
+ on: [...on, { conflictDoNothing: onConflictDoNothing ?? true }]
737
+ });
738
+ }
739
+ onConflictDoUpdate(onConflict) {
740
+ const { on = [], ...data } = getData(this);
741
+ return new _InsertCanConflict({
742
+ ...data,
743
+ on: [...on, { conflictDoUpdate: onConflict }]
744
+ });
745
+ }
746
+ onDuplicateKeyUpdate(onDuplicateKeyUpdate) {
747
+ const { on = [], ...data } = getData(this);
748
+ return new _InsertCanConflict({
749
+ ...data,
750
+ on: [...on, { duplicateKeyUpdate: onDuplicateKeyUpdate }]
751
+ });
752
+ }
753
+ };
754
+ var InsertInto = class _InsertInto {
755
+ [internalData];
756
+ constructor(data) {
757
+ this[internalData] = data;
758
+ }
759
+ ignore() {
760
+ return new _InsertInto({ ...getData(this), ignore: true });
761
+ }
762
+ overridingSystemValue() {
763
+ return new _InsertInto({
764
+ ...getData(this),
765
+ overridingSystemValue: true
766
+ });
767
+ }
768
+ values(values) {
769
+ return new InsertCanConflict({
770
+ ...getData(this),
771
+ values
772
+ });
773
+ }
774
+ select(query) {
775
+ const insert = getTable(getData(this).insert);
776
+ const expected = Object.keys(insert.columns);
777
+ const actual = querySelection(getData(query)).fieldNames();
778
+ if (expected.length !== actual.length)
779
+ throw new Error("Insert select fields must match table columns");
780
+ for (let i = 0; i < expected.length; i++) {
781
+ if (expected[i] !== actual[i])
782
+ throw new Error("Insert select fields must match table columns");
783
+ }
784
+ return new InsertCanConflict({
785
+ ...getData(this),
786
+ ...getData(query)
787
+ });
788
+ }
789
+ };
790
+ function formatDefaultValue(value) {
791
+ return sql.universal({
792
+ sqlite: value,
793
+ default: sql`default`
794
+ });
795
+ }
796
+ var defaultKeyword = formatDefaultValue(sql`null`);
797
+ function formatValues(table, rows) {
798
+ return sql.join(
799
+ rows.map((row) => {
800
+ return sql`(${sql.join(
801
+ Object.entries(table.columns).map(([key, column]) => {
802
+ const expr = row[key];
803
+ const columnApi = getData(column);
804
+ const { $onUpdate, $default, defaultValue } = columnApi;
805
+ if (expr !== void 0) return mapToColumn(columnApi, expr);
806
+ if ($default) return $default();
807
+ if (defaultValue) return formatDefaultValue(defaultValue);
808
+ if ($onUpdate) return $onUpdate();
809
+ return defaultKeyword;
810
+ }),
811
+ sql`, `
812
+ )})`;
813
+ }),
814
+ sql`, `
815
+ );
816
+ }
817
+ function formatUpdates(update) {
818
+ return sql.join(
819
+ Object.entries(update).map(
820
+ ([key, value]) => sql`${sql.identifier(key)} = ${input(value)}`
821
+ ),
822
+ sql`, `
823
+ );
824
+ }
825
+ function formatConflict({
826
+ target,
827
+ targetWhere,
828
+ set,
829
+ where: setWhere
830
+ }) {
831
+ const update = set && formatUpdates(set);
832
+ return sql.query({
833
+ onConflict: sql.join([
834
+ target && sql`(${Array.isArray(target) ? sql.join(target, sql`, `) : target})`,
835
+ targetWhere && sql`where ${targetWhere}`,
836
+ update ? sql.join([
837
+ sql`do update set ${update}`,
838
+ setWhere && sql`where ${setWhere}`
839
+ ]) : sql`do nothing`
840
+ ])
841
+ });
842
+ }
843
+ function formatConflicts(on) {
844
+ return sql.join(
845
+ on.map((conflict) => {
846
+ if ("duplicateKeyUpdate" in conflict)
847
+ return sql.query({
848
+ onDuplicateKeyUpdate: formatUpdates(conflict.duplicateKeyUpdate.set)
849
+ });
850
+ if ("conflictDoUpdate" in conflict)
851
+ return formatConflict(conflict.conflictDoUpdate);
852
+ if ("conflictDoNothing" in conflict)
853
+ return formatConflict(
854
+ conflict.conflictDoNothing === true ? {} : conflict.conflictDoNothing
855
+ );
856
+ throw new Error("Unknown conflict type");
857
+ })
858
+ );
859
+ }
860
+ function insertQuery(query) {
861
+ const { insert, values, select, returning, overridingSystemValue, ignore } = query;
862
+ if (!values && !select) throw new Error("No values defined");
863
+ const table = getTable(insert);
864
+ const toInsert = values ? sql.query({
865
+ values: formatValues(table, Array.isArray(values) ? values : [values])
866
+ }) : selectQuery(query);
867
+ const conflicts = query.on ? formatConflicts(query.on) : void 0;
868
+ return sql.query(
869
+ formatCTE(query),
870
+ {
871
+ "": sql.universal({
872
+ mysql: ignore ? sql`insert ignore into` : sql`insert into`,
873
+ default: sql`insert into`
874
+ })
875
+ },
876
+ sql`${table.identifier()} (${table.listColumns()})`,
877
+ { overridingSystemValue },
878
+ toInsert,
879
+ conflicts,
880
+ returning && sql.query({ returning: selection(returning) })
881
+ ).inlineFields(false);
882
+ }
883
+
884
+ // node_modules/rado/dist/core/query/Update.js
885
+ var Update = class _Update extends MutationQuery {
886
+ [internalData];
887
+ constructor(data) {
888
+ super(data);
889
+ this[internalData] = data;
890
+ if (data.returning) this[internalSelection] = selection(data.returning);
891
+ }
892
+ get [internalQuery]() {
893
+ return cached(
894
+ this,
895
+ internalQuery,
896
+ () => updateQuery(getData(this))
897
+ );
898
+ }
899
+ limit(limit) {
900
+ return new _Update({ ...getData(this), limit });
901
+ }
902
+ offset(offset) {
903
+ return new _Update({ ...getData(this), offset });
904
+ }
905
+ orderBy(...orderBy) {
906
+ return new _Update({ ...getData(this), orderBy });
907
+ }
908
+ };
909
+ var UpdateTable = class _UpdateTable extends Update {
910
+ set(set) {
911
+ const data = getData(this);
912
+ const hasDefinedValue = Object.values(set).some(
913
+ (value) => value !== void 0
914
+ );
915
+ if (!hasDefinedValue) {
916
+ const hasOnUpdate = Object.values(getTable(data.update).columns).some(
917
+ (column) => !!getData(column).$onUpdate
918
+ );
919
+ if (!hasOnUpdate) throw new Error("No values to set");
920
+ }
921
+ return new _UpdateTable({ ...data, set });
922
+ }
923
+ from(from) {
924
+ return new _UpdateTable({ ...getData(this), from });
925
+ }
926
+ where(...where) {
927
+ return new _UpdateTable({
928
+ ...getData(this),
929
+ where: and(...where)
930
+ });
931
+ }
932
+ returning(returning) {
933
+ const data = getData(this);
934
+ return new Update({
935
+ ...data,
936
+ returning: returning ?? data.update
937
+ });
938
+ }
939
+ };
940
+ function formatUpdateFrom(target, set, from) {
941
+ if (!from) return sql`update ${target} set ${set}`;
942
+ const source = formatFrom(from);
943
+ return sql.universal({
944
+ mysql: sql`update ${target} join ${source} set ${set}`,
945
+ default: sql`update ${target} set ${set} from ${source}`
946
+ });
947
+ }
948
+ function updateQuery(query) {
949
+ const { update: table, set: values, from, where, returning } = query;
950
+ const tableApi = getTable(table);
951
+ if (!values) throw new Error("Update values are required");
952
+ const assignments = Object.entries(tableApi.columns).flatMap(
953
+ ([key, column]) => {
954
+ const columnApi = getData(column);
955
+ const { name, $onUpdate } = columnApi;
956
+ const value = values[key];
957
+ let expr;
958
+ if (value === void 0) {
959
+ if ($onUpdate) expr = $onUpdate();
960
+ else return [];
961
+ } else {
962
+ expr = mapToColumn(columnApi, value);
963
+ }
964
+ const fieldName = name ?? key;
965
+ const field = sql.identifier(fieldName);
966
+ const assignment = from ? sql.universal({
967
+ mysql: sql`${sql.identifier(tableApi.aliased)}.${field}`,
968
+ default: field
969
+ }) : field;
970
+ return [sql`${assignment} = ${expr}`];
971
+ }
972
+ );
973
+ if (assignments.length === 0) throw new Error("No values to set");
974
+ const set = sql.join(assignments, sql`, `);
975
+ const target = tableApi.target();
976
+ const selected = returning && getSql(selection(returning));
977
+ const returningSelection = selected && from ? sql.universal({
978
+ postgres: selected,
979
+ default: selected.inlineFields(false)
980
+ }) : selected?.inlineFields(false);
981
+ return sql.query(
982
+ formatCTE(query),
983
+ formatUpdateFrom(target, set, from),
984
+ { where, returning: returningSelection },
985
+ formatModifiers(query)
986
+ );
987
+ }
988
+
989
+ // node_modules/rado/dist/core/Builder.js
990
+ function aliasSql(input2, name) {
991
+ const expr = getSql(input2);
992
+ if (expr.alias) return expr;
993
+ const aliased = new Sql((emitter) => expr.emit(emitter));
994
+ aliased.alias = name;
995
+ aliased.mapFromDriverValue = expr.mapFromDriverValue;
996
+ return aliased;
997
+ }
998
+ function aliasUnnamedFields(input2, name) {
999
+ if (hasSql(input2)) {
1000
+ if (!name) return input2;
1001
+ return aliasSql(input2, name);
1002
+ }
1003
+ if (!input2 || typeof input2 !== "object") return input2;
1004
+ return Object.fromEntries(
1005
+ Object.entries(input2).map(([key, value]) => [
1006
+ key,
1007
+ aliasUnnamedFields(value, key)
1008
+ ])
1009
+ );
1010
+ }
1011
+ var BuilderBase = class {
1012
+ [internalData];
1013
+ constructor(data = {}) {
1014
+ this[internalData] = data;
1015
+ }
1016
+ $query(query) {
1017
+ const data = { ...getData(this), ...query };
1018
+ if ("delete" in query) return new Delete(data);
1019
+ if ("insert" in query) return new Insert(data);
1020
+ if ("update" in query) return new Update(data);
1021
+ return new Select(data);
1022
+ }
1023
+ select(select) {
1024
+ return new Select({
1025
+ ...getData(this),
1026
+ select
1027
+ });
1028
+ }
1029
+ selectDistinct(select) {
1030
+ return new Select({
1031
+ ...getData(this),
1032
+ select,
1033
+ distinct: true
1034
+ });
1035
+ }
1036
+ selectDistinctOn(columns, select) {
1037
+ return new Select({
1038
+ ...getData(this),
1039
+ select,
1040
+ distinctOn: columns
1041
+ });
1042
+ }
1043
+ update(table) {
1044
+ return new UpdateTable({ ...getData(this), update: table });
1045
+ }
1046
+ insert(into) {
1047
+ return new InsertInto({ ...getData(this), insert: into });
1048
+ }
1049
+ delete(from) {
1050
+ return new DeleteFrom({ ...getData(this), delete: from });
1051
+ }
1052
+ };
1053
+ var Builder = class extends BuilderBase {
1054
+ $with(cteName, columns) {
1055
+ return {
1056
+ as(query) {
1057
+ let input2 = hasQuery(query) ? hasSelection(query) ? getSelection(query).input : columns : columns;
1058
+ const data = hasQuery(query) && hasData(query) ? getData(query) : void 0;
1059
+ if (input2 && data?.compound?.length > 1)
1060
+ input2 = aliasUnnamedFields(input2);
1061
+ const target = virtualTarget(cteName, input2 ?? {});
1062
+ return {
1063
+ ...target,
1064
+ [internalSelection]: new StarSelection(target),
1065
+ get [internalQuery]() {
1066
+ return hasQuery(query) ? getQuery(query).nameSelf(cteName) : getSql(query);
1067
+ }
1068
+ };
1069
+ }
1070
+ };
1071
+ }
1072
+ with(...definitions) {
1073
+ return new BuilderBase({ ...getData(this), with: definitions });
1074
+ }
1075
+ withRecursive(...definitions) {
1076
+ return new BuilderBase({ ...getData(this), withRecursive: definitions });
1077
+ }
1078
+ };
1079
+
1080
+ export {
1081
+ BatchQuery,
1082
+ querySelection,
1083
+ selectQuery,
1084
+ Builder
1085
+ };