@constructive-io/schema-builder 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +12 -4
  3. package/dist/card-widths-CmIxkRfh.cjs +28 -0
  4. package/dist/card-widths-CmIxkRfh.cjs.map +1 -0
  5. package/dist/card-widths-pI4Kw4Xz.js +22 -0
  6. package/dist/card-widths-pI4Kw4Xz.js.map +1 -0
  7. package/dist/core-CVxnNLs7.cjs +66 -0
  8. package/dist/core-CVxnNLs7.cjs.map +1 -0
  9. package/dist/core-DJ5kA4r-.js +49 -0
  10. package/dist/core-DJ5kA4r-.js.map +1 -0
  11. package/dist/core.cjs +14 -212
  12. package/dist/core.d.cts +3 -54
  13. package/dist/core.d.ts +3 -54
  14. package/dist/core.js +3 -200
  15. package/dist/create-table-card-B6SMrt_g.js +7691 -0
  16. package/dist/create-table-card-B6SMrt_g.js.map +1 -0
  17. package/dist/create-table-card-BrQ7pB_U.cjs +7732 -0
  18. package/dist/create-table-card-BrQ7pB_U.cjs.map +1 -0
  19. package/dist/delete-confirm-dialog-Ch4tWj6B.cjs +42 -0
  20. package/dist/delete-confirm-dialog-Ch4tWj6B.cjs.map +1 -0
  21. package/dist/delete-confirm-dialog-K0AWE643.js +37 -0
  22. package/dist/delete-confirm-dialog-K0AWE643.js.map +1 -0
  23. package/dist/empty-state-card-5t7LygTB.cjs +62 -0
  24. package/dist/empty-state-card-5t7LygTB.cjs.map +1 -0
  25. package/dist/empty-state-card-C-Nylatg.js +57 -0
  26. package/dist/empty-state-card-C-Nylatg.js.map +1 -0
  27. package/dist/fields.cjs +8 -7763
  28. package/dist/fields.d.cts +38 -39
  29. package/dist/fields.d.ts +38 -39
  30. package/dist/fields.js +2 -7735
  31. package/dist/generated-types-DZMUyNBH.d.cts +27 -0
  32. package/dist/generated-types-DZMUyNBH.d.ts +27 -0
  33. package/dist/index--9KTz1LB.d.cts +56 -0
  34. package/dist/index-10idAfdb.d.cts +7 -0
  35. package/dist/index-BcdFaKFS.d.ts +7 -0
  36. package/dist/index-euaSKMQK.d.ts +56 -0
  37. package/dist/index.cjs +392 -13730
  38. package/dist/index.cjs.map +1 -1
  39. package/dist/index.d.cts +4 -10
  40. package/dist/index.d.ts +4 -10
  41. package/dist/index.js +381 -13698
  42. package/dist/index.js.map +1 -1
  43. package/dist/indexes-view-CBzYpKcd.js +736 -0
  44. package/dist/indexes-view-CBzYpKcd.js.map +1 -0
  45. package/dist/indexes-view-D1K24ts1.cjs +759 -0
  46. package/dist/indexes-view-D1K24ts1.cjs.map +1 -0
  47. package/dist/indexes.cjs +6 -1041
  48. package/dist/indexes.d.cts +27 -27
  49. package/dist/indexes.d.ts +27 -27
  50. package/dist/indexes.js +2 -1016
  51. package/dist/invalidate-database-entities-BFCWf5Zf.js +2561 -0
  52. package/dist/invalidate-database-entities-BFCWf5Zf.js.map +1 -0
  53. package/dist/invalidate-database-entities-Gs5k8A-M.cjs +2864 -0
  54. package/dist/invalidate-database-entities-Gs5k8A-M.cjs.map +1 -0
  55. package/dist/keyboard-activation-BQiV0scm.js +13 -0
  56. package/dist/keyboard-activation-BQiV0scm.js.map +1 -0
  57. package/dist/keyboard-activation-D-L0IU6Y.cjs +19 -0
  58. package/dist/keyboard-activation-D-L0IU6Y.cjs.map +1 -0
  59. package/dist/policies-view-CkvGpJBo.cjs +1023 -0
  60. package/dist/policies-view-CkvGpJBo.cjs.map +1 -0
  61. package/dist/policies-view-DTfAhX7G.js +1018 -0
  62. package/dist/policies-view-DTfAhX7G.js.map +1 -0
  63. package/dist/policies.cjs +10 -5939
  64. package/dist/policies.d.cts +55 -50
  65. package/dist/policies.d.ts +55 -50
  66. package/dist/policies.js +4 -5911
  67. package/dist/policy-config-form-EEcQ9ywL.js +2502 -0
  68. package/dist/policy-config-form-EEcQ9ywL.js.map +1 -0
  69. package/dist/policy-config-form-RP9010zj.cjs +2629 -0
  70. package/dist/policy-config-form-RP9010zj.cjs.map +1 -0
  71. package/dist/policy-types-D_sYWb-b.d.cts +81 -0
  72. package/dist/policy-types-D_sYWb-b.d.ts +81 -0
  73. package/dist/query-BGtzUV7Y.cjs +233 -0
  74. package/dist/query-BGtzUV7Y.cjs.map +1 -0
  75. package/dist/query-r6j8IjQB.js +192 -0
  76. package/dist/query-r6j8IjQB.js.map +1 -0
  77. package/dist/relationships-view-C884EvGj.js +2417 -0
  78. package/dist/relationships-view-C884EvGj.js.map +1 -0
  79. package/dist/relationships-view-DlBs-gCF.cjs +2452 -0
  80. package/dist/relationships-view-DlBs-gCF.cjs.map +1 -0
  81. package/dist/relationships.cjs +9 -4328
  82. package/dist/relationships.d.cts +92 -92
  83. package/dist/relationships.d.ts +92 -92
  84. package/dist/relationships.js +2 -4300
  85. package/dist/rolldown-runtime-D_qnwXdH.cjs +35 -0
  86. package/dist/schema-builder-sidebar-DAgK7ZTu.js +654 -0
  87. package/dist/schema-builder-sidebar-DAgK7ZTu.js.map +1 -0
  88. package/dist/schema-builder-sidebar-L3TcQeQr.cjs +671 -0
  89. package/dist/schema-builder-sidebar-L3TcQeQr.cjs.map +1 -0
  90. package/dist/table-editor-B-5i1M79.js +1906 -0
  91. package/dist/table-editor-B-5i1M79.js.map +1 -0
  92. package/dist/table-editor-BdLdVZfh.cjs +1935 -0
  93. package/dist/table-editor-BdLdVZfh.cjs.map +1 -0
  94. package/dist/tables.cjs +6 -5643
  95. package/dist/tables.d.cts +22 -21
  96. package/dist/tables.d.ts +22 -21
  97. package/dist/tables.js +3 -5618
  98. package/dist/testing.cjs +63 -62
  99. package/dist/testing.cjs.map +1 -1
  100. package/dist/testing.d.cts +4 -4
  101. package/dist/testing.d.ts +4 -4
  102. package/dist/testing.js +62 -61
  103. package/dist/testing.js.map +1 -1
  104. package/dist/types-CXNRA22N.d.cts +450 -0
  105. package/dist/types-CXNRA22N.d.ts +450 -0
  106. package/dist/use-update-table-DlLX9fdE.js +244 -0
  107. package/dist/use-update-table-DlLX9fdE.js.map +1 -0
  108. package/dist/use-update-table-vcoHVolI.cjs +291 -0
  109. package/dist/use-update-table-vcoHVolI.cjs.map +1 -0
  110. package/package.json +8 -6
  111. package/dist/core.cjs.map +0 -1
  112. package/dist/core.js.map +0 -1
  113. package/dist/fields.cjs.map +0 -1
  114. package/dist/fields.js.map +0 -1
  115. package/dist/generated-types-CnAlqHU_.d.cts +0 -25
  116. package/dist/generated-types-CnAlqHU_.d.ts +0 -25
  117. package/dist/indexes.cjs.map +0 -1
  118. package/dist/indexes.js.map +0 -1
  119. package/dist/policies.cjs.map +0 -1
  120. package/dist/policies.js.map +0 -1
  121. package/dist/policy-types-CEYAR5TN.d.cts +0 -79
  122. package/dist/policy-types-CEYAR5TN.d.ts +0 -79
  123. package/dist/relationships.cjs.map +0 -1
  124. package/dist/relationships.js.map +0 -1
  125. package/dist/tables.cjs.map +0 -1
  126. package/dist/tables.js.map +0 -1
  127. package/dist/types-Du86atZ8.d.cts +0 -199
  128. package/dist/types-Du86atZ8.d.ts +0 -199
  129. package/dist/types-bX2dCrdl.d.cts +0 -52
  130. package/dist/types-bX2dCrdl.d.ts +0 -52
@@ -0,0 +1,2417 @@
1
+ 'use client';
2
+ import { i as useSchemaBuilder } from "./query-r6j8IjQB.js";
3
+ import { C as useCreateUniqueConstraintMutation, G as ForeignKeyActionLabels, J as RelationshipTypes, K as ForeignKeyActions, N as useUpdateForeignKeyConstraintMutation, S as useCreateTableMutation, T as useDeleteForeignKeyConstraintMutation, Y as cn, _ as useCreateFieldMutation, b as useCreatePrimaryKeyConstraintMutation, d as toFieldDefault, f as toFieldType, h as useSchemaBuilderRuntime, i as useCreateRelationProvisionMutation, l as useSchemaBuilderSelectors, o as asFieldIds, r as useDatabasePolicies, t as invalidateDatabaseEntities, v as useCreateForeignKeyConstraintMutation } from "./invalidate-database-entities-BFCWf5Zf.js";
4
+ import { c as buildGrants, h as getDataNodeForPolicy, l as buildPolicyEntry, m as buildNodeData, r as getDefaultFormValues, s as usePolicyTypes, t as PolicyConfigForm, x as useCreateTableGrant, y as sanitizePolicyData } from "./policy-config-form-EEcQ9ywL.js";
5
+ import { t as handleActivationKeyDown } from "./keyboard-activation-BQiV0scm.js";
6
+ import { t as EmptyStateCard } from "./empty-state-card-C-Nylatg.js";
7
+ import { t as DeleteConfirmDialog } from "./delete-confirm-dialog-K0AWE643.js";
8
+ import { useCardStack } from "@constructive-io/ui/stack";
9
+ import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
10
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
11
+ import { Check, ChevronRight, CornerDownRight, Info, Link2, Loader2, Share2, Shield, Table2, Target, Trash2, Waypoints, Zap } from "lucide-react";
12
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
13
+ import { Button } from "@constructive-io/ui/button";
14
+ import { toast } from "@constructive-io/ui/toast";
15
+ import { Combobox, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList, ComboboxPopup } from "@constructive-io/ui/combobox";
16
+ import { Input } from "@constructive-io/ui/input";
17
+ import { Label } from "@constructive-io/ui/label";
18
+ import { ScrollArea } from "@constructive-io/ui/scroll-area";
19
+ import { ResponsiveDiagram } from "@constructive-io/ui/responsive-diagram";
20
+ import { RelationBelongsTo, RelationHasMany, RelationHasOne, RelationManyToMany } from "node-type-registry";
21
+ import { Select, SelectContent, SelectItem, SelectTrigger } from "@constructive-io/ui/select";
22
+ import { Switch } from "@constructive-io/ui/switch";
23
+
24
+ //#region src/schema/schema-builder-core/components/policies/policy-diagram/svg-icons.ts
25
+ const DATABASE_ICON = { elements: [
26
+ {
27
+ tag: "ellipse",
28
+ attrs: {
29
+ cx: 12,
30
+ cy: 5,
31
+ rx: 9,
32
+ ry: 3
33
+ }
34
+ },
35
+ {
36
+ tag: "path",
37
+ attrs: { d: "M3 5V19A9 3 0 0 0 21 19V5" }
38
+ },
39
+ {
40
+ tag: "path",
41
+ attrs: { d: "M3 12A9 3 0 0 0 21 12" }
42
+ }
43
+ ] };
44
+ const TABLE2_ICON = { elements: [{
45
+ tag: "path",
46
+ attrs: { d: "M9 3H5a2 2 0 0 0-2 2v4m6-6h10a2 2 0 0 1 2 2v4M9 3v18m0 0h10a2 2 0 0 0 2-2V9M9 21H5a2 2 0 0 1-2-2V9m0 0h18" }
47
+ }] };
48
+
49
+ //#endregion
50
+ //#region src/schema/schema-builder-core/components/policies/policy-diagram/svg-primitives.tsx
51
+ const MD_RADIUS = 28;
52
+ const SM_RADIUS = 24;
53
+ const MD_ICON_SIZE = 24;
54
+ const SM_ICON_SIZE = 20;
55
+ const FONT_FAMILY = "'Inter', system-ui, sans-serif";
56
+ const LABEL_FONT_SIZE = 12;
57
+ const TOP_LABEL_FONT_SIZE = 10;
58
+ const NODE_TOTAL_HEIGHT = 76;
59
+ const CENTER_Y = 28;
60
+ const LABEL_Y = 76 - 6;
61
+ const DiagramModeCtx = createContext("light");
62
+ const DiagramModeProvider = DiagramModeCtx.Provider;
63
+ function SvgIcon({ x, y, size, color, icon }) {
64
+ const scale = size / 24;
65
+ return /* @__PURE__ */ jsx("g", {
66
+ transform: `translate(${x - size / 2},${y - size / 2}) scale(${scale})`,
67
+ fill: "none",
68
+ stroke: color,
69
+ strokeWidth: "2",
70
+ strokeLinecap: "round",
71
+ strokeLinejoin: "round",
72
+ children: icon.elements.map((el, i) => {
73
+ if (el.tag === "path") return /* @__PURE__ */ jsx("path", { d: el.attrs.d }, i);
74
+ if (el.tag === "circle") return /* @__PURE__ */ jsx("circle", {
75
+ cx: el.attrs.cx,
76
+ cy: el.attrs.cy,
77
+ r: el.attrs.r
78
+ }, i);
79
+ if (el.tag === "ellipse") return /* @__PURE__ */ jsx("ellipse", {
80
+ cx: el.attrs.cx,
81
+ cy: el.attrs.cy,
82
+ rx: el.attrs.rx,
83
+ ry: el.attrs.ry
84
+ }, i);
85
+ return null;
86
+ })
87
+ });
88
+ }
89
+ function SvgDiagramNode({ x, icon, label, theme, size = "md", stacked, outlined }) {
90
+ const r = size === "sm" ? 24 : 28;
91
+ const iconSize = size === "sm" ? SM_ICON_SIZE : MD_ICON_SIZE;
92
+ const cy = 28;
93
+ const bg = outlined ? theme.bg : theme.fill;
94
+ return /* @__PURE__ */ jsxs("g", { children: [
95
+ stacked && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("circle", {
96
+ cx: x - 8,
97
+ cy: cy - 6,
98
+ r,
99
+ fill: theme.bg,
100
+ stroke: theme.border,
101
+ strokeWidth: "2"
102
+ }), /* @__PURE__ */ jsx("circle", {
103
+ cx: x - 4,
104
+ cy: cy - 3,
105
+ r,
106
+ fill: theme.bg,
107
+ stroke: theme.border,
108
+ strokeWidth: "2"
109
+ })] }),
110
+ /* @__PURE__ */ jsx("circle", {
111
+ cx: x,
112
+ cy,
113
+ r,
114
+ fill: bg,
115
+ stroke: theme.border,
116
+ strokeWidth: "2"
117
+ }),
118
+ /* @__PURE__ */ jsx(SvgIcon, {
119
+ x,
120
+ y: cy,
121
+ size: iconSize,
122
+ color: theme.primary,
123
+ icon
124
+ }),
125
+ /* @__PURE__ */ jsx("text", {
126
+ x,
127
+ y: 70,
128
+ textAnchor: "middle",
129
+ fontSize: LABEL_FONT_SIZE,
130
+ fill: theme.text,
131
+ style: { fontFamily: FONT_FAMILY },
132
+ children: label
133
+ })
134
+ ] });
135
+ }
136
+ /** Width of a diagram node (including stacked offset) */
137
+ function nodeWidth(size = "md", stacked) {
138
+ const diameter = (size === "sm" ? 24 : 28) * 2;
139
+ return stacked ? diameter + 8 : diameter;
140
+ }
141
+ function SvgConnector({ x, width, theme, topLabel, topLabelFilled, dashed }) {
142
+ const cy = 28;
143
+ const dotR = 4;
144
+ return /* @__PURE__ */ jsxs("g", { children: [
145
+ /* @__PURE__ */ jsx("circle", {
146
+ cx: x + 6,
147
+ cy,
148
+ r: dotR,
149
+ fill: theme.connector
150
+ }),
151
+ /* @__PURE__ */ jsx("line", {
152
+ x1: x + 10,
153
+ y1: cy,
154
+ x2: x + width - 10,
155
+ y2: cy,
156
+ stroke: theme.connectorLight,
157
+ strokeWidth: "2.5",
158
+ strokeLinecap: "round",
159
+ ...dashed ? { strokeDasharray: "4 3" } : {}
160
+ }),
161
+ /* @__PURE__ */ jsx("circle", {
162
+ cx: x + width - 6,
163
+ cy,
164
+ r: dotR,
165
+ fill: theme.connector
166
+ }),
167
+ topLabel && /* @__PURE__ */ jsx(SvgTopLabel, {
168
+ cx: x + width / 2,
169
+ y: 4,
170
+ label: topLabel,
171
+ filled: topLabelFilled,
172
+ theme
173
+ })
174
+ ] });
175
+ }
176
+ function SvgFanConnector({ x, theme, topLabel, topLabelFilled }) {
177
+ const flatWidth = topLabel ? Math.max(24, topLabelBoxWidth(topLabel) + 4) : 24;
178
+ const fanMidX = x + flatWidth + 4 + 3;
179
+ const fanTipX = x + (flatWidth + 36) - 6;
180
+ const cy = 28;
181
+ const fanSpread = 14;
182
+ const startDotCx = x + 6;
183
+ const endDots = [
184
+ {
185
+ cx: fanTipX,
186
+ cy: cy - fanSpread
187
+ },
188
+ {
189
+ cx: fanTipX,
190
+ cy
191
+ },
192
+ {
193
+ cx: fanTipX,
194
+ cy: 42
195
+ }
196
+ ];
197
+ return /* @__PURE__ */ jsxs("g", { children: [
198
+ /* @__PURE__ */ jsx("line", {
199
+ x1: startDotCx,
200
+ y1: cy,
201
+ x2: fanMidX,
202
+ y2: cy,
203
+ stroke: theme.connectorLight,
204
+ strokeWidth: "2.5",
205
+ strokeLinecap: "round"
206
+ }),
207
+ endDots.map((dot, i) => /* @__PURE__ */ jsx("line", {
208
+ x1: fanMidX,
209
+ y1: cy,
210
+ x2: dot.cx,
211
+ y2: dot.cy,
212
+ stroke: theme.connector,
213
+ strokeWidth: "2",
214
+ strokeLinecap: "round"
215
+ }, i)),
216
+ /* @__PURE__ */ jsx("circle", {
217
+ cx: startDotCx,
218
+ cy,
219
+ r: 4,
220
+ fill: theme.connector
221
+ }),
222
+ /* @__PURE__ */ jsx("circle", {
223
+ cx: fanMidX,
224
+ cy,
225
+ r: 3,
226
+ fill: theme.connector
227
+ }),
228
+ endDots.map((dot, i) => /* @__PURE__ */ jsx("circle", {
229
+ cx: dot.cx,
230
+ cy: dot.cy,
231
+ r: 3,
232
+ fill: theme.connector
233
+ }, i)),
234
+ topLabel && /* @__PURE__ */ jsx(SvgTopLabel, {
235
+ cx: x + (flatWidth + 6) / 2,
236
+ y: 4,
237
+ label: topLabel,
238
+ filled: topLabelFilled,
239
+ theme
240
+ })
241
+ ] });
242
+ }
243
+ /** Width of a fan connector */
244
+ function fanConnectorWidth(topLabel) {
245
+ return (topLabel ? Math.max(24, topLabelBoxWidth(topLabel) + 4) : 24) + 36;
246
+ }
247
+ /** Compute connector width that fits the top label with padding from dots */
248
+ function connectorWidthForLabel(label, minWidth = 40) {
249
+ if (!label) return minWidth;
250
+ const boxW = topLabelBoxWidth(label);
251
+ return Math.max(minWidth, boxW + 16);
252
+ }
253
+ const TOP_LABEL_MAX_CHARS = 12;
254
+ const TOP_LABEL_CHAR_WIDTH = 5.5;
255
+ const TOP_LABEL_PAD_X = 6;
256
+ /** Truncate a label string for display, adding ellipsis if needed */
257
+ function truncateLabel(label, maxChars = TOP_LABEL_MAX_CHARS) {
258
+ return label.length > maxChars ? label.slice(0, maxChars - 1) + "…" : label;
259
+ }
260
+ /** Compute the rendered width of a top-label box (accounts for truncation) */
261
+ function topLabelBoxWidth(label) {
262
+ return truncateLabel(label).length * TOP_LABEL_CHAR_WIDTH + TOP_LABEL_PAD_X * 2;
263
+ }
264
+ function SvgTopLabel({ cx, y, label, filled, theme }) {
265
+ const display = truncateLabel(label);
266
+ const rectW = display.length * TOP_LABEL_CHAR_WIDTH + TOP_LABEL_PAD_X * 2;
267
+ const rectH = 16;
268
+ return /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("rect", {
269
+ x: cx - rectW / 2,
270
+ y,
271
+ width: rectW,
272
+ height: rectH,
273
+ rx: 3,
274
+ fill: filled ? theme.fill : theme.bg,
275
+ stroke: filled ? theme.primary : theme.border,
276
+ strokeWidth: "1",
277
+ ...!filled ? { strokeDasharray: "3 2" } : {}
278
+ }), /* @__PURE__ */ jsx("text", {
279
+ x: cx,
280
+ y: y + rectH / 2 + 10 * .35,
281
+ textAnchor: "middle",
282
+ fontSize: 10,
283
+ fontWeight: "500",
284
+ fill: theme.primary,
285
+ style: { fontFamily: FONT_FAMILY },
286
+ children: display
287
+ })] });
288
+ }
289
+
290
+ //#endregion
291
+ //#region src/schema/schema-builder-relationships/lib/gql/hooks/schema-builder/use-relation-provision.ts
292
+ /**
293
+ * Hook for atomic relationship creation via relation_provision
294
+ * Tier 4 wrapper: single mutation replaces multi-step FK/M2M creation
295
+ */
296
+ const RELATION_TYPE_MAP = {
297
+ "one-to-one": RelationHasOne.name,
298
+ "belongs-to": RelationBelongsTo.name,
299
+ "one-to-many": RelationHasMany.name,
300
+ "many-to-many": RelationManyToMany.name
301
+ };
302
+ function useRelationProvision(options) {
303
+ const queryClient = useQueryClient();
304
+ const { selectTable, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
305
+ const createProvisionMutation = useCreateRelationProvisionMutation({ selection: { fields: {
306
+ id: true,
307
+ outFieldId: true,
308
+ outJunctionTableId: true,
309
+ outSourceFieldId: true,
310
+ outTargetFieldId: true
311
+ } } });
312
+ return useMutation({
313
+ mutationFn: async (input) => {
314
+ const databaseId = currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId;
315
+ if (!databaseId) throw new Error("Database ID not found");
316
+ const relationType = RELATION_TYPE_MAP[input.relationshipType];
317
+ if (!relationType) throw new Error(`Unsupported relationship type: ${input.relationshipType}`);
318
+ const isManyToMany = input.relationshipType === "many-to-many";
319
+ let derivedNodes;
320
+ let resolvedPolicyData = input.policyData ?? {};
321
+ if (isManyToMany) {
322
+ const dataNode = getDataNodeForPolicy(input.policyType || "AuthzAllowAll");
323
+ if (input.policyTypeObj) {
324
+ resolvedPolicyData = {
325
+ ...getDefaultFormValues(input.policyTypeObj),
326
+ ...resolvedPolicyData
327
+ };
328
+ resolvedPolicyData = sanitizePolicyData(resolvedPolicyData);
329
+ }
330
+ const nodes = [];
331
+ if (!input.useCompositeKey) nodes.push({ $type: "DataId" });
332
+ if (dataNode) {
333
+ const baseNodeData = input.policyTypeObj ? buildNodeData(resolvedPolicyData, input.policyTypeObj) : {};
334
+ nodes.push({
335
+ $type: dataNode,
336
+ ...Object.keys(baseNodeData).length > 0 ? { data: baseNodeData } : {}
337
+ });
338
+ }
339
+ if (nodes.length > 0) derivedNodes = nodes;
340
+ }
341
+ const provision = (await createProvisionMutation.mutateAsync({
342
+ databaseId,
343
+ relationType,
344
+ sourceTableId: input.sourceTableId,
345
+ targetTableId: input.targetTableId,
346
+ ...input.deleteAction && { deleteAction: input.deleteAction },
347
+ ...input.fieldName && { fieldName: input.fieldName },
348
+ ...input.isRequired !== void 0 && { isRequired: input.isRequired },
349
+ ...input.junctionTableName && { junctionTableName: input.junctionTableName },
350
+ ...input.useCompositeKey !== void 0 && { useCompositeKey: input.useCompositeKey },
351
+ ...input.sourceFieldName && { sourceFieldName: input.sourceFieldName },
352
+ ...input.targetFieldName && { targetFieldName: input.targetFieldName },
353
+ ...derivedNodes && { nodes: derivedNodes },
354
+ ...input.exposeInApi !== void 0 && { exposeInApi: input.exposeInApi },
355
+ ...isManyToMany && {
356
+ grants: buildGrants(),
357
+ policies: [buildPolicyEntry(input.policyType || "AuthzAllowAll", {
358
+ permissive: true,
359
+ ...Object.keys(resolvedPolicyData).length > 0 && { data: resolvedPolicyData }
360
+ })]
361
+ }
362
+ })).createRelationProvision?.relationProvision;
363
+ if (!provision) throw new Error("Failed to create relation provision");
364
+ return {
365
+ outFieldId: provision.outFieldId ?? null,
366
+ outJunctionTableId: provision.outJunctionTableId ?? null,
367
+ outSourceFieldId: provision.outSourceFieldId ?? null,
368
+ outTargetFieldId: provision.outTargetFieldId ?? null
369
+ };
370
+ },
371
+ onSuccess: async (_result, input) => {
372
+ await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
373
+ if (!options?.skipTableSelect) selectTable(input.sourceTableId);
374
+ },
375
+ onError: (error) => {
376
+ console.error("Failed to provision relation:", error);
377
+ }
378
+ });
379
+ }
380
+
381
+ //#endregion
382
+ //#region src/schema/schema-builder-relationships/lib/gql/hooks/schema-builder/use-relationship-mutations.ts
383
+ /**
384
+ * Hook for managing foreign key relationships
385
+ * Tier 4 wrapper: orchestrates FK + M2M relationship creation
386
+ */
387
+ function useCreateForeignKey() {
388
+ const queryClient = useQueryClient();
389
+ const { selectTable, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
390
+ const createFkMutation = useCreateForeignKeyConstraintMutation({ selection: { fields: {
391
+ id: true,
392
+ name: true
393
+ } } });
394
+ const createUniqueMutation = useCreateUniqueConstraintMutation({ selection: { fields: { id: true } } });
395
+ return useMutation({
396
+ mutationFn: async (input) => {
397
+ const databaseId = currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId;
398
+ if (!databaseId) throw new Error("Database ID not found");
399
+ const refUniqueConstraintName = `${input.refTableId}_${input.refFieldIds.join("_")}_ref_unique`.slice(0, 63);
400
+ try {
401
+ await createUniqueMutation.mutateAsync({
402
+ tableId: input.refTableId,
403
+ databaseId,
404
+ fieldIds: asFieldIds(input.refFieldIds),
405
+ name: refUniqueConstraintName,
406
+ type: "u"
407
+ });
408
+ } catch (error) {
409
+ const errorMessage = error instanceof Error ? error.message : "";
410
+ if (!errorMessage.includes("already exists") && !errorMessage.includes("duplicate")) throw error;
411
+ }
412
+ const createdFk = (await createFkMutation.mutateAsync({
413
+ databaseId,
414
+ tableId: input.tableId,
415
+ fieldIds: asFieldIds(input.fieldIds),
416
+ refTableId: input.refTableId,
417
+ refFieldIds: asFieldIds(input.refFieldIds),
418
+ name: input.name ?? void 0,
419
+ deleteAction: input.deleteAction ?? "a",
420
+ updateAction: input.updateAction ?? "a",
421
+ type: "f",
422
+ smartTags: input.relationshipType ? { relationshipType: input.relationshipType } : void 0
423
+ })).createForeignKeyConstraint?.foreignKeyConstraint;
424
+ if (!createdFk?.id) throw new Error("Failed to create foreign key constraint");
425
+ if (input.relationshipType === RelationshipTypes.ONE_TO_ONE) {
426
+ const uniqueName = (input.name || createdFk.name || "fk").replace(/_fkey\d*$/, "_unique");
427
+ await createUniqueMutation.mutateAsync({
428
+ tableId: input.tableId,
429
+ databaseId,
430
+ fieldIds: asFieldIds(input.fieldIds),
431
+ name: uniqueName,
432
+ type: "u"
433
+ });
434
+ }
435
+ return {
436
+ id: createdFk.id,
437
+ name: createdFk.name ?? ""
438
+ };
439
+ },
440
+ onSuccess: async (_createdForeignKey, variables) => {
441
+ await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
442
+ selectTable(variables.tableId);
443
+ },
444
+ onError: (error) => {
445
+ console.error("Failed to create foreign key constraint:", error);
446
+ }
447
+ });
448
+ }
449
+ function useUpdateForeignKey() {
450
+ const queryClient = useQueryClient();
451
+ const { selectTable, selectedTableId, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
452
+ const updateFkMutation = useUpdateForeignKeyConstraintMutation({ selection: { fields: {
453
+ id: true,
454
+ name: true
455
+ } } });
456
+ return useMutation({
457
+ mutationFn: async (input) => {
458
+ const patch = {};
459
+ if (input.name !== void 0) patch.name = input.name;
460
+ if (input.fieldIds !== void 0) patch.fieldIds = input.fieldIds;
461
+ if (input.refTableId !== void 0) patch.refTableId = input.refTableId;
462
+ if (input.refFieldIds !== void 0) patch.refFieldIds = input.refFieldIds;
463
+ if (input.deleteAction !== void 0) patch.deleteAction = input.deleteAction;
464
+ if (input.updateAction !== void 0) patch.updateAction = input.updateAction;
465
+ const updatedFk = (await updateFkMutation.mutateAsync({
466
+ id: input.id,
467
+ foreignKeyConstraintPatch: patch
468
+ })).updateForeignKeyConstraint?.foreignKeyConstraint;
469
+ if (!updatedFk?.id) throw new Error("Failed to update foreign key constraint");
470
+ return {
471
+ id: updatedFk.id,
472
+ name: updatedFk.name ?? ""
473
+ };
474
+ },
475
+ onSuccess: async () => {
476
+ await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
477
+ if (selectedTableId) selectTable(selectedTableId);
478
+ },
479
+ onError: (error) => {
480
+ console.error("Failed to update foreign key constraint:", error);
481
+ }
482
+ });
483
+ }
484
+ function useDeleteForeignKey() {
485
+ const queryClient = useQueryClient();
486
+ const { selectTable, selectedTableId, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
487
+ const deleteFkMutation = useDeleteForeignKeyConstraintMutation({ selection: { fields: { id: true } } });
488
+ return useMutation({
489
+ mutationFn: async (input) => {
490
+ await deleteFkMutation.mutateAsync({ id: input.id });
491
+ return { id: input.id };
492
+ },
493
+ onSuccess: async () => {
494
+ await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
495
+ if (selectedTableId) selectTable(selectedTableId);
496
+ },
497
+ onError: (error) => {
498
+ console.error("Failed to delete foreign key constraint:", error);
499
+ }
500
+ });
501
+ }
502
+ /**
503
+ * Creates a many-to-many relationship by:
504
+ * 1. Creating a junction table
505
+ * 2. Creating an id field with primary key
506
+ * 3. Creating two FK fields pointing to both related tables
507
+ * 4. Creating two FK constraints
508
+ * 5. Creating a composite unique constraint on (a_id, b_id) to prevent duplicates
509
+ */
510
+ function useCreateManyToMany() {
511
+ const queryClient = useQueryClient();
512
+ const { selectTable, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
513
+ const { mutateAsync: createGrant } = useCreateTableGrant();
514
+ const createTableMutation = useCreateTableMutation({ selection: { fields: {
515
+ id: true,
516
+ name: true
517
+ } } });
518
+ const createFieldMutation = useCreateFieldMutation({ selection: { fields: { id: true } } });
519
+ const createPkMutation = useCreatePrimaryKeyConstraintMutation({ selection: { fields: { id: true } } });
520
+ const createFkMutation = useCreateForeignKeyConstraintMutation({ selection: { fields: { id: true } } });
521
+ const createUniqueMutation = useCreateUniqueConstraintMutation({ selection: { fields: { id: true } } });
522
+ return useMutation({
523
+ mutationFn: async (input) => {
524
+ const databaseId = currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId;
525
+ const schemaId = currentDatabase?.schemaId ?? currentSchema?.metadata?.schemaId;
526
+ if (!databaseId || !schemaId) throw new Error("Database ID or Schema ID not found");
527
+ const deleteAction = input.deleteAction ?? ForeignKeyActions.CASCADE;
528
+ const updateAction = input.updateAction ?? ForeignKeyActions.NO_ACTION;
529
+ const junctionTable = (await createTableMutation.mutateAsync({
530
+ name: input.junctionTableName,
531
+ databaseId,
532
+ schemaId
533
+ })).createTable?.table;
534
+ if (!junctionTable?.id || !junctionTable?.name) throw new Error("Failed to create junction table");
535
+ const junctionTableId = junctionTable.id;
536
+ const junctionTableName = junctionTable.name;
537
+ const idField = (await createFieldMutation.mutateAsync({
538
+ name: "id",
539
+ type: toFieldType("uuid"),
540
+ tableId: junctionTableId,
541
+ databaseId,
542
+ isRequired: true,
543
+ defaultValue: toFieldDefault("uuidv7()")
544
+ })).createField?.field;
545
+ if (!idField?.id) throw new Error("Failed to create id field");
546
+ const idFieldId = idField.id;
547
+ await createPkMutation.mutateAsync({
548
+ tableId: junctionTableId,
549
+ databaseId,
550
+ fieldIds: asFieldIds([idFieldId]),
551
+ name: `${input.junctionTableName}_pkey`,
552
+ type: "p"
553
+ });
554
+ const fieldAName = `${input.tableAName}_id`;
555
+ const fieldBName = `${input.tableBName}_id`;
556
+ const fieldA = (await createFieldMutation.mutateAsync({
557
+ name: fieldAName,
558
+ type: toFieldType("uuid"),
559
+ tableId: junctionTableId,
560
+ databaseId,
561
+ isRequired: true
562
+ })).createField?.field;
563
+ if (!fieldA?.id) throw new Error("Failed to create FK field for table A");
564
+ const fieldAId = fieldA.id;
565
+ const fieldB = (await createFieldMutation.mutateAsync({
566
+ name: fieldBName,
567
+ type: toFieldType("uuid"),
568
+ tableId: junctionTableId,
569
+ databaseId,
570
+ isRequired: true
571
+ })).createField?.field;
572
+ if (!fieldB?.id) throw new Error("Failed to create FK field for table B");
573
+ const fieldBId = fieldB.id;
574
+ await createFkMutation.mutateAsync({
575
+ databaseId,
576
+ tableId: junctionTableId,
577
+ fieldIds: asFieldIds([fieldAId]),
578
+ refTableId: input.tableAId,
579
+ refFieldIds: asFieldIds([input.tableAPrimaryKeyFieldId]),
580
+ name: `${input.junctionTableName}_${input.tableAName}_fkey`,
581
+ deleteAction,
582
+ updateAction,
583
+ type: "f"
584
+ });
585
+ await createFkMutation.mutateAsync({
586
+ databaseId,
587
+ tableId: junctionTableId,
588
+ fieldIds: asFieldIds([fieldBId]),
589
+ refTableId: input.tableBId,
590
+ refFieldIds: asFieldIds([input.tableBPrimaryKeyFieldId]),
591
+ name: `${input.junctionTableName}_${input.tableBName}_fkey`,
592
+ deleteAction,
593
+ updateAction,
594
+ type: "f"
595
+ });
596
+ await createUniqueMutation.mutateAsync({
597
+ tableId: junctionTableId,
598
+ databaseId,
599
+ fieldIds: asFieldIds([fieldAId, fieldBId]),
600
+ name: `${input.junctionTableName}_pair_unique`,
601
+ type: "u"
602
+ });
603
+ await createGrant({
604
+ databaseId,
605
+ tableName: junctionTableName,
606
+ privileges: [
607
+ "select",
608
+ "insert",
609
+ "update",
610
+ "delete"
611
+ ],
612
+ roleName: "authenticated"
613
+ }).catch((error) => {
614
+ console.warn(`Failed to grant permissions for junction table: ${error instanceof Error ? error.message : "Unknown error"}`);
615
+ });
616
+ return {
617
+ junctionTableId,
618
+ junctionTableName
619
+ };
620
+ },
621
+ onSuccess: async (result) => {
622
+ await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
623
+ if (result?.junctionTableId && result?.junctionTableName) selectTable(result.junctionTableId, result.junctionTableName);
624
+ },
625
+ onError: (error) => {
626
+ console.error("Failed to create many-to-many relationship:", error);
627
+ }
628
+ });
629
+ }
630
+
631
+ //#endregion
632
+ //#region src/schema/schema-builder-relationships/components/table-editor/relationships/relationship-diagram-themes.ts
633
+ const AMBIENT_LIGHT = {
634
+ bg: "#ffffff",
635
+ text: "#4B5563",
636
+ muted: "#9CA3AF"
637
+ };
638
+ const AMBIENT_DARK = {
639
+ bg: "#1e293b",
640
+ text: "#D1D5DB",
641
+ muted: "#6B7280"
642
+ };
643
+ const RELATIONSHIP_THEMES = {
644
+ "one-to-one": {
645
+ light: {
646
+ primary: "#A855F7",
647
+ fill: "#F3E8FF",
648
+ border: "#D8B4FE",
649
+ connector: "#A855F7",
650
+ connectorLight: "#E9D5FF",
651
+ ...AMBIENT_LIGHT
652
+ },
653
+ dark: {
654
+ primary: "#C084FC",
655
+ fill: "#3B0764",
656
+ border: "#7E22CE",
657
+ connector: "#C084FC",
658
+ connectorLight: "#9333EA",
659
+ ...AMBIENT_DARK
660
+ }
661
+ },
662
+ "belongs-to": {
663
+ light: {
664
+ primary: "#22C55E",
665
+ fill: "#F0FDF4",
666
+ border: "#86EFAC",
667
+ connector: "#22C55E",
668
+ connectorLight: "#BBF7D0",
669
+ ...AMBIENT_LIGHT
670
+ },
671
+ dark: {
672
+ primary: "#4ADE80",
673
+ fill: "#052E16",
674
+ border: "#166534",
675
+ connector: "#4ADE80",
676
+ connectorLight: "#15803D",
677
+ ...AMBIENT_DARK
678
+ }
679
+ },
680
+ "one-to-many": {
681
+ light: {
682
+ primary: "#3B82F6",
683
+ fill: "#DBEAFE",
684
+ border: "#93C5FD",
685
+ connector: "#3B82F6",
686
+ connectorLight: "#BFDBFE",
687
+ ...AMBIENT_LIGHT
688
+ },
689
+ dark: {
690
+ primary: "#60A5FA",
691
+ fill: "#172554",
692
+ border: "#1E40AF",
693
+ connector: "#60A5FA",
694
+ connectorLight: "#2563EB",
695
+ ...AMBIENT_DARK
696
+ }
697
+ },
698
+ "many-to-many": {
699
+ light: {
700
+ primary: "#F59E0B",
701
+ fill: "#FEF3C7",
702
+ border: "#FCD34D",
703
+ connector: "#F59E0B",
704
+ connectorLight: "#FDE68A",
705
+ ...AMBIENT_LIGHT
706
+ },
707
+ dark: {
708
+ primary: "#FBBF24",
709
+ fill: "#451A03",
710
+ border: "#92400E",
711
+ connector: "#FBBF24",
712
+ connectorLight: "#B45309",
713
+ ...AMBIENT_DARK
714
+ }
715
+ }
716
+ };
717
+ function getRelationshipTheme(type, mode = "light") {
718
+ return RELATIONSHIP_THEMES[type][mode];
719
+ }
720
+
721
+ //#endregion
722
+ //#region src/schema/schema-builder-relationships/components/table-editor/relationships/relationship-diagrams.tsx
723
+ const GAP = 8;
724
+ const FAN_MARGIN_RIGHT = 4;
725
+ const PAD = 12;
726
+ function layoutHorizontal(widths) {
727
+ let x = 0;
728
+ const centers = [];
729
+ for (let i = 0; i < widths.length; i++) {
730
+ if (i > 0) x += GAP;
731
+ centers.push(x + widths[i] / 2);
732
+ x += widths[i];
733
+ }
734
+ return {
735
+ totalWidth: x,
736
+ centers
737
+ };
738
+ }
739
+ function svgProps(w, h = 76) {
740
+ return {
741
+ width: w + PAD * 2,
742
+ height: h + PAD * 2,
743
+ viewBox: `-12 -12 ${w + PAD * 2} ${h + PAD * 2}`,
744
+ fill: "none"
745
+ };
746
+ }
747
+ function fanLeftX(center, topLabel) {
748
+ return center - fanConnectorWidth(topLabel) / 2;
749
+ }
750
+ function useMode() {
751
+ const { colorMode: resolvedTheme } = useSchemaBuilderRuntime();
752
+ return resolvedTheme === "dark" ? "dark" : "light";
753
+ }
754
+ function OneToOneDiagram({ sourceTable, targetTable, fieldName }) {
755
+ const theme = getRelationshipTheme("one-to-one", useMode());
756
+ const connW = connectorWidthForLabel(fieldName, 60);
757
+ const { totalWidth, centers } = layoutHorizontal([
758
+ nodeWidth("md"),
759
+ connW,
760
+ nodeWidth("md")
761
+ ]);
762
+ return /* @__PURE__ */ jsxs("svg", {
763
+ ...svgProps(totalWidth),
764
+ children: [
765
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
766
+ x: centers[0],
767
+ icon: TABLE2_ICON,
768
+ label: sourceTable,
769
+ theme
770
+ }),
771
+ /* @__PURE__ */ jsx(SvgConnector, {
772
+ x: centers[1] - connW / 2,
773
+ width: connW,
774
+ theme,
775
+ topLabel: fieldName,
776
+ topLabelFilled: fieldName !== "fk"
777
+ }),
778
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
779
+ x: centers[2],
780
+ icon: TABLE2_ICON,
781
+ label: targetTable,
782
+ theme
783
+ })
784
+ ]
785
+ });
786
+ }
787
+ function SvgFanConnectorReversed({ x, theme, topLabel, topLabelFilled }) {
788
+ const flatWidth = topLabel ? Math.max(24, topLabelBoxWidth(topLabel) + 4) : 24;
789
+ const totalWidth = flatWidth + 36;
790
+ const cy = 28;
791
+ const fanSpread = 14;
792
+ const fanTipX = x + 6;
793
+ const fanMidX = x + totalWidth - flatWidth - 7;
794
+ const endDotCx = x + totalWidth - 6;
795
+ const startDots = [
796
+ {
797
+ cx: fanTipX,
798
+ cy: cy - fanSpread
799
+ },
800
+ {
801
+ cx: fanTipX,
802
+ cy
803
+ },
804
+ {
805
+ cx: fanTipX,
806
+ cy: cy + fanSpread
807
+ }
808
+ ];
809
+ return /* @__PURE__ */ jsxs("g", { children: [
810
+ startDots.map((dot, i) => /* @__PURE__ */ jsx("line", {
811
+ x1: dot.cx,
812
+ y1: dot.cy,
813
+ x2: fanMidX,
814
+ y2: cy,
815
+ stroke: theme.connector,
816
+ strokeWidth: "2",
817
+ strokeLinecap: "round"
818
+ }, i)),
819
+ /* @__PURE__ */ jsx("line", {
820
+ x1: fanMidX,
821
+ y1: cy,
822
+ x2: endDotCx,
823
+ y2: cy,
824
+ stroke: theme.connectorLight,
825
+ strokeWidth: "2.5",
826
+ strokeLinecap: "round"
827
+ }),
828
+ startDots.map((dot, i) => /* @__PURE__ */ jsx("circle", {
829
+ cx: dot.cx,
830
+ cy: dot.cy,
831
+ r: 3,
832
+ fill: theme.connector
833
+ }, i)),
834
+ /* @__PURE__ */ jsx("circle", {
835
+ cx: fanMidX,
836
+ cy,
837
+ r: 3,
838
+ fill: theme.connector
839
+ }),
840
+ /* @__PURE__ */ jsx("circle", {
841
+ cx: endDotCx,
842
+ cy,
843
+ r: 4,
844
+ fill: theme.connector
845
+ }),
846
+ topLabel && /* @__PURE__ */ jsx(SvgTopLabel, {
847
+ cx: fanMidX + (endDotCx - fanMidX) / 2,
848
+ y: 4,
849
+ label: topLabel,
850
+ filled: topLabelFilled,
851
+ theme
852
+ })
853
+ ] });
854
+ }
855
+ function BelongsToDiagram({ sourceTable, targetTable, fieldName }) {
856
+ const theme = getRelationshipTheme("belongs-to", useMode());
857
+ const fanW = fanConnectorWidth(fieldName);
858
+ const { totalWidth, centers } = layoutHorizontal([
859
+ nodeWidth("md", true),
860
+ FAN_MARGIN_RIGHT + fanW,
861
+ nodeWidth("md")
862
+ ]);
863
+ return /* @__PURE__ */ jsxs("svg", {
864
+ ...svgProps(totalWidth),
865
+ children: [
866
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
867
+ x: centers[0],
868
+ icon: DATABASE_ICON,
869
+ label: sourceTable,
870
+ theme,
871
+ stacked: true,
872
+ outlined: true
873
+ }),
874
+ /* @__PURE__ */ jsx(SvgFanConnectorReversed, {
875
+ x: fanLeftX(centers[1], fieldName),
876
+ theme,
877
+ topLabel: fieldName,
878
+ topLabelFilled: fieldName !== "fk"
879
+ }),
880
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
881
+ x: centers[2],
882
+ icon: TABLE2_ICON,
883
+ label: targetTable,
884
+ theme
885
+ })
886
+ ]
887
+ });
888
+ }
889
+ function OneToManyDiagram({ sourceTable, targetTable, fieldName }) {
890
+ const theme = getRelationshipTheme("one-to-many", useMode());
891
+ const fanW = fanConnectorWidth(fieldName);
892
+ const { totalWidth, centers } = layoutHorizontal([
893
+ nodeWidth("md"),
894
+ fanW + FAN_MARGIN_RIGHT,
895
+ nodeWidth("md", true)
896
+ ]);
897
+ return /* @__PURE__ */ jsxs("svg", {
898
+ ...svgProps(totalWidth),
899
+ children: [
900
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
901
+ x: centers[0],
902
+ icon: TABLE2_ICON,
903
+ label: sourceTable,
904
+ theme
905
+ }),
906
+ /* @__PURE__ */ jsx(SvgFanConnector, {
907
+ x: fanLeftX(centers[1], fieldName),
908
+ theme,
909
+ topLabel: fieldName,
910
+ topLabelFilled: fieldName !== "fk"
911
+ }),
912
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
913
+ x: centers[2],
914
+ icon: DATABASE_ICON,
915
+ label: targetTable,
916
+ theme,
917
+ stacked: true,
918
+ outlined: true
919
+ })
920
+ ]
921
+ });
922
+ }
923
+ function ManyToManyDiagram({ sourceTable, targetTable, junctionTable, sourceFkName, targetFkName }) {
924
+ const theme = getRelationshipTheme("many-to-many", useMode());
925
+ const srcFk = sourceFkName || `${sourceTable}_id`;
926
+ const tgtFk = targetFkName || `${targetTable}_id`;
927
+ const srcFanW = fanConnectorWidth(srcFk);
928
+ const tgtFanW = fanConnectorWidth(tgtFk);
929
+ const { totalWidth, centers } = layoutHorizontal([
930
+ nodeWidth("sm"),
931
+ srcFanW + FAN_MARGIN_RIGHT,
932
+ nodeWidth("sm"),
933
+ FAN_MARGIN_RIGHT + tgtFanW,
934
+ nodeWidth("sm")
935
+ ]);
936
+ return /* @__PURE__ */ jsxs("svg", {
937
+ ...svgProps(totalWidth),
938
+ children: [
939
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
940
+ x: centers[0],
941
+ icon: TABLE2_ICON,
942
+ label: sourceTable,
943
+ theme,
944
+ size: "sm"
945
+ }),
946
+ /* @__PURE__ */ jsx(SvgFanConnectorReversed, {
947
+ x: fanLeftX(centers[1], srcFk),
948
+ theme,
949
+ topLabel: srcFk,
950
+ topLabelFilled: true
951
+ }),
952
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
953
+ x: centers[2],
954
+ icon: DATABASE_ICON,
955
+ label: junctionTable,
956
+ theme,
957
+ size: "sm"
958
+ }),
959
+ /* @__PURE__ */ jsx(SvgFanConnector, {
960
+ x: fanLeftX(centers[3], tgtFk),
961
+ theme,
962
+ topLabel: tgtFk,
963
+ topLabelFilled: true
964
+ }),
965
+ /* @__PURE__ */ jsx(SvgDiagramNode, {
966
+ x: centers[4],
967
+ icon: TABLE2_ICON,
968
+ label: targetTable,
969
+ theme,
970
+ size: "sm"
971
+ })
972
+ ]
973
+ });
974
+ }
975
+ function RelationshipDiagram({ type, sourceTable, targetTable, fieldName = "fk", junctionTable = "junction", sourceFkName, targetFkName, className }) {
976
+ return /* @__PURE__ */ jsxs(ResponsiveDiagram, {
977
+ className,
978
+ children: [
979
+ type === "one-to-one" && /* @__PURE__ */ jsx(OneToOneDiagram, {
980
+ sourceTable,
981
+ targetTable,
982
+ fieldName
983
+ }),
984
+ type === "belongs-to" && /* @__PURE__ */ jsx(BelongsToDiagram, {
985
+ sourceTable,
986
+ targetTable,
987
+ fieldName
988
+ }),
989
+ type === "one-to-many" && /* @__PURE__ */ jsx(OneToManyDiagram, {
990
+ sourceTable,
991
+ targetTable,
992
+ fieldName
993
+ }),
994
+ type === "many-to-many" && /* @__PURE__ */ jsx(ManyToManyDiagram, {
995
+ sourceTable,
996
+ targetTable,
997
+ junctionTable,
998
+ sourceFkName,
999
+ targetFkName
1000
+ })
1001
+ ]
1002
+ });
1003
+ }
1004
+
1005
+ //#endregion
1006
+ //#region src/schema/schema-builder-relationships/components/table-editor/relationships/relationship-type-selector.tsx
1007
+ const TYPE_OPTIONS = [
1008
+ {
1009
+ type: "one-to-one",
1010
+ icon: Link2,
1011
+ title: "One-to-One",
1012
+ subtitle: "Exactly one each",
1013
+ colors: {
1014
+ iconBg: "bg-purple-500/10",
1015
+ iconText: "text-purple-600 dark:text-purple-400",
1016
+ selectedBorder: "border-purple-500",
1017
+ selectedBg: "bg-purple-500/5",
1018
+ checkBg: "bg-purple-500"
1019
+ }
1020
+ },
1021
+ {
1022
+ type: "belongs-to",
1023
+ icon: Share2,
1024
+ iconClassName: "rotate-180",
1025
+ title: "Belongs To",
1026
+ subtitle: "Many belong to one",
1027
+ colors: {
1028
+ iconBg: "bg-green-500/10",
1029
+ iconText: "text-green-600 dark:text-green-400",
1030
+ selectedBorder: "border-green-500",
1031
+ selectedBg: "bg-green-500/5",
1032
+ checkBg: "bg-green-500"
1033
+ }
1034
+ },
1035
+ {
1036
+ type: "one-to-many",
1037
+ icon: Share2,
1038
+ title: "One-to-Many",
1039
+ subtitle: "One has many",
1040
+ colors: {
1041
+ iconBg: "bg-blue-500/10",
1042
+ iconText: "text-blue-600 dark:text-blue-400",
1043
+ selectedBorder: "border-blue-500",
1044
+ selectedBg: "bg-blue-500/5",
1045
+ checkBg: "bg-blue-500"
1046
+ }
1047
+ },
1048
+ {
1049
+ type: "many-to-many",
1050
+ icon: Waypoints,
1051
+ title: "Many-to-Many",
1052
+ subtitle: "Linked via join table",
1053
+ colors: {
1054
+ iconBg: "bg-amber-500/10",
1055
+ iconText: "text-amber-600 dark:text-amber-400",
1056
+ selectedBorder: "border-amber-500",
1057
+ selectedBg: "bg-amber-500/5",
1058
+ checkBg: "bg-amber-500"
1059
+ }
1060
+ }
1061
+ ];
1062
+ function RelationshipTypeSelector({ value, onChange, disabled }) {
1063
+ return /* @__PURE__ */ jsxs("div", {
1064
+ className: "space-y-3",
1065
+ children: [/* @__PURE__ */ jsx("p", {
1066
+ className: "text-muted-foreground text-xs font-semibold tracking-wide uppercase",
1067
+ children: "Select type"
1068
+ }), /* @__PURE__ */ jsx("div", {
1069
+ className: "grid grid-cols-2 gap-2",
1070
+ children: TYPE_OPTIONS.map((option) => {
1071
+ const Icon = option.icon;
1072
+ const isSelected = value === option.type;
1073
+ return /* @__PURE__ */ jsxs("button", {
1074
+ type: "button",
1075
+ onClick: () => onChange(option.type),
1076
+ disabled,
1077
+ className: cn(`border-border relative flex min-h-10 items-center gap-2 rounded-lg border px-3 py-2.5 text-left
1078
+ transition-[background-color,border-color,box-shadow,scale] duration-150 ease-out motion-safe:active:scale-[0.96]`, !isSelected && "disabled:pointer-events-none disabled:opacity-70", isSelected && "disabled:pointer-events-none", isSelected ? [option.colors.selectedBorder, option.colors.selectedBg] : "hover:border-border/80 hover:bg-muted/30"),
1079
+ children: [
1080
+ isSelected && /* @__PURE__ */ jsx("div", {
1081
+ className: "absolute top-2 right-2",
1082
+ children: /* @__PURE__ */ jsx("div", {
1083
+ className: cn("flex h-4 w-4 items-center justify-center rounded-full", option.colors.checkBg),
1084
+ children: /* @__PURE__ */ jsx(Check, {
1085
+ className: "h-2.5 w-2.5 text-white",
1086
+ strokeWidth: 3
1087
+ })
1088
+ })
1089
+ }),
1090
+ /* @__PURE__ */ jsx("div", {
1091
+ className: cn("flex h-7 w-7 shrink-0 items-center justify-center rounded-md", option.colors.iconBg),
1092
+ children: /* @__PURE__ */ jsx(Icon, { className: cn("h-4 w-4", option.colors.iconText, option.iconClassName) })
1093
+ }),
1094
+ /* @__PURE__ */ jsxs("div", {
1095
+ className: "min-w-0 flex-1 pr-4",
1096
+ children: [/* @__PURE__ */ jsx("h3", {
1097
+ className: "text-balance text-xs leading-tight font-semibold",
1098
+ children: option.title
1099
+ }), /* @__PURE__ */ jsx("p", {
1100
+ className: cn("text-muted-foreground text-[10px]", isSelected && option.colors.iconText),
1101
+ children: option.subtitle
1102
+ })]
1103
+ })
1104
+ ]
1105
+ }, option.type);
1106
+ })
1107
+ })]
1108
+ });
1109
+ }
1110
+
1111
+ //#endregion
1112
+ //#region src/schema/schema-builder-relationships/components/table-editor/relationships/relationship-card.tsx
1113
+ function TableCombobox({ tables, value, onChange, placeholder = "Select table", disabled }) {
1114
+ const options = tables.map((t) => ({
1115
+ value: t.id,
1116
+ label: t.name,
1117
+ table: t
1118
+ }));
1119
+ return /* @__PURE__ */ jsxs(Combobox, {
1120
+ items: options,
1121
+ value: options.find((o) => o.value === value) ?? null,
1122
+ onValueChange: (next) => next && onChange(next.value),
1123
+ filter: (item, q) => item.label.toLowerCase().includes(q.toLowerCase()),
1124
+ disabled,
1125
+ children: [/* @__PURE__ */ jsx(ComboboxInput, {
1126
+ placeholder,
1127
+ showClear: false
1128
+ }), /* @__PURE__ */ jsxs(ComboboxPopup, { children: [/* @__PURE__ */ jsx(ComboboxEmpty, { children: "No tables found" }), /* @__PURE__ */ jsx(ComboboxList, { children: (option) => /* @__PURE__ */ jsx(ComboboxItem, {
1129
+ value: option,
1130
+ children: option.label
1131
+ }, option.value) })] })]
1132
+ });
1133
+ }
1134
+ function FieldCombobox({ fields, value, onChange, placeholder = "Select field", disabled }) {
1135
+ const selectedField = fields.find((f) => f.id === value);
1136
+ const options = fields.map((f) => ({
1137
+ value: f.id,
1138
+ label: selectedField?.id === f.id ? `${f.name} ` : f.name,
1139
+ field: f
1140
+ }));
1141
+ return /* @__PURE__ */ jsxs("div", {
1142
+ className: "relative",
1143
+ children: [/* @__PURE__ */ jsxs(Combobox, {
1144
+ items: options,
1145
+ value: options.find((o) => o.value === value) ?? null,
1146
+ onValueChange: (next) => next && onChange(next.value),
1147
+ filter: (item, q) => {
1148
+ const lower = q.toLowerCase();
1149
+ return item.field.name.toLowerCase().includes(lower) || item.field.type.toLowerCase().includes(lower);
1150
+ },
1151
+ disabled,
1152
+ children: [/* @__PURE__ */ jsx(ComboboxInput, {
1153
+ placeholder,
1154
+ showClear: false
1155
+ }), /* @__PURE__ */ jsxs(ComboboxPopup, { children: [/* @__PURE__ */ jsx(ComboboxEmpty, { children: "No fields found" }), /* @__PURE__ */ jsx(ComboboxList, { children: (option) => /* @__PURE__ */ jsx(ComboboxItem, {
1156
+ value: option,
1157
+ children: /* @__PURE__ */ jsxs("div", {
1158
+ className: "flex w-full items-center justify-between gap-2",
1159
+ children: [/* @__PURE__ */ jsx("span", { children: option.field.name }), /* @__PURE__ */ jsx("span", {
1160
+ className: "bg-muted text-muted-foreground rounded px-1.5 py-0.5 font-mono text-[10px]",
1161
+ children: option.field.type
1162
+ })]
1163
+ })
1164
+ }, option.value) })] })]
1165
+ }), selectedField && /* @__PURE__ */ jsx("span", {
1166
+ className: "bg-muted text-muted-foreground pointer-events-none absolute top-1/2 right-9 -translate-y-1/2\n rounded px-1.5 py-0.5 font-mono text-[10px]",
1167
+ children: selectedField.type
1168
+ })]
1169
+ });
1170
+ }
1171
+ const isSystemTable = (category) => category === "CORE" || category === "MODULE";
1172
+ const RelationshipCard = ({ editingRelationship, sourceTable: directSourceTable, prefilledSourceTableId, prefilledTargetTableId, card }) => {
1173
+ const editingConstraint = editingRelationship?.constraint ?? null;
1174
+ const { currentSchema, currentTable: selectedTable, currentDatabase } = useSchemaBuilderSelectors();
1175
+ const relationProvisionMutation = useRelationProvision();
1176
+ const updateForeignKeyMutation = useUpdateForeignKey();
1177
+ const { policyTypes } = usePolicyTypes();
1178
+ const databaseId = currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId ?? "";
1179
+ const { data: tablesWithPolicies = [] } = useDatabasePolicies(databaseId, { enabled: !!databaseId });
1180
+ const tables = currentSchema?.tables || [];
1181
+ const defaultSourceTableId = useMemo(() => {
1182
+ if (directSourceTable) return directSourceTable.id;
1183
+ if (prefilledSourceTableId) return prefilledSourceTableId;
1184
+ return selectedTable?.id || "";
1185
+ }, [
1186
+ directSourceTable,
1187
+ prefilledSourceTableId,
1188
+ selectedTable?.id
1189
+ ]);
1190
+ const [relationshipType, setRelationshipType] = useState(RelationshipTypes.ONE_TO_ONE);
1191
+ const [sourceTableId, setSourceTableId] = useState(defaultSourceTableId);
1192
+ const [sourceFieldId, setSourceFieldId] = useState("");
1193
+ const [targetTableId, setTargetTableId] = useState(prefilledTargetTableId || "");
1194
+ const [targetFieldId, setTargetFieldId] = useState("");
1195
+ const [onDelete, setOnDelete] = useState(ForeignKeyActions.NO_ACTION);
1196
+ const [junctionTableName, setJunctionTableName] = useState("");
1197
+ const [showAdvanced, setShowAdvanced] = useState(false);
1198
+ const [fieldNameOverride, setFieldNameOverride] = useState("");
1199
+ const [isRequired, setIsRequired] = useState(true);
1200
+ const [useCompositeKey, setUseCompositeKey] = useState(true);
1201
+ const [sourceFieldName, setSourceFieldName] = useState("");
1202
+ const [targetFieldName, setTargetFieldName] = useState("");
1203
+ const [exposeInApi, setExposeInApi] = useState(true);
1204
+ const [selectedPolicyTypeName, setSelectedPolicyTypeName] = useState("AuthzAllowAll");
1205
+ const [policyData, setPolicyData] = useState({});
1206
+ const isEditMode = editingConstraint !== null;
1207
+ const isManyToMany = relationshipType === RelationshipTypes.MANY_TO_MANY;
1208
+ const isFkType = !isManyToMany;
1209
+ const isM2MEdit = isEditMode && isManyToMany;
1210
+ const sourceTable = useMemo(() => tables.find((t) => t.id === sourceTableId), [tables, sourceTableId]);
1211
+ const targetTable = useMemo(() => tables.find((t) => t.id === targetTableId), [tables, targetTableId]);
1212
+ const availableSourceTables = useMemo(() => tables.filter((t) => !isSystemTable(t.category)), [tables]);
1213
+ const availableTargetTables = useMemo(() => tables.filter((t) => (isEditMode || t.id !== sourceTableId) && !isSystemTable(t.category)), [
1214
+ tables,
1215
+ sourceTableId,
1216
+ isEditMode
1217
+ ]);
1218
+ const sourceFields = sourceTable?.fields || [];
1219
+ const targetFields = targetTable?.fields || [];
1220
+ const existingTableNames = useMemo(() => new Set(tables.map((t) => t.name.toLowerCase())), [tables]);
1221
+ const isJunctionNameTaken = useMemo(() => junctionTableName.trim() && existingTableNames.has(junctionTableName.trim().toLowerCase()), [junctionTableName, existingTableNames]);
1222
+ const inferredPolicyTypeName = useMemo(() => {
1223
+ const tableData = tablesWithPolicies.find((t) => t.id === sourceTableId);
1224
+ if (!tableData?.policies?.length) return "AuthzAllowAll";
1225
+ const resolved = tableData.policies.find((p) => p.privilege === "SELECT" && p.policyType)?.policyType ?? tableData.policies.find((p) => p.policyType)?.policyType ?? "AuthzAllowAll";
1226
+ return resolved === "AuthzComposite" ? "AuthzDirectOwner" : resolved;
1227
+ }, [tablesWithPolicies, sourceTableId]);
1228
+ const selectedPolicyType = useMemo(() => policyTypes.find((pt) => pt.name === selectedPolicyTypeName) ?? null, [policyTypes, selectedPolicyTypeName]);
1229
+ useEffect(() => {
1230
+ if (editingRelationship) {
1231
+ const { constraint, sourceTable: relSource, targetTable: relTarget, relationshipType: relType } = editingRelationship;
1232
+ setOnDelete(constraint.onDelete || ForeignKeyActions.NO_ACTION);
1233
+ setRelationshipType(relType);
1234
+ setJunctionTableName("");
1235
+ if (relType === "many-to-many") {
1236
+ const actualTarget = tables.find((t) => t.name === editingRelationship.withTableName);
1237
+ setTargetTableId(actualTarget?.id || "");
1238
+ } else {
1239
+ setTargetTableId(relTarget?.id || "");
1240
+ const srcField = relSource?.fields.find((f) => f.name === editingRelationship.sourceFieldName);
1241
+ const tgtField = relTarget?.fields.find((f) => f.name === editingRelationship.targetFieldName);
1242
+ setSourceFieldId(srcField?.id || "");
1243
+ setTargetFieldId(tgtField?.id || "");
1244
+ }
1245
+ }
1246
+ }, [editingRelationship, tables]);
1247
+ const m2mEditInfo = useMemo(() => {
1248
+ if (!isM2MEdit || !editingRelationship) return null;
1249
+ const junction = editingRelationship.targetTable;
1250
+ if (!junction) return null;
1251
+ const actualTarget = tables.find((t) => t.name === editingRelationship.withTableName);
1252
+ const srcFkName = editingRelationship.targetFieldName;
1253
+ const targetFk = (junction.constraints || []).filter((c) => c.type === "foreign_key").find((c) => c.referencedTable === actualTarget?.id);
1254
+ const tgtFkName = targetFk ? junction.fields.find((f) => f.id === targetFk.fields[0])?.name || "" : "";
1255
+ const pk = (junction.constraints || []).find((c) => c.type === "primary_key");
1256
+ const hasCompositePk = pk ? pk.fields.length > 1 : false;
1257
+ return {
1258
+ junctionName: junction.name,
1259
+ srcFkName,
1260
+ tgtFkName,
1261
+ hasCompositePk
1262
+ };
1263
+ }, [
1264
+ isM2MEdit,
1265
+ editingRelationship,
1266
+ tables
1267
+ ]);
1268
+ useEffect(() => {
1269
+ if (!isManyToMany) {
1270
+ setJunctionTableName("");
1271
+ setSourceFieldName("");
1272
+ setTargetFieldName("");
1273
+ }
1274
+ setSelectedPolicyTypeName(inferredPolicyTypeName);
1275
+ setPolicyData({});
1276
+ }, [isManyToMany, inferredPolicyTypeName]);
1277
+ useEffect(() => {
1278
+ if (selectedPolicyType) setPolicyData(getDefaultFormValues(selectedPolicyType));
1279
+ }, [selectedPolicyTypeName]);
1280
+ const handleSourceTableChange = useCallback((tableId) => {
1281
+ setSourceTableId(tableId);
1282
+ setTargetTableId((prev) => prev === tableId ? "" : prev);
1283
+ }, []);
1284
+ const handleTargetTableChange = useCallback((tableId) => {
1285
+ setTargetTableId(tableId);
1286
+ setTargetFieldId("");
1287
+ }, []);
1288
+ const handleSave = useCallback(async () => {
1289
+ if (!sourceTable) {
1290
+ toast.error({ message: "No source table selected" });
1291
+ return;
1292
+ }
1293
+ if (!targetTableId) {
1294
+ toast.error({ message: "Please select a target table" });
1295
+ return;
1296
+ }
1297
+ if (isManyToMany) {
1298
+ try {
1299
+ await relationProvisionMutation.mutateAsync({
1300
+ relationshipType,
1301
+ sourceTableId: sourceTable.id,
1302
+ targetTableId,
1303
+ ...junctionTableName.trim() && { junctionTableName: junctionTableName.trim() },
1304
+ useCompositeKey,
1305
+ exposeInApi,
1306
+ ...sourceFieldName.trim() && { sourceFieldName: sourceFieldName.trim() },
1307
+ ...targetFieldName.trim() && { targetFieldName: targetFieldName.trim() },
1308
+ policyType: selectedPolicyTypeName,
1309
+ policyData,
1310
+ policyTypeObj: selectedPolicyType ?? void 0
1311
+ });
1312
+ toast.success({
1313
+ message: "Many-to-many relationship created",
1314
+ description: junctionTableName.trim() ? `Junction table "${junctionTableName}" created.` : "Junction table created."
1315
+ });
1316
+ card.close();
1317
+ } catch (error) {
1318
+ toast.error({
1319
+ message: "Failed to create relationship",
1320
+ description: error instanceof Error ? error.message : "An error occurred"
1321
+ });
1322
+ }
1323
+ return;
1324
+ }
1325
+ if (isEditMode && editingConstraint) {
1326
+ try {
1327
+ await updateForeignKeyMutation.mutateAsync({
1328
+ id: editingConstraint.id,
1329
+ deleteAction: onDelete
1330
+ });
1331
+ toast.success({ message: "Relationship updated" });
1332
+ card.close();
1333
+ } catch (error) {
1334
+ toast.error({
1335
+ message: "Failed to update relationship",
1336
+ description: error instanceof Error ? error.message : "An error occurred"
1337
+ });
1338
+ }
1339
+ return;
1340
+ }
1341
+ try {
1342
+ await relationProvisionMutation.mutateAsync({
1343
+ relationshipType,
1344
+ sourceTableId: sourceTable.id,
1345
+ targetTableId,
1346
+ deleteAction: onDelete,
1347
+ ...fieldNameOverride.trim() && { fieldName: fieldNameOverride.trim() },
1348
+ ...isRequired !== void 0 && { isRequired }
1349
+ });
1350
+ toast.success({ message: "Relationship created" });
1351
+ card.close();
1352
+ } catch (error) {
1353
+ toast.error({
1354
+ message: "Failed to create relationship",
1355
+ description: error instanceof Error ? error.message : "An error occurred"
1356
+ });
1357
+ }
1358
+ }, [
1359
+ sourceTable,
1360
+ targetTableId,
1361
+ isManyToMany,
1362
+ junctionTableName,
1363
+ relationshipType,
1364
+ onDelete,
1365
+ isEditMode,
1366
+ editingConstraint,
1367
+ fieldNameOverride,
1368
+ isRequired,
1369
+ useCompositeKey,
1370
+ exposeInApi,
1371
+ sourceFieldName,
1372
+ targetFieldName,
1373
+ selectedPolicyTypeName,
1374
+ selectedPolicyType,
1375
+ policyData,
1376
+ relationProvisionMutation,
1377
+ updateForeignKeyMutation,
1378
+ card
1379
+ ]);
1380
+ const isPending = relationProvisionMutation.isPending || updateForeignKeyMutation.isPending;
1381
+ const canSaveM2M = isManyToMany && sourceTableId && targetTableId;
1382
+ const initialOnDelete = editingRelationship?.constraint.onDelete || ForeignKeyActions.NO_ACTION;
1383
+ const canSave = canSaveM2M || isEditMode && isFkType && onDelete !== initialOnDelete || !isEditMode && isFkType && sourceTableId && targetTableId;
1384
+ const sourceTableName = sourceTable?.name || "source";
1385
+ const targetTableName = targetTable?.name || "target";
1386
+ const fkDerivedTable = relationshipType === RelationshipTypes.ONE_TO_MANY ? sourceTable : targetTable;
1387
+ const diagramFieldName = fieldNameOverride.trim() || (fkDerivedTable ? `${fkDerivedTable.name}_id` : "fk");
1388
+ const diagramJunctionName = m2mEditInfo?.junctionName || junctionTableName.trim() || (sourceTable && targetTable ? [sourceTable.name, targetTable.name].sort().join("_") : "junction");
1389
+ const sourceFkPlaceholder = sourceTable ? `${sourceTable.name}_id (auto)` : "Auto-derived";
1390
+ const targetFkPlaceholder = targetTable ? `${targetTable.name}_id (auto)` : "Auto-derived";
1391
+ return /* @__PURE__ */ jsxs("div", {
1392
+ "data-chat-component": "relationship-card",
1393
+ "data-chat-relationship-type": relationshipType,
1394
+ "data-chat-source-table": sourceTable?.name ?? "",
1395
+ "data-chat-target-table": targetTable?.name ?? "",
1396
+ className: "flex h-full flex-col",
1397
+ children: [/* @__PURE__ */ jsx(ScrollArea, {
1398
+ className: "min-h-0 flex-1",
1399
+ children: /* @__PURE__ */ jsxs("div", {
1400
+ className: "space-y-6 p-6",
1401
+ children: [
1402
+ /* @__PURE__ */ jsx(RelationshipTypeSelector, {
1403
+ value: relationshipType,
1404
+ onChange: setRelationshipType,
1405
+ disabled: isEditMode
1406
+ }),
1407
+ /* @__PURE__ */ jsxs("div", {
1408
+ className: "space-y-4",
1409
+ children: [
1410
+ /* @__PURE__ */ jsx("p", {
1411
+ className: "text-muted-foreground text-xs font-semibold tracking-wide uppercase",
1412
+ children: "Connect tables"
1413
+ }),
1414
+ /* @__PURE__ */ jsx(RelationshipDiagram, {
1415
+ type: relationshipType,
1416
+ sourceTable: sourceTableName,
1417
+ targetTable: targetTableName,
1418
+ fieldName: isManyToMany ? void 0 : diagramFieldName,
1419
+ junctionTable: isManyToMany ? diagramJunctionName : void 0,
1420
+ sourceFkName: isManyToMany ? m2mEditInfo?.srcFkName || sourceFieldName.trim() || void 0 : void 0,
1421
+ targetFkName: isManyToMany ? m2mEditInfo?.tgtFkName || targetFieldName.trim() || void 0 : void 0
1422
+ }),
1423
+ /* @__PURE__ */ jsxs("div", {
1424
+ className: "space-y-1.5",
1425
+ children: [/* @__PURE__ */ jsx(Label, { children: "Source table" }), /* @__PURE__ */ jsx(TableCombobox, {
1426
+ tables: availableSourceTables,
1427
+ value: sourceTableId,
1428
+ onChange: handleSourceTableChange,
1429
+ placeholder: "Select source table",
1430
+ disabled: isEditMode
1431
+ })]
1432
+ }),
1433
+ /* @__PURE__ */ jsxs("div", {
1434
+ className: "space-y-1.5",
1435
+ children: [/* @__PURE__ */ jsx(Label, { children: "Target table" }), /* @__PURE__ */ jsx(TableCombobox, {
1436
+ tables: availableTargetTables,
1437
+ value: targetTableId,
1438
+ onChange: handleTargetTableChange,
1439
+ placeholder: "Select target table",
1440
+ disabled: isEditMode
1441
+ })]
1442
+ }),
1443
+ isManyToMany && /* @__PURE__ */ jsxs("div", {
1444
+ className: "space-y-3",
1445
+ children: [
1446
+ /* @__PURE__ */ jsxs("div", {
1447
+ className: "flex items-center justify-between gap-3",
1448
+ children: [/* @__PURE__ */ jsxs("div", {
1449
+ className: "space-y-0.5",
1450
+ children: [/* @__PURE__ */ jsx(Label, { children: "Use composite primary key" }), /* @__PURE__ */ jsx("p", {
1451
+ className: "text-muted-foreground text-xs",
1452
+ children: (isM2MEdit ? m2mEditInfo?.hasCompositePk : useCompositeKey) ? "Each source–target pair can only appear once." : "Rows get a UUID id — pairs are not enforced as unique."
1453
+ })]
1454
+ }), /* @__PURE__ */ jsx(Switch, {
1455
+ checked: isM2MEdit ? m2mEditInfo?.hasCompositePk ?? true : useCompositeKey,
1456
+ onCheckedChange: setUseCompositeKey,
1457
+ disabled: isM2MEdit
1458
+ })]
1459
+ }),
1460
+ /* @__PURE__ */ jsx("div", {
1461
+ className: "bg-muted/50 rounded-md px-3 py-2 font-mono text-xs",
1462
+ children: (isM2MEdit ? m2mEditInfo?.hasCompositePk : useCompositeKey) ? /* @__PURE__ */ jsxs(Fragment, { children: [
1463
+ /* @__PURE__ */ jsx("span", {
1464
+ className: "text-muted-foreground",
1465
+ children: "PK"
1466
+ }),
1467
+ " (",
1468
+ sourceTable?.name || "source",
1469
+ "_id,",
1470
+ " ",
1471
+ targetTable?.name || "target",
1472
+ "_id)"
1473
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
1474
+ /* @__PURE__ */ jsx("span", {
1475
+ className: "text-muted-foreground",
1476
+ children: "PK"
1477
+ }),
1478
+ " id",
1479
+ " ",
1480
+ /* @__PURE__ */ jsx("span", {
1481
+ className: "text-muted-foreground",
1482
+ children: "uuid"
1483
+ })
1484
+ ] })
1485
+ }),
1486
+ /* @__PURE__ */ jsxs("div", {
1487
+ className: "flex items-center justify-between gap-3",
1488
+ children: [/* @__PURE__ */ jsxs("div", {
1489
+ className: "space-y-0.5",
1490
+ children: [/* @__PURE__ */ jsx(Label, { children: "Expose in GraphQL API" }), /* @__PURE__ */ jsx("p", {
1491
+ className: "text-muted-foreground text-xs",
1492
+ children: "Makes this many-to-many relation directly queryable in the GraphQL API."
1493
+ })]
1494
+ }), /* @__PURE__ */ jsx(Switch, {
1495
+ checked: isM2MEdit ? editingRelationship?.provision?.exposeInApi ?? true : exposeInApi,
1496
+ onCheckedChange: setExposeInApi,
1497
+ disabled: isM2MEdit
1498
+ })]
1499
+ })
1500
+ ]
1501
+ }),
1502
+ isM2MEdit && m2mEditInfo && /* @__PURE__ */ jsxs(Fragment, { children: [
1503
+ /* @__PURE__ */ jsxs("div", {
1504
+ className: "space-y-1.5",
1505
+ children: [/* @__PURE__ */ jsx(Label, { children: "Junction table name" }), /* @__PURE__ */ jsx(Input, {
1506
+ value: m2mEditInfo.junctionName,
1507
+ disabled: true
1508
+ })]
1509
+ }),
1510
+ /* @__PURE__ */ jsxs("div", {
1511
+ className: "space-y-1.5",
1512
+ children: [/* @__PURE__ */ jsx(Label, { children: "Source FK name" }), /* @__PURE__ */ jsx(Input, {
1513
+ value: m2mEditInfo.srcFkName,
1514
+ disabled: true
1515
+ })]
1516
+ }),
1517
+ /* @__PURE__ */ jsxs("div", {
1518
+ className: "space-y-1.5",
1519
+ children: [/* @__PURE__ */ jsx(Label, { children: "Target FK name" }), /* @__PURE__ */ jsx(Input, {
1520
+ value: m2mEditInfo.tgtFkName,
1521
+ disabled: true
1522
+ })]
1523
+ })
1524
+ ] }),
1525
+ isEditMode && isFkType && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
1526
+ className: "space-y-1.5",
1527
+ children: [/* @__PURE__ */ jsx(Label, { children: "Source field" }), /* @__PURE__ */ jsx(FieldCombobox, {
1528
+ fields: sourceFields,
1529
+ value: sourceFieldId,
1530
+ onChange: setSourceFieldId,
1531
+ placeholder: "Select field",
1532
+ disabled: true
1533
+ })]
1534
+ }), /* @__PURE__ */ jsxs("div", {
1535
+ className: "space-y-1.5",
1536
+ children: [/* @__PURE__ */ jsx(Label, { children: "Target field" }), /* @__PURE__ */ jsx(FieldCombobox, {
1537
+ fields: targetFields,
1538
+ value: targetFieldId,
1539
+ onChange: setTargetFieldId,
1540
+ placeholder: "Select field",
1541
+ disabled: true
1542
+ })]
1543
+ })] }),
1544
+ isFkType && /* @__PURE__ */ jsxs("div", {
1545
+ className: "space-y-1.5",
1546
+ children: [/* @__PURE__ */ jsx(Label, { children: "On delete" }), /* @__PURE__ */ jsxs(Select, {
1547
+ value: onDelete,
1548
+ onValueChange: (v) => setOnDelete(v),
1549
+ children: [/* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx("span", { children: ForeignKeyActionLabels[onDelete] }) }), /* @__PURE__ */ jsx(SelectContent, { children: Object.entries(ForeignKeyActions).map(([key, value]) => /* @__PURE__ */ jsx(SelectItem, {
1550
+ value,
1551
+ children: ForeignKeyActionLabels[value]
1552
+ }, key)) })]
1553
+ })]
1554
+ })
1555
+ ]
1556
+ }),
1557
+ !isEditMode && /* @__PURE__ */ jsxs("div", {
1558
+ className: "border-border/60 rounded-lg border",
1559
+ children: [/* @__PURE__ */ jsxs("button", {
1560
+ type: "button",
1561
+ onClick: () => setShowAdvanced(!showAdvanced),
1562
+ className: "flex min-h-10 w-full items-center gap-2 rounded-lg px-4 py-3 text-left text-sm font-medium\n transition-[background-color,scale] duration-150 ease-out hover:bg-muted/40 motion-safe:active:scale-[0.96]",
1563
+ "aria-expanded": showAdvanced,
1564
+ children: [/* @__PURE__ */ jsx(ChevronRight, { className: cn("text-muted-foreground h-4 w-4 transition-transform", showAdvanced && "rotate-90") }), "Advanced"]
1565
+ }), showAdvanced && /* @__PURE__ */ jsxs("div", {
1566
+ className: "space-y-4 border-t px-4 pt-3 pb-4",
1567
+ children: [isFkType && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
1568
+ className: "space-y-1.5",
1569
+ children: [
1570
+ /* @__PURE__ */ jsx(Label, { children: "Foreign key field name" }),
1571
+ /* @__PURE__ */ jsx(Input, {
1572
+ value: fieldNameOverride,
1573
+ onChange: (e) => setFieldNameOverride(e.target.value),
1574
+ placeholder: targetTable ? `${targetTable.name}_id (auto)` : "Auto-derived"
1575
+ }),
1576
+ /* @__PURE__ */ jsx("p", {
1577
+ className: "text-muted-foreground text-xs",
1578
+ children: "Leave empty to auto-derive from the target table name."
1579
+ })
1580
+ ]
1581
+ }), /* @__PURE__ */ jsxs("div", {
1582
+ className: "flex items-center justify-between gap-3",
1583
+ children: [/* @__PURE__ */ jsxs("div", {
1584
+ className: "space-y-0.5",
1585
+ children: [/* @__PURE__ */ jsx(Label, { children: "Foreign key field required" }), /* @__PURE__ */ jsx("p", {
1586
+ className: "text-muted-foreground text-xs",
1587
+ children: "Whether the foreign key field is NOT NULL."
1588
+ })]
1589
+ }), /* @__PURE__ */ jsx(Switch, {
1590
+ checked: isRequired,
1591
+ onCheckedChange: setIsRequired
1592
+ })]
1593
+ })] }), isManyToMany && /* @__PURE__ */ jsxs(Fragment, { children: [
1594
+ /* @__PURE__ */ jsxs("div", {
1595
+ className: "space-y-1.5",
1596
+ children: [
1597
+ /* @__PURE__ */ jsx(Label, { children: "Junction table name" }),
1598
+ /* @__PURE__ */ jsx(Input, {
1599
+ value: junctionTableName,
1600
+ onChange: (e) => setJunctionTableName(e.target.value),
1601
+ placeholder: sourceTable && targetTable ? `${[sourceTable.name, targetTable.name].sort().join("_")} (auto)` : "Auto-derived",
1602
+ className: isJunctionNameTaken ? "border-destructive" : ""
1603
+ }),
1604
+ isJunctionNameTaken ? /* @__PURE__ */ jsx("p", {
1605
+ className: "text-destructive text-xs",
1606
+ children: "Table name already exists"
1607
+ }) : /* @__PURE__ */ jsx("p", {
1608
+ className: "text-muted-foreground text-xs",
1609
+ children: "Leave empty to auto-derive from table names."
1610
+ })
1611
+ ]
1612
+ }),
1613
+ /* @__PURE__ */ jsxs("div", {
1614
+ className: "space-y-1.5",
1615
+ children: [/* @__PURE__ */ jsx(Label, { children: "Source FK name" }), /* @__PURE__ */ jsx(Input, {
1616
+ value: sourceFieldName,
1617
+ onChange: (e) => setSourceFieldName(e.target.value),
1618
+ placeholder: sourceFkPlaceholder
1619
+ })]
1620
+ }),
1621
+ /* @__PURE__ */ jsxs("div", {
1622
+ className: "space-y-1.5",
1623
+ children: [/* @__PURE__ */ jsx(Label, { children: "Target FK name" }), /* @__PURE__ */ jsx(Input, {
1624
+ value: targetFieldName,
1625
+ onChange: (e) => setTargetFieldName(e.target.value),
1626
+ placeholder: targetFkPlaceholder
1627
+ })]
1628
+ }),
1629
+ /* @__PURE__ */ jsxs("div", {
1630
+ className: "space-y-1.5",
1631
+ children: [
1632
+ /* @__PURE__ */ jsx(Label, { children: "Junction table policy" }),
1633
+ /* @__PURE__ */ jsxs(Select, {
1634
+ value: selectedPolicyTypeName,
1635
+ onValueChange: setSelectedPolicyTypeName,
1636
+ children: [/* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsxs("div", {
1637
+ className: "flex items-center gap-2",
1638
+ children: [selectedPolicyType && /* @__PURE__ */ jsx(selectedPolicyType.icon, { className: "text-muted-foreground h-4 w-4 shrink-0" }), /* @__PURE__ */ jsx("span", { children: selectedPolicyType?.title || selectedPolicyTypeName })]
1639
+ }) }), /* @__PURE__ */ jsx(SelectContent, { children: policyTypes.filter((pt) => pt.name !== "AuthzComposite").map((pt) => {
1640
+ const Icon = pt.icon;
1641
+ return /* @__PURE__ */ jsx(SelectItem, {
1642
+ value: pt.name,
1643
+ children: /* @__PURE__ */ jsxs("div", {
1644
+ className: "flex items-center gap-2",
1645
+ children: [/* @__PURE__ */ jsx(Icon, { className: "text-muted-foreground h-4 w-4 shrink-0" }), /* @__PURE__ */ jsx("span", { children: pt.title })]
1646
+ })
1647
+ }, pt.name);
1648
+ }) })]
1649
+ }),
1650
+ selectedPolicyType && selectedPolicyTypeName === inferredPolicyTypeName && inferredPolicyTypeName !== "AuthzAllowAll" && /* @__PURE__ */ jsxs("div", {
1651
+ className: "text-muted-foreground flex items-start gap-2 text-xs",
1652
+ children: [/* @__PURE__ */ jsx(Info, { className: "mt-0.5 h-3 w-3 shrink-0" }), /* @__PURE__ */ jsx("span", { children: "Inherited from source table’s policy." })]
1653
+ }),
1654
+ selectedPolicyType && (selectedPolicyTypeName !== inferredPolicyTypeName || inferredPolicyTypeName === "AuthzAllowAll") && /* @__PURE__ */ jsx("p", {
1655
+ className: "text-muted-foreground text-xs",
1656
+ children: selectedPolicyType.description
1657
+ })
1658
+ ]
1659
+ }),
1660
+ selectedPolicyType && selectedPolicyTypeName !== "AuthzAllowAll" && (selectedPolicyType.fieldOverrides && Object.keys(selectedPolicyType.fieldOverrides).length > 0 || selectedPolicyType.generatedFields.length > 0) && /* @__PURE__ */ jsxs("div", {
1661
+ className: "space-y-3",
1662
+ children: [/* @__PURE__ */ jsx("p", {
1663
+ className: "text-muted-foreground text-xs font-semibold tracking-wide uppercase",
1664
+ children: "Policy config"
1665
+ }), /* @__PURE__ */ jsx(PolicyConfigForm, {
1666
+ policyType: selectedPolicyType,
1667
+ value: policyData,
1668
+ onChange: setPolicyData
1669
+ })]
1670
+ })
1671
+ ] })]
1672
+ })]
1673
+ })
1674
+ ]
1675
+ })
1676
+ }), /* @__PURE__ */ jsxs("div", {
1677
+ className: "flex justify-end gap-2 border-t px-4 py-3",
1678
+ children: [/* @__PURE__ */ jsx(Button, {
1679
+ variant: "outline",
1680
+ onClick: () => card.close(),
1681
+ disabled: isPending,
1682
+ children: isM2MEdit ? "Close" : "Cancel"
1683
+ }), !isM2MEdit && /* @__PURE__ */ jsxs(Button, {
1684
+ onClick: handleSave,
1685
+ disabled: !canSave || isPending,
1686
+ children: [isPending && /* @__PURE__ */ jsx(Loader2, { className: "mr-2 h-4 w-4 animate-spin" }), isEditMode ? "Update Relationship" : "Create Relationship"]
1687
+ })]
1688
+ })]
1689
+ });
1690
+ };
1691
+
1692
+ //#endregion
1693
+ //#region src/schema/schema-builder-relationships/components/table-editor/relationships/relationship-empty-state.tsx
1694
+ function RelationshipEmptyState({ onCreateClick, tableName }) {
1695
+ return /* @__PURE__ */ jsx(EmptyStateCard, {
1696
+ icon: Link2,
1697
+ title: tableName ? /* @__PURE__ */ jsxs(Fragment, { children: ["No relationships for ", tableName] }) : "Create your first relationship",
1698
+ description: tableName ? `Add a relationship to connect ${tableName} to other tables. This helps organize your data and maintain consistency.` : "Connect your tables to establish relationships. This helps organize your data and maintain consistency.",
1699
+ actionLabel: "Create Relationship",
1700
+ actionIcon: Link2,
1701
+ onAction: onCreateClick,
1702
+ features: [
1703
+ {
1704
+ icon: Shield,
1705
+ title: "Data Integrity",
1706
+ description: "Maintain consistency across tables"
1707
+ },
1708
+ {
1709
+ icon: Target,
1710
+ title: "Referential Actions",
1711
+ description: "Control cascade & deletion behavior"
1712
+ },
1713
+ {
1714
+ icon: Zap,
1715
+ title: "Query Optimization",
1716
+ description: "Efficient joins & data retrieval"
1717
+ }
1718
+ ]
1719
+ });
1720
+ }
1721
+
1722
+ //#endregion
1723
+ //#region src/compat/relation-provisions.ts
1724
+ function useRelationProvisionsQuery(variables) {
1725
+ const { adapter, scope } = useSchemaBuilder();
1726
+ const { enabled = true, ...operationVariables } = variables;
1727
+ return useQuery({
1728
+ queryKey: [
1729
+ "schema-builder",
1730
+ scope,
1731
+ "relationProvisions",
1732
+ operationVariables
1733
+ ],
1734
+ queryFn: ({ signal }) => adapter.relationships.relationProvisions(operationVariables, {
1735
+ scope,
1736
+ signal
1737
+ }),
1738
+ enabled
1739
+ });
1740
+ }
1741
+
1742
+ //#endregion
1743
+ //#region src/schema/schema-builder-relationships/components/table-editor/relationships/relationship-utils.ts
1744
+ /** Map backend relation type names to UI RelationshipType */
1745
+ const PROVISION_TYPE_MAP = {
1746
+ [RelationHasOne.name]: "one-to-one",
1747
+ [RelationBelongsTo.name]: "belongs-to",
1748
+ [RelationHasMany.name]: "one-to-many",
1749
+ [RelationManyToMany.name]: "many-to-many"
1750
+ };
1751
+ function isForeignKeyConstraint(constraint) {
1752
+ return typeof constraint === "object" && constraint != null && "type" in constraint && constraint.type === "foreign_key";
1753
+ }
1754
+ function resolveFieldName(table, fieldId) {
1755
+ if (!fieldId) return "";
1756
+ return table?.fields.find((field) => field.id === fieldId)?.name || fieldId;
1757
+ }
1758
+ /**
1759
+ * Find matching provision for an FK constraint.
1760
+ * Match by outFieldId (the FK field created by the provision) first,
1761
+ * then fall back to sourceTableId + targetTableId match.
1762
+ */
1763
+ function findProvision(constraint, sourceTableId, provisions) {
1764
+ const fkFieldId = constraint.fields[0];
1765
+ if (fkFieldId) {
1766
+ const byField = provisions.find((p) => p.outFieldId === fkFieldId);
1767
+ if (byField) return byField;
1768
+ }
1769
+ const targetTableId = constraint.referencedTable;
1770
+ return provisions.find((p) => p.sourceTableId === sourceTableId && p.targetTableId === targetTableId);
1771
+ }
1772
+ function resolveRelationshipType(constraint, provision) {
1773
+ if (provision?.relationType) {
1774
+ const mapped = PROVISION_TYPE_MAP[provision.relationType];
1775
+ if (mapped) return mapped;
1776
+ }
1777
+ return constraint.smartTags?.relationshipType || "one-to-many";
1778
+ }
1779
+ /**
1780
+ * Get all relationships involving the current table.
1781
+ * Source/target are oriented from the current table's perspective:
1782
+ * - belongs-to: currentTable (has FK) → referenced table
1783
+ * - one-to-one: currentTable (has FK) → referenced table
1784
+ * - one-to-many: currentTable (the "one" side) → other table (has FK)
1785
+ * - For FK on other table referencing current: other table → currentTable
1786
+ */
1787
+ function getTableRelationships(currentTable, tables, provisions = []) {
1788
+ if (!currentTable) return [];
1789
+ const relationships = [];
1790
+ const seenConstraintIds = /* @__PURE__ */ new Set();
1791
+ const ownForeignKeys = (currentTable.constraints || []).filter(isForeignKeyConstraint);
1792
+ for (const constraint of ownForeignKeys) {
1793
+ seenConstraintIds.add(constraint.id);
1794
+ const targetTable = tables.find((table) => table.id === constraint.referencedTable);
1795
+ const provision = findProvision(constraint, currentTable.id, provisions);
1796
+ relationships.push({
1797
+ constraint,
1798
+ sourceTable: currentTable,
1799
+ targetTable,
1800
+ sourceFieldName: resolveFieldName(currentTable, constraint.fields[0]),
1801
+ targetFieldName: resolveFieldName(targetTable, constraint.referencedFields?.[0]),
1802
+ relationshipType: resolveRelationshipType(constraint, provision),
1803
+ provision
1804
+ });
1805
+ }
1806
+ for (const otherTable of tables) {
1807
+ if (otherTable.id === currentTable.id) continue;
1808
+ const referencesCurrent = (otherTable.constraints || []).filter(isForeignKeyConstraint).filter((c) => c.referencedTable === currentTable.id);
1809
+ for (const constraint of referencesCurrent) {
1810
+ if (seenConstraintIds.has(constraint.id)) continue;
1811
+ seenConstraintIds.add(constraint.id);
1812
+ const provision = findProvision(constraint, otherTable.id, provisions);
1813
+ let relationshipType = resolveRelationshipType(constraint, provision);
1814
+ let withTableName;
1815
+ const m2mProvision = provisions.find((p) => p.relationType === RelationManyToMany.name && p.outJunctionTableId === otherTable.id && (p.sourceTableId === currentTable.id || p.targetTableId === currentTable.id));
1816
+ if (m2mProvision) {
1817
+ relationshipType = "many-to-many";
1818
+ const actualTargetId = m2mProvision.sourceTableId === currentTable.id ? m2mProvision.targetTableId : m2mProvision.sourceTableId;
1819
+ withTableName = tables.find((t) => t.id === actualTargetId)?.name;
1820
+ }
1821
+ if (relationshipType === "one-to-many" || relationshipType === "one-to-one" || relationshipType === "many-to-many") relationships.push({
1822
+ constraint,
1823
+ sourceTable: currentTable,
1824
+ targetTable: otherTable,
1825
+ sourceFieldName: resolveFieldName(currentTable, constraint.referencedFields?.[0]),
1826
+ targetFieldName: resolveFieldName(otherTable, constraint.fields[0]),
1827
+ relationshipType,
1828
+ provision: m2mProvision ?? provision,
1829
+ withTableName
1830
+ });
1831
+ else relationships.push({
1832
+ constraint,
1833
+ sourceTable: otherTable,
1834
+ targetTable: currentTable,
1835
+ sourceFieldName: resolveFieldName(otherTable, constraint.fields[0]),
1836
+ targetFieldName: resolveFieldName(currentTable, constraint.referencedFields?.[0]),
1837
+ relationshipType,
1838
+ provision
1839
+ });
1840
+ }
1841
+ }
1842
+ relationships.sort((a, b) => {
1843
+ const aTime = a.constraint.createdAt || "";
1844
+ return (b.constraint.createdAt || "").localeCompare(aTime);
1845
+ });
1846
+ return relationships;
1847
+ }
1848
+
1849
+ //#endregion
1850
+ //#region src/schema/schema-builder-relationships/components/table-editor/relationships/relationships-view.tsx
1851
+ const RELATIONSHIP_STYLES = {
1852
+ "one-to-one": {
1853
+ bg: "bg-purple-500/10",
1854
+ border: "border-purple-500/30",
1855
+ text: "text-purple-600 dark:text-purple-400",
1856
+ label: "One to One",
1857
+ shortLabel: "1:1"
1858
+ },
1859
+ "belongs-to": {
1860
+ bg: "bg-green-500/10",
1861
+ border: "border-green-500/30",
1862
+ text: "text-green-600 dark:text-green-400",
1863
+ label: "Belongs To",
1864
+ shortLabel: "BT"
1865
+ },
1866
+ "one-to-many": {
1867
+ bg: "bg-blue-500/10",
1868
+ border: "border-blue-500/30",
1869
+ text: "text-blue-600 dark:text-blue-400",
1870
+ label: "One to Many",
1871
+ shortLabel: "1:N"
1872
+ },
1873
+ "many-to-many": {
1874
+ bg: "bg-amber-500/10",
1875
+ border: "border-amber-500/30",
1876
+ text: "text-amber-600 dark:text-amber-400",
1877
+ label: "Many to Many",
1878
+ shortLabel: "N:M"
1879
+ }
1880
+ };
1881
+ function MiniConnector({ type }) {
1882
+ if (type === "one-to-one") return /* @__PURE__ */ jsxs("svg", {
1883
+ width: "32",
1884
+ height: "16",
1885
+ viewBox: "0 0 32 16",
1886
+ fill: "none",
1887
+ className: "shrink-0",
1888
+ children: [
1889
+ /* @__PURE__ */ jsx("circle", {
1890
+ cx: "4",
1891
+ cy: "8",
1892
+ r: "3",
1893
+ fill: "currentColor",
1894
+ fillOpacity: "0.3",
1895
+ stroke: "currentColor",
1896
+ strokeWidth: "1.5"
1897
+ }),
1898
+ /* @__PURE__ */ jsx("line", {
1899
+ x1: "8",
1900
+ y1: "8",
1901
+ x2: "24",
1902
+ y2: "8",
1903
+ stroke: "currentColor",
1904
+ strokeWidth: "1.5",
1905
+ strokeDasharray: "2 2"
1906
+ }),
1907
+ /* @__PURE__ */ jsx("circle", {
1908
+ cx: "28",
1909
+ cy: "8",
1910
+ r: "3",
1911
+ fill: "currentColor",
1912
+ fillOpacity: "0.3",
1913
+ stroke: "currentColor",
1914
+ strokeWidth: "1.5"
1915
+ })
1916
+ ]
1917
+ });
1918
+ if (type === "belongs-to") return /* @__PURE__ */ jsxs("svg", {
1919
+ width: "32",
1920
+ height: "16",
1921
+ viewBox: "0 0 32 16",
1922
+ fill: "none",
1923
+ className: "shrink-0",
1924
+ children: [
1925
+ /* @__PURE__ */ jsx("circle", {
1926
+ cx: "6",
1927
+ cy: "3",
1928
+ r: "2",
1929
+ fill: "currentColor"
1930
+ }),
1931
+ /* @__PURE__ */ jsx("circle", {
1932
+ cx: "4",
1933
+ cy: "8",
1934
+ r: "2",
1935
+ fill: "currentColor"
1936
+ }),
1937
+ /* @__PURE__ */ jsx("circle", {
1938
+ cx: "6",
1939
+ cy: "13",
1940
+ r: "2",
1941
+ fill: "currentColor"
1942
+ }),
1943
+ /* @__PURE__ */ jsx("line", {
1944
+ x1: "10",
1945
+ y1: "8",
1946
+ x2: "20",
1947
+ y2: "8",
1948
+ stroke: "currentColor",
1949
+ strokeWidth: "1.5"
1950
+ }),
1951
+ /* @__PURE__ */ jsx("path", {
1952
+ d: "M18 5L22 8L18 11",
1953
+ stroke: "currentColor",
1954
+ strokeWidth: "1.5",
1955
+ fill: "none",
1956
+ strokeLinecap: "round"
1957
+ }),
1958
+ /* @__PURE__ */ jsx("circle", {
1959
+ cx: "28",
1960
+ cy: "8",
1961
+ r: "3",
1962
+ fill: "currentColor",
1963
+ fillOpacity: "0.3",
1964
+ stroke: "currentColor",
1965
+ strokeWidth: "1.5"
1966
+ })
1967
+ ]
1968
+ });
1969
+ if (type === "one-to-many") return /* @__PURE__ */ jsxs("svg", {
1970
+ width: "32",
1971
+ height: "16",
1972
+ viewBox: "0 0 32 16",
1973
+ fill: "none",
1974
+ className: "shrink-0",
1975
+ children: [
1976
+ /* @__PURE__ */ jsx("circle", {
1977
+ cx: "4",
1978
+ cy: "8",
1979
+ r: "3",
1980
+ fill: "currentColor",
1981
+ fillOpacity: "0.3",
1982
+ stroke: "currentColor",
1983
+ strokeWidth: "1.5"
1984
+ }),
1985
+ /* @__PURE__ */ jsx("line", {
1986
+ x1: "8",
1987
+ y1: "8",
1988
+ x2: "20",
1989
+ y2: "8",
1990
+ stroke: "currentColor",
1991
+ strokeWidth: "1.5"
1992
+ }),
1993
+ /* @__PURE__ */ jsx("path", {
1994
+ d: "M18 5L22 8L18 11",
1995
+ stroke: "currentColor",
1996
+ strokeWidth: "1.5",
1997
+ fill: "none",
1998
+ strokeLinecap: "round"
1999
+ }),
2000
+ /* @__PURE__ */ jsx("circle", {
2001
+ cx: "26",
2002
+ cy: "3",
2003
+ r: "2",
2004
+ fill: "currentColor"
2005
+ }),
2006
+ /* @__PURE__ */ jsx("circle", {
2007
+ cx: "28",
2008
+ cy: "8",
2009
+ r: "2",
2010
+ fill: "currentColor"
2011
+ }),
2012
+ /* @__PURE__ */ jsx("circle", {
2013
+ cx: "26",
2014
+ cy: "13",
2015
+ r: "2",
2016
+ fill: "currentColor"
2017
+ })
2018
+ ]
2019
+ });
2020
+ return /* @__PURE__ */ jsxs("svg", {
2021
+ width: "32",
2022
+ height: "16",
2023
+ viewBox: "0 0 32 16",
2024
+ fill: "none",
2025
+ className: "shrink-0",
2026
+ children: [
2027
+ /* @__PURE__ */ jsx("circle", {
2028
+ cx: "4",
2029
+ cy: "3",
2030
+ r: "2",
2031
+ fill: "currentColor"
2032
+ }),
2033
+ /* @__PURE__ */ jsx("circle", {
2034
+ cx: "2",
2035
+ cy: "8",
2036
+ r: "2",
2037
+ fill: "currentColor"
2038
+ }),
2039
+ /* @__PURE__ */ jsx("circle", {
2040
+ cx: "4",
2041
+ cy: "13",
2042
+ r: "2",
2043
+ fill: "currentColor"
2044
+ }),
2045
+ /* @__PURE__ */ jsx("line", {
2046
+ x1: "8",
2047
+ y1: "8",
2048
+ x2: "24",
2049
+ y2: "8",
2050
+ stroke: "currentColor",
2051
+ strokeWidth: "1.5"
2052
+ }),
2053
+ /* @__PURE__ */ jsx("circle", {
2054
+ cx: "28",
2055
+ cy: "3",
2056
+ r: "2",
2057
+ fill: "currentColor"
2058
+ }),
2059
+ /* @__PURE__ */ jsx("circle", {
2060
+ cx: "30",
2061
+ cy: "8",
2062
+ r: "2",
2063
+ fill: "currentColor"
2064
+ }),
2065
+ /* @__PURE__ */ jsx("circle", {
2066
+ cx: "28",
2067
+ cy: "13",
2068
+ r: "2",
2069
+ fill: "currentColor"
2070
+ })
2071
+ ]
2072
+ });
2073
+ }
2074
+ function NoTableSelectedState() {
2075
+ return /* @__PURE__ */ jsxs("div", {
2076
+ className: "flex min-h-0 flex-1 flex-col items-center justify-center p-6",
2077
+ children: [
2078
+ /* @__PURE__ */ jsx("div", {
2079
+ className: "bg-muted/50 mb-4 flex h-16 w-16 items-center justify-center rounded-full",
2080
+ children: /* @__PURE__ */ jsx(Table2, { className: "text-muted-foreground h-8 w-8" })
2081
+ }),
2082
+ /* @__PURE__ */ jsx("h3", {
2083
+ className: "mb-1 text-balance text-lg font-semibold",
2084
+ children: "No table selected"
2085
+ }),
2086
+ /* @__PURE__ */ jsx("p", {
2087
+ className: "text-muted-foreground max-w-sm text-center text-sm",
2088
+ children: "Select a table from the sidebar to view and manage its relationships."
2089
+ })
2090
+ ]
2091
+ });
2092
+ }
2093
+ function RelationshipsView() {
2094
+ const [deleteTarget, setDeleteTarget] = useState(null);
2095
+ const [isDeleting, setIsDeleting] = useState(false);
2096
+ const stack = useCardStack();
2097
+ const { currentTable, currentSchema, currentDatabase } = useSchemaBuilderSelectors();
2098
+ const deleteForeignKeyMutation = useDeleteForeignKey();
2099
+ const tables = currentSchema?.tables || [];
2100
+ const databaseId = currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId ?? "";
2101
+ const { data: provisionsData } = useRelationProvisionsQuery({
2102
+ selection: {
2103
+ fields: {
2104
+ id: true,
2105
+ relationType: true,
2106
+ sourceTableId: true,
2107
+ targetTableId: true,
2108
+ outFieldId: true,
2109
+ outJunctionTableId: true,
2110
+ exposeInApi: true
2111
+ },
2112
+ where: databaseId ? { databaseId: { equalTo: databaseId } } : void 0
2113
+ },
2114
+ enabled: !!databaseId
2115
+ });
2116
+ const provisions = useMemo(() => {
2117
+ const nodes = provisionsData?.relationProvisions?.nodes;
2118
+ if (!nodes) return [];
2119
+ return nodes.map((n) => ({
2120
+ id: n.id,
2121
+ relationType: n.relationType ?? null,
2122
+ sourceTableId: n.sourceTableId ?? null,
2123
+ targetTableId: n.targetTableId ?? null,
2124
+ outFieldId: n.outFieldId ?? null,
2125
+ outJunctionTableId: n.outJunctionTableId ?? null,
2126
+ exposeInApi: n.exposeInApi ?? null
2127
+ }));
2128
+ }, [provisionsData]);
2129
+ const relationships = useMemo(() => getTableRelationships(currentTable, tables, provisions), [
2130
+ currentTable,
2131
+ tables,
2132
+ provisions
2133
+ ]);
2134
+ const handleOpenCreate = () => {
2135
+ if (!currentTable) return;
2136
+ stack.push({
2137
+ id: "relationship-new",
2138
+ title: "Create Relationship",
2139
+ Component: RelationshipCard,
2140
+ props: {
2141
+ editingRelationship: null,
2142
+ sourceTable: currentTable
2143
+ },
2144
+ width: 540
2145
+ });
2146
+ };
2147
+ const handleOpenEdit = (rel) => {
2148
+ const sourceTable = rel.sourceTable ?? currentTable;
2149
+ if (!sourceTable) return;
2150
+ stack.push({
2151
+ id: `relationship-edit-${rel.constraint.id}`,
2152
+ title: "Edit Relationship",
2153
+ Component: RelationshipCard,
2154
+ props: {
2155
+ editingRelationship: rel,
2156
+ sourceTable
2157
+ },
2158
+ width: 540
2159
+ });
2160
+ };
2161
+ const handleDeleteRequest = (constraintId, constraintName) => {
2162
+ setDeleteTarget({
2163
+ id: constraintId,
2164
+ name: constraintName
2165
+ });
2166
+ };
2167
+ const handleDeleteConfirm = async () => {
2168
+ if (!deleteTarget) return;
2169
+ setIsDeleting(true);
2170
+ try {
2171
+ await deleteForeignKeyMutation.mutateAsync({ id: deleteTarget.id });
2172
+ toast.success({ message: "Relationship deleted" });
2173
+ setDeleteTarget(null);
2174
+ } catch (error) {
2175
+ toast.error({
2176
+ message: "Failed to delete relationship",
2177
+ description: error instanceof Error ? error.message : "An error occurred"
2178
+ });
2179
+ } finally {
2180
+ setIsDeleting(false);
2181
+ }
2182
+ };
2183
+ if (!currentTable) return /* @__PURE__ */ jsx(NoTableSelectedState, {});
2184
+ const hasRelationships = relationships.length > 0;
2185
+ function renderRelationshipCard(rel) {
2186
+ const style = RELATIONSHIP_STYLES[rel.relationshipType];
2187
+ const isM2M = !!rel.withTableName;
2188
+ let junctionTargetFieldName;
2189
+ let targetPkFieldName;
2190
+ if (isM2M && rel.targetTable) {
2191
+ const junction = rel.targetTable;
2192
+ const actualTarget = tables.find((t) => t.name === rel.withTableName);
2193
+ if (actualTarget) {
2194
+ const junctionFk = (junction.constraints || []).find((c) => c.type === "foreign_key" && c.referencedTable === actualTarget.id);
2195
+ if (junctionFk) {
2196
+ junctionTargetFieldName = junction.fields.find((f) => f.id === junctionFk.fields[0])?.name;
2197
+ targetPkFieldName = actualTarget.fields.find((f) => f.id === junctionFk.referencedFields?.[0])?.name;
2198
+ }
2199
+ }
2200
+ }
2201
+ return /* @__PURE__ */ jsxs("div", {
2202
+ role: "button",
2203
+ tabIndex: 0,
2204
+ "data-testid": "relationship-row",
2205
+ onClick: () => handleOpenEdit(rel),
2206
+ onKeyDown: (event) => {
2207
+ handleActivationKeyDown(event, () => handleOpenEdit(rel));
2208
+ },
2209
+ className: cn(`group relative cursor-pointer rounded-xl border p-4 transition-[background-color,border-color,box-shadow,scale]
2210
+ duration-150 ease-out motion-safe:active:scale-[0.96]`, "border-border/60 hover:border-border/80 hover:bg-muted/30"),
2211
+ children: [
2212
+ /* @__PURE__ */ jsxs("div", {
2213
+ className: "flex items-center justify-between gap-2",
2214
+ children: [/* @__PURE__ */ jsxs("div", {
2215
+ className: "flex min-w-0 flex-wrap items-center gap-2",
2216
+ children: [
2217
+ /* @__PURE__ */ jsx(Link2, { className: "text-muted-foreground h-4 w-4 shrink-0" }),
2218
+ /* @__PURE__ */ jsx("p", {
2219
+ className: "truncate text-sm font-semibold",
2220
+ children: rel.constraint.name
2221
+ }),
2222
+ /* @__PURE__ */ jsx("span", {
2223
+ className: cn("shrink-0 rounded-full px-2 py-0.5 text-[10px] font-semibold tracking-wide uppercase", style.bg, style.text, "border", style.border),
2224
+ children: style.label
2225
+ })
2226
+ ]
2227
+ }), /* @__PURE__ */ jsx(Button, {
2228
+ variant: "ghost",
2229
+ size: "sm",
2230
+ "aria-label": "Delete relationship",
2231
+ className: "text-muted-foreground hover:text-destructive hover:bg-destructive/10 h-8 w-8 shrink-0 p-0",
2232
+ onClick: (e) => {
2233
+ e.stopPropagation();
2234
+ handleDeleteRequest(rel.constraint.id, rel.constraint.name || "this relationship");
2235
+ },
2236
+ children: /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" })
2237
+ })]
2238
+ }),
2239
+ isM2M ? /* @__PURE__ */ jsxs("div", {
2240
+ className: "mt-3 space-y-2",
2241
+ children: [/* @__PURE__ */ jsxs("div", {
2242
+ className: "flex items-center gap-2",
2243
+ children: [
2244
+ /* @__PURE__ */ jsxs("div", {
2245
+ className: "bg-background truncate rounded-md border px-3 py-1.5 font-mono text-sm",
2246
+ children: [
2247
+ /* @__PURE__ */ jsx("span", {
2248
+ className: "text-foreground",
2249
+ children: rel.sourceTable?.name || "?"
2250
+ }),
2251
+ /* @__PURE__ */ jsx("span", {
2252
+ className: "text-muted-foreground",
2253
+ children: "."
2254
+ }),
2255
+ /* @__PURE__ */ jsx("span", {
2256
+ className: style.text,
2257
+ children: rel.sourceFieldName || "?"
2258
+ })
2259
+ ]
2260
+ }),
2261
+ /* @__PURE__ */ jsx("span", {
2262
+ className: style.text,
2263
+ children: /* @__PURE__ */ jsx(MiniConnector, { type: "many-to-many" })
2264
+ }),
2265
+ /* @__PURE__ */ jsx("div", {
2266
+ className: "bg-muted/50 truncate rounded-md border border-dashed px-3 py-1.5 font-mono text-sm",
2267
+ children: /* @__PURE__ */ jsx("span", {
2268
+ className: "text-muted-foreground",
2269
+ children: rel.targetTable?.name || "?"
2270
+ })
2271
+ }),
2272
+ /* @__PURE__ */ jsx("span", {
2273
+ className: style.text,
2274
+ children: /* @__PURE__ */ jsx(MiniConnector, { type: "many-to-many" })
2275
+ }),
2276
+ /* @__PURE__ */ jsxs("div", {
2277
+ className: "bg-background truncate rounded-md border px-3 py-1.5 font-mono text-sm",
2278
+ children: [
2279
+ /* @__PURE__ */ jsx("span", {
2280
+ className: "text-foreground",
2281
+ children: rel.withTableName
2282
+ }),
2283
+ /* @__PURE__ */ jsx("span", {
2284
+ className: "text-muted-foreground",
2285
+ children: "."
2286
+ }),
2287
+ /* @__PURE__ */ jsx("span", {
2288
+ className: style.text,
2289
+ children: targetPkFieldName || "id"
2290
+ })
2291
+ ]
2292
+ })
2293
+ ]
2294
+ }), /* @__PURE__ */ jsxs("div", {
2295
+ className: "text-muted-foreground flex items-center gap-1.5 pl-1 font-mono text-[10px]",
2296
+ children: [
2297
+ /* @__PURE__ */ jsx("span", { children: "via" }),
2298
+ /* @__PURE__ */ jsx("span", {
2299
+ className: "bg-muted rounded px-1.5 py-0.5",
2300
+ children: rel.targetFieldName || "?"
2301
+ }),
2302
+ /* @__PURE__ */ jsx("span", { children: "&" }),
2303
+ /* @__PURE__ */ jsx("span", {
2304
+ className: "bg-muted rounded px-1.5 py-0.5",
2305
+ children: junctionTargetFieldName || "?"
2306
+ })
2307
+ ]
2308
+ })]
2309
+ }) : /* @__PURE__ */ jsxs("div", {
2310
+ className: "mt-3 flex items-center gap-3",
2311
+ children: [
2312
+ /* @__PURE__ */ jsxs("div", {
2313
+ className: "bg-background truncate rounded-md border px-3 py-1.5 font-mono text-sm",
2314
+ children: [
2315
+ /* @__PURE__ */ jsx("span", {
2316
+ className: "text-foreground",
2317
+ children: rel.sourceTable?.name || "?"
2318
+ }),
2319
+ /* @__PURE__ */ jsx("span", {
2320
+ className: "text-muted-foreground",
2321
+ children: "."
2322
+ }),
2323
+ /* @__PURE__ */ jsx("span", {
2324
+ className: style.text,
2325
+ children: rel.sourceFieldName || "?"
2326
+ })
2327
+ ]
2328
+ }),
2329
+ /* @__PURE__ */ jsx("span", {
2330
+ className: style.text,
2331
+ children: /* @__PURE__ */ jsx(MiniConnector, { type: rel.relationshipType })
2332
+ }),
2333
+ /* @__PURE__ */ jsxs("div", {
2334
+ className: "bg-background truncate rounded-md border px-3 py-1.5 font-mono text-sm",
2335
+ children: [
2336
+ /* @__PURE__ */ jsx("span", {
2337
+ className: "text-foreground",
2338
+ children: rel.targetTable?.name || "?"
2339
+ }),
2340
+ /* @__PURE__ */ jsx("span", {
2341
+ className: "text-muted-foreground",
2342
+ children: "."
2343
+ }),
2344
+ /* @__PURE__ */ jsx("span", {
2345
+ className: style.text,
2346
+ children: rel.targetFieldName || "?"
2347
+ })
2348
+ ]
2349
+ })
2350
+ ]
2351
+ }),
2352
+ /* @__PURE__ */ jsx("div", {
2353
+ className: "mt-3 flex items-center justify-between gap-4 border-t pt-3",
2354
+ children: /* @__PURE__ */ jsxs("div", {
2355
+ className: "text-muted-foreground flex items-center gap-1.5 text-xs",
2356
+ children: [
2357
+ /* @__PURE__ */ jsx(CornerDownRight, { className: "h-3 w-3" }),
2358
+ /* @__PURE__ */ jsx("span", { children: "On Delete:" }),
2359
+ /* @__PURE__ */ jsx("span", {
2360
+ className: "text-foreground/70 font-medium",
2361
+ children: ForeignKeyActionLabels[rel.constraint.onDelete ?? ForeignKeyActions.NO_ACTION]
2362
+ })
2363
+ ]
2364
+ })
2365
+ })
2366
+ ]
2367
+ }, rel.constraint.id);
2368
+ }
2369
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", {
2370
+ "data-chat-component": "relationships-view",
2371
+ "data-chat-relationship-count": String(relationships.length),
2372
+ className: "flex min-h-0 flex-1 flex-col overflow-auto p-6",
2373
+ children: !hasRelationships ? /* @__PURE__ */ jsx(RelationshipEmptyState, {
2374
+ onCreateClick: handleOpenCreate,
2375
+ tableName: currentTable.name
2376
+ }) : /* @__PURE__ */ jsxs("div", {
2377
+ className: "mx-auto w-full min-w-80 max-w-4xl space-y-6",
2378
+ children: [/* @__PURE__ */ jsxs("div", {
2379
+ className: "flex items-center justify-between gap-4",
2380
+ children: [/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h2", {
2381
+ className: "text-balance text-xl font-semibold tracking-tight",
2382
+ children: "Relationships"
2383
+ }), /* @__PURE__ */ jsxs("p", {
2384
+ className: "text-muted-foreground mt-1 text-sm",
2385
+ children: [
2386
+ relationships.length,
2387
+ " connection",
2388
+ relationships.length !== 1 ? "s" : "",
2389
+ " for",
2390
+ " ",
2391
+ /* @__PURE__ */ jsx("code", {
2392
+ className: "bg-muted rounded px-1.5 py-0.5 font-mono text-xs",
2393
+ children: currentTable.name
2394
+ })
2395
+ ]
2396
+ })] }), /* @__PURE__ */ jsxs(Button, {
2397
+ onClick: handleOpenCreate,
2398
+ children: [/* @__PURE__ */ jsx(Link2, { className: "mr-2 h-4 w-4" }), "Add Relationship"]
2399
+ })]
2400
+ }), /* @__PURE__ */ jsx("div", {
2401
+ className: "space-y-3",
2402
+ children: relationships.map(renderRelationshipCard)
2403
+ })]
2404
+ })
2405
+ }), /* @__PURE__ */ jsx(DeleteConfirmDialog, {
2406
+ entityType: "Relationship",
2407
+ entityName: deleteTarget?.name ?? null,
2408
+ open: !!deleteTarget,
2409
+ onOpenChange: () => setDeleteTarget(null),
2410
+ onConfirm: handleDeleteConfirm,
2411
+ isPending: isDeleting
2412
+ })] });
2413
+ }
2414
+
2415
+ //#endregion
2416
+ export { useUpdateForeignKey as a, useDeleteForeignKey as i, useCreateForeignKey as n, useRelationProvision as o, useCreateManyToMany as r, RelationshipsView as t };
2417
+ //# sourceMappingURL=relationships-view-C884EvGj.js.map