@constructive-io/schema-builder 0.2.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.
- package/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/core.cjs +213 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +54 -0
- package/dist/core.d.ts +54 -0
- package/dist/core.js +202 -0
- package/dist/core.js.map +1 -0
- package/dist/fields.cjs +7764 -0
- package/dist/fields.cjs.map +1 -0
- package/dist/fields.d.cts +49 -0
- package/dist/fields.d.ts +49 -0
- package/dist/fields.js +7737 -0
- package/dist/fields.js.map +1 -0
- package/dist/generated-types-CnAlqHU_.d.cts +25 -0
- package/dist/generated-types-CnAlqHU_.d.ts +25 -0
- package/dist/index.cjs +13752 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +13720 -0
- package/dist/index.js.map +1 -0
- package/dist/indexes.cjs +1043 -0
- package/dist/indexes.cjs.map +1 -0
- package/dist/indexes.d.cts +36 -0
- package/dist/indexes.d.ts +36 -0
- package/dist/indexes.js +1018 -0
- package/dist/indexes.js.map +1 -0
- package/dist/policies.cjs +5940 -0
- package/dist/policies.cjs.map +1 -0
- package/dist/policies.d.cts +103 -0
- package/dist/policies.d.ts +103 -0
- package/dist/policies.js +5913 -0
- package/dist/policies.js.map +1 -0
- package/dist/policy-types-CEYAR5TN.d.cts +79 -0
- package/dist/policy-types-CEYAR5TN.d.ts +79 -0
- package/dist/relationships.cjs +4329 -0
- package/dist/relationships.cjs.map +1 -0
- package/dist/relationships.d.cts +121 -0
- package/dist/relationships.d.ts +121 -0
- package/dist/relationships.js +4302 -0
- package/dist/relationships.js.map +1 -0
- package/dist/tables.cjs +5645 -0
- package/dist/tables.cjs.map +1 -0
- package/dist/tables.d.cts +29 -0
- package/dist/tables.d.ts +29 -0
- package/dist/tables.js +5620 -0
- package/dist/tables.js.map +1 -0
- package/dist/testing.cjs +69 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +7 -0
- package/dist/testing.d.ts +7 -0
- package/dist/testing.js +67 -0
- package/dist/testing.js.map +1 -0
- package/dist/types-Du86atZ8.d.cts +199 -0
- package/dist/types-Du86atZ8.d.ts +199 -0
- package/dist/types-bX2dCrdl.d.cts +52 -0
- package/dist/types-bX2dCrdl.d.ts +52 -0
- package/package.json +165 -0
- package/src/styles.css +2 -0
package/dist/indexes.js
ADDED
|
@@ -0,0 +1,1018 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createContext, useState, useCallback, useMemo, useTransition, useEffect, useContext } from 'react';
|
|
3
|
+
import { Button } from '@constructive-io/ui/button';
|
|
4
|
+
import { useCardStack } from '@constructive-io/ui/stack';
|
|
5
|
+
import { toast } from '@constructive-io/ui/toast';
|
|
6
|
+
import { Tooltip, TooltipTrigger, TooltipContent } from '@constructive-io/ui/tooltip';
|
|
7
|
+
import { Plus, ListChecks, Trash2, Table2, Zap, Search, Shield, Loader2, DatabaseZap, Database } from 'lucide-react';
|
|
8
|
+
import * as FluentContextSelector from '@fluentui/react-context-selector';
|
|
9
|
+
import { useStore } from 'zustand';
|
|
10
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
11
|
+
import { useQueryClient, useMutation } from '@tanstack/react-query';
|
|
12
|
+
import { clsx } from 'clsx';
|
|
13
|
+
import { twMerge } from 'tailwind-merge';
|
|
14
|
+
import { AlertDialog, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter, AlertDialogCancel, AlertDialogAction } from '@constructive-io/ui/alert-dialog';
|
|
15
|
+
import { Input } from '@constructive-io/ui/input';
|
|
16
|
+
import { Label } from '@constructive-io/ui/label';
|
|
17
|
+
import { MultiSelect } from '@constructive-io/ui/multi-select';
|
|
18
|
+
import { ResponsiveDiagram } from '@constructive-io/ui/responsive-diagram';
|
|
19
|
+
import { ScrollArea } from '@constructive-io/ui/scroll-area';
|
|
20
|
+
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from '@constructive-io/ui/select';
|
|
21
|
+
import { Switch } from '@constructive-io/ui/switch';
|
|
22
|
+
|
|
23
|
+
var INDEX_TYPE_LABELS = {
|
|
24
|
+
btree: "B-Tree",
|
|
25
|
+
hash: "Hash",
|
|
26
|
+
gin: "GIN",
|
|
27
|
+
gist: "GiST",
|
|
28
|
+
spgist: "SP-GiST",
|
|
29
|
+
brin: "BRIN"
|
|
30
|
+
};
|
|
31
|
+
var SchemaBuilderContext = createContext(null);
|
|
32
|
+
function useSchemaBuilder() {
|
|
33
|
+
const value = useContext(SchemaBuilderContext);
|
|
34
|
+
if (!value) throw new Error("SchemaBuilderProvider is missing");
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
function useSchemaBuilderStore(selector) {
|
|
38
|
+
const { store } = useSchemaBuilder();
|
|
39
|
+
return useStore(store, selector);
|
|
40
|
+
}
|
|
41
|
+
var featureByMutation = {
|
|
42
|
+
createField: "fields",
|
|
43
|
+
createForeignKeyConstraint: "relationships",
|
|
44
|
+
createIndex: "indexes",
|
|
45
|
+
createPrimaryKeyConstraint: "fields",
|
|
46
|
+
createTableGrant: "policies",
|
|
47
|
+
createTable: "policies",
|
|
48
|
+
createUniqueConstraint: "fields",
|
|
49
|
+
deleteField: "fields",
|
|
50
|
+
deleteForeignKeyConstraint: "relationships",
|
|
51
|
+
deleteIndex: "indexes",
|
|
52
|
+
deletePolicy: "policies",
|
|
53
|
+
deletePrimaryKeyConstraint: "fields",
|
|
54
|
+
deleteTable: "tables",
|
|
55
|
+
deleteUniqueConstraint: "fields",
|
|
56
|
+
updateField: "fields",
|
|
57
|
+
updateForeignKeyConstraint: "relationships",
|
|
58
|
+
updateIndex: "indexes",
|
|
59
|
+
updatePolicy: "policies",
|
|
60
|
+
updatePrimaryKeyConstraint: "fields",
|
|
61
|
+
updateTable: "tables"
|
|
62
|
+
};
|
|
63
|
+
function useGeneratedMutation(operation, options) {
|
|
64
|
+
const { adapter, scope, onInvalidate } = useSchemaBuilder();
|
|
65
|
+
const feature = featureByMutation[operation];
|
|
66
|
+
const operationFn = adapter[feature][operation];
|
|
67
|
+
const selection = options?.selection?.fields;
|
|
68
|
+
return useMutation({
|
|
69
|
+
mutationFn: (variables) => operationFn(variables, { scope, selection }),
|
|
70
|
+
onSuccess: () => onInvalidate?.({ scope, feature, operation })
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
var apiKeys = { all: ["schema-builder", "apis"] };
|
|
74
|
+
var domainKeys = {
|
|
75
|
+
all: ["schema-builder", "domains"],
|
|
76
|
+
lists: () => ["schema-builder", "domains", "list"]
|
|
77
|
+
};
|
|
78
|
+
var schemaKeys = { all: ["schema-builder", "schemas"] };
|
|
79
|
+
var siteKeys = { all: ["schema-builder", "sites"] };
|
|
80
|
+
var useCreateIndexMutation = (options) => useGeneratedMutation("createIndex", options);
|
|
81
|
+
var useDeleteIndexMutation = (options) => useGeneratedMutation("deleteIndex", options);
|
|
82
|
+
var useUpdateIndexMutation = (options) => useGeneratedMutation("updateIndex", options);
|
|
83
|
+
|
|
84
|
+
// src/compat/block-config.tsx
|
|
85
|
+
function useSchemaBuilderRuntime() {
|
|
86
|
+
const runtime = useSchemaBuilder();
|
|
87
|
+
return {
|
|
88
|
+
...runtime,
|
|
89
|
+
setCurrentDatabaseApi: (api) => runtime.store.getState().setCurrentDatabaseApi(api)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function useSchemaBuilderRuntimeStore(selector) {
|
|
93
|
+
return useSchemaBuilderStore(selector);
|
|
94
|
+
}
|
|
95
|
+
var accessibleDatabasesQueryKeys = {
|
|
96
|
+
all: ["accessible-databases"]};
|
|
97
|
+
var databaseConstraintsQueryKeys = {
|
|
98
|
+
all: ["database-constraints"]};
|
|
99
|
+
|
|
100
|
+
// src/schema/schema-builder-core/lib/gql/hooks/schema-builder/use-schema-builder-selectors.ts
|
|
101
|
+
var fluentContextSelector = FluentContextSelector.default ?? FluentContextSelector;
|
|
102
|
+
var { createContext: createContext2, useContextSelector } = fluentContextSelector;
|
|
103
|
+
var EMPTY_STORE_SCHEMAS = [];
|
|
104
|
+
var EMPTY_SCHEMAS = EMPTY_STORE_SCHEMAS;
|
|
105
|
+
var NOOP_SELECT_ORG = (_orgId) => void 0;
|
|
106
|
+
var NOOP_SELECT_SCHEMA = (_schemaKey) => void 0;
|
|
107
|
+
var NOOP_SET_ACTIVE_TAB = (_tab) => void 0;
|
|
108
|
+
var IS_CUSTOM_SCHEMA = (schemaKey) => schemaKey.startsWith("custom-");
|
|
109
|
+
var SchemaBuilderDataContext = createContext2(null);
|
|
110
|
+
function useSchemaBuilderDataSelector(selector) {
|
|
111
|
+
return useContextSelector(SchemaBuilderDataContext, (state) => {
|
|
112
|
+
if (!state) {
|
|
113
|
+
throw new Error("SchemaBuilderDataProvider is missing");
|
|
114
|
+
}
|
|
115
|
+
return selector(state);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function useSchemaBuilderSelectors() {
|
|
119
|
+
const {
|
|
120
|
+
scope,
|
|
121
|
+
store,
|
|
122
|
+
selectField: selectFieldFromContext,
|
|
123
|
+
selectTable: selectTableFromContext
|
|
124
|
+
} = useSchemaBuilderRuntime();
|
|
125
|
+
const selectedFieldId = useSchemaBuilderRuntimeStore((state) => state.selectedFieldId);
|
|
126
|
+
const currentDatabaseApi = useSchemaBuilderRuntimeStore((state) => state.currentDatabaseApi);
|
|
127
|
+
const selectedOrgId = scope.orgId;
|
|
128
|
+
const activeTab = "schemas";
|
|
129
|
+
const customSchemas = EMPTY_SCHEMAS;
|
|
130
|
+
const selectOrg = NOOP_SELECT_ORG;
|
|
131
|
+
const selectSchema = NOOP_SELECT_SCHEMA;
|
|
132
|
+
const selectField = useCallback(
|
|
133
|
+
(fieldId) => selectFieldFromContext(fieldId),
|
|
134
|
+
[selectFieldFromContext]
|
|
135
|
+
);
|
|
136
|
+
const clearAllSelections = useCallback(() => {
|
|
137
|
+
selectTableFromContext(null);
|
|
138
|
+
selectFieldFromContext(null);
|
|
139
|
+
}, [selectTableFromContext, selectFieldFromContext]);
|
|
140
|
+
const setActiveTab = NOOP_SET_ACTIVE_TAB;
|
|
141
|
+
const setCurrentDatabaseApi = useCallback(
|
|
142
|
+
(api) => store.getState().setCurrentDatabaseApi(api),
|
|
143
|
+
[store]
|
|
144
|
+
);
|
|
145
|
+
const availableSchemas = useSchemaBuilderDataSelector((state) => state.availableSchemas);
|
|
146
|
+
const selectedSchemaKey = useSchemaBuilderDataSelector((state) => state.selectedSchemaKey);
|
|
147
|
+
const currentSchemaInfo = useSchemaBuilderDataSelector((state) => state.currentSchemaInfo);
|
|
148
|
+
const currentSchema = useSchemaBuilderDataSelector((state) => state.currentSchema);
|
|
149
|
+
const currentTable = useSchemaBuilderDataSelector((state) => state.currentTable);
|
|
150
|
+
const selectedTableId = useSchemaBuilderDataSelector((state) => state.selectedTableId);
|
|
151
|
+
const isLoading = useSchemaBuilderDataSelector((state) => state.isLoading);
|
|
152
|
+
const isFetching = useSchemaBuilderDataSelector((state) => state.isFetching);
|
|
153
|
+
const error = useSchemaBuilderDataSelector((state) => state.error);
|
|
154
|
+
const refetch = useSchemaBuilderDataSelector((state) => state.refetch);
|
|
155
|
+
const selectTableFromProvider = useSchemaBuilderDataSelector((state) => state.selectTable);
|
|
156
|
+
const currentField = useMemo(
|
|
157
|
+
() => currentTable?.fields.find((f) => f.id === selectedFieldId) ?? null,
|
|
158
|
+
[currentTable, selectedFieldId]
|
|
159
|
+
);
|
|
160
|
+
const currentDatabase = useMemo(() => {
|
|
161
|
+
if (!currentSchemaInfo?.databaseInfo) return null;
|
|
162
|
+
return {
|
|
163
|
+
schemaKey: selectedSchemaKey,
|
|
164
|
+
databaseId: currentSchemaInfo.databaseInfo.id,
|
|
165
|
+
name: currentSchemaInfo.databaseInfo.name,
|
|
166
|
+
label: currentSchemaInfo.databaseInfo.label ?? null,
|
|
167
|
+
schemaId: currentSchemaInfo.databaseInfo.schemaId ?? null
|
|
168
|
+
};
|
|
169
|
+
}, [currentSchemaInfo, selectedSchemaKey]);
|
|
170
|
+
const isCustomSchema = IS_CUSTOM_SCHEMA;
|
|
171
|
+
const getSchemaByKey = useCallback(
|
|
172
|
+
(key) => availableSchemas.find((schema) => schema.key === key) ?? null,
|
|
173
|
+
[availableSchemas]
|
|
174
|
+
);
|
|
175
|
+
const selectTable = useCallback(
|
|
176
|
+
(tableId, tableName) => {
|
|
177
|
+
selectTableFromProvider(tableId, tableName);
|
|
178
|
+
selectField(null);
|
|
179
|
+
},
|
|
180
|
+
[selectTableFromProvider, selectField]
|
|
181
|
+
);
|
|
182
|
+
return {
|
|
183
|
+
// Entity selection state
|
|
184
|
+
selectedOrgId,
|
|
185
|
+
selectedSchemaKey,
|
|
186
|
+
selectedTableId,
|
|
187
|
+
selectedFieldId,
|
|
188
|
+
activeTab,
|
|
189
|
+
// Entity selection actions (with cascade clearing)
|
|
190
|
+
selectOrg,
|
|
191
|
+
selectSchema,
|
|
192
|
+
selectTable,
|
|
193
|
+
selectField,
|
|
194
|
+
clearAllSelections,
|
|
195
|
+
setActiveTab,
|
|
196
|
+
// Derived data
|
|
197
|
+
availableSchemas,
|
|
198
|
+
currentSchema,
|
|
199
|
+
currentTable,
|
|
200
|
+
currentField,
|
|
201
|
+
currentDatabase,
|
|
202
|
+
// API state (from Zustand)
|
|
203
|
+
currentDatabaseApi,
|
|
204
|
+
setCurrentDatabaseApi,
|
|
205
|
+
// Helpers
|
|
206
|
+
isCustomSchema,
|
|
207
|
+
getSchemaByKey,
|
|
208
|
+
// Loading/error
|
|
209
|
+
isLoading,
|
|
210
|
+
isFetching,
|
|
211
|
+
error,
|
|
212
|
+
refetch,
|
|
213
|
+
// Custom schemas
|
|
214
|
+
customSchemas
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/schema/schema-builder-core/lib/data/mutation-input.ts
|
|
219
|
+
var asFieldIds = (ids) => ids;
|
|
220
|
+
|
|
221
|
+
// src/schema/schema-builder-core/lib/gql/hooks/dashboard/dashboard-query-keys.ts
|
|
222
|
+
function isDashboardCacheScopeKey(value) {
|
|
223
|
+
if (!value || typeof value !== "object") return false;
|
|
224
|
+
const candidate = value;
|
|
225
|
+
return "databaseId" in candidate && "endpoint" in candidate;
|
|
226
|
+
}
|
|
227
|
+
var blueprintTemplateKeys = { all: ["schema-builder", "blueprint-templates"] };
|
|
228
|
+
var databasePoliciesQueryKeys = {
|
|
229
|
+
all: ["database-policies"],
|
|
230
|
+
byDatabase: (databaseId) => ["database-policies", databaseId]
|
|
231
|
+
};
|
|
232
|
+
var databaseTablesQueryKeys = {
|
|
233
|
+
all: ["database-tables"],
|
|
234
|
+
byDatabase: (databaseId) => [...databaseTablesQueryKeys.all, { databaseId }],
|
|
235
|
+
byDatabaseWithOptions: (databaseId, options) => [...databaseTablesQueryKeys.byDatabase(databaseId), options]
|
|
236
|
+
};
|
|
237
|
+
var userDatabasesQueryKeys = {
|
|
238
|
+
all: ["user-databases"]};
|
|
239
|
+
|
|
240
|
+
// src/schema/schema-builder-core/lib/gql/hooks/schema-builder/modules/invalidate-database-entities.ts
|
|
241
|
+
var invalidateQueryKey = (queryClient, queryKey) => queryClient.invalidateQueries({ queryKey });
|
|
242
|
+
function invalidateDashboardQueries(queryClient, databaseId) {
|
|
243
|
+
const matchesDashboard = (queryKey) => {
|
|
244
|
+
const [root, scope] = queryKey;
|
|
245
|
+
if (root !== "sheets") return false;
|
|
246
|
+
if (!databaseId) return true;
|
|
247
|
+
return isDashboardCacheScopeKey(scope) && scope.databaseId === databaseId;
|
|
248
|
+
};
|
|
249
|
+
queryClient.removeQueries({
|
|
250
|
+
predicate: (query) => {
|
|
251
|
+
if (!matchesDashboard(query.queryKey)) return false;
|
|
252
|
+
const type = query.queryKey[2];
|
|
253
|
+
return type === "meta" || type === "relations";
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
return queryClient.invalidateQueries({
|
|
257
|
+
predicate: (query) => matchesDashboard(query.queryKey)
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
async function invalidateDatabaseEntities(queryClient, databaseId) {
|
|
261
|
+
const tasks = [];
|
|
262
|
+
tasks.push(invalidateQueryKey(queryClient, ["@constructive-io/schema-builder"]));
|
|
263
|
+
if (databaseId) {
|
|
264
|
+
tasks.push(invalidateQueryKey(queryClient, databaseTablesQueryKeys.byDatabase(databaseId)));
|
|
265
|
+
tasks.push(invalidateQueryKey(queryClient, databasePoliciesQueryKeys.byDatabase(databaseId)));
|
|
266
|
+
tasks.push(invalidateQueryKey(queryClient, domainKeys.lists()));
|
|
267
|
+
tasks.push(invalidateQueryKey(queryClient, ["database-table"]));
|
|
268
|
+
}
|
|
269
|
+
tasks.push(invalidateDashboardQueries(queryClient, databaseId));
|
|
270
|
+
tasks.push(invalidateQueryKey(queryClient, userDatabasesQueryKeys.all));
|
|
271
|
+
tasks.push(invalidateQueryKey(queryClient, accessibleDatabasesQueryKeys.all));
|
|
272
|
+
tasks.push(invalidateQueryKey(queryClient, databaseConstraintsQueryKeys.all));
|
|
273
|
+
tasks.push(invalidateQueryKey(queryClient, apiKeys.all));
|
|
274
|
+
tasks.push(invalidateQueryKey(queryClient, schemaKeys.all));
|
|
275
|
+
tasks.push(invalidateQueryKey(queryClient, siteKeys.all));
|
|
276
|
+
tasks.push(invalidateQueryKey(queryClient, blueprintTemplateKeys.all));
|
|
277
|
+
tasks.push(invalidateQueryKey(queryClient, databaseTablesQueryKeys.all));
|
|
278
|
+
tasks.push(invalidateQueryKey(queryClient, databasePoliciesQueryKeys.all));
|
|
279
|
+
tasks.push(invalidateQueryKey(queryClient, domainKeys.all));
|
|
280
|
+
await Promise.allSettled(tasks);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// src/schema/schema-builder-indexes/lib/gql/hooks/schema-builder/use-index-mutations.ts
|
|
284
|
+
function useCreateIndex() {
|
|
285
|
+
const queryClient = useQueryClient();
|
|
286
|
+
const { selectTable, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
|
|
287
|
+
const createIndexMutation = useCreateIndexMutation({ selection: { fields: { id: true, name: true } } });
|
|
288
|
+
return useMutation({
|
|
289
|
+
mutationFn: async (input) => {
|
|
290
|
+
const databaseId = currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId;
|
|
291
|
+
if (!databaseId) {
|
|
292
|
+
throw new Error("Database ID not found");
|
|
293
|
+
}
|
|
294
|
+
const result = await createIndexMutation.mutateAsync({
|
|
295
|
+
databaseId,
|
|
296
|
+
tableId: input.tableId,
|
|
297
|
+
name: input.name,
|
|
298
|
+
fieldIds: asFieldIds(input.fieldIds),
|
|
299
|
+
isUnique: input.isUnique ?? false,
|
|
300
|
+
accessMethod: input.accessMethod || "btree"
|
|
301
|
+
});
|
|
302
|
+
const createdIndex = result.createIndex?.index;
|
|
303
|
+
if (!createdIndex?.id) {
|
|
304
|
+
throw new Error("Failed to create index");
|
|
305
|
+
}
|
|
306
|
+
return { id: createdIndex.id, name: createdIndex.name ?? "" };
|
|
307
|
+
},
|
|
308
|
+
onSuccess: async (_createdIndex, variables) => {
|
|
309
|
+
await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
|
|
310
|
+
selectTable(variables.tableId);
|
|
311
|
+
},
|
|
312
|
+
onError: (error) => {
|
|
313
|
+
console.error("Failed to create index:", error);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
function useUpdateIndex() {
|
|
318
|
+
const queryClient = useQueryClient();
|
|
319
|
+
const { selectTable, selectedTableId, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
|
|
320
|
+
const updateIndexMutation = useUpdateIndexMutation({ selection: { fields: { id: true, name: true } } });
|
|
321
|
+
return useMutation({
|
|
322
|
+
mutationFn: async (input) => {
|
|
323
|
+
const patch = {};
|
|
324
|
+
if (input.name !== void 0) patch.name = input.name;
|
|
325
|
+
if (input.fieldIds !== void 0) patch.fieldIds = input.fieldIds;
|
|
326
|
+
if (input.isUnique !== void 0) patch.isUnique = input.isUnique;
|
|
327
|
+
if (input.accessMethod !== void 0) patch.accessMethod = input.accessMethod;
|
|
328
|
+
const result = await updateIndexMutation.mutateAsync({ id: input.id, indexPatch: patch });
|
|
329
|
+
const updatedIndex = result.updateIndex?.index;
|
|
330
|
+
if (!updatedIndex?.id) {
|
|
331
|
+
throw new Error("Failed to update index");
|
|
332
|
+
}
|
|
333
|
+
return { id: updatedIndex.id, name: updatedIndex.name ?? "" };
|
|
334
|
+
},
|
|
335
|
+
onSuccess: async () => {
|
|
336
|
+
await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
|
|
337
|
+
if (selectedTableId) {
|
|
338
|
+
selectTable(selectedTableId);
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
onError: (error) => {
|
|
342
|
+
console.error("Failed to update index:", error);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
function useDeleteIndex() {
|
|
347
|
+
const queryClient = useQueryClient();
|
|
348
|
+
const { selectTable, selectedTableId, currentDatabase, currentSchema } = useSchemaBuilderSelectors();
|
|
349
|
+
const deleteIndexMutation = useDeleteIndexMutation({ selection: { fields: { id: true } } });
|
|
350
|
+
return useMutation({
|
|
351
|
+
mutationFn: async (input) => {
|
|
352
|
+
await deleteIndexMutation.mutateAsync({ id: input.id });
|
|
353
|
+
return { id: input.id };
|
|
354
|
+
},
|
|
355
|
+
onSuccess: async () => {
|
|
356
|
+
await invalidateDatabaseEntities(queryClient, currentDatabase?.databaseId ?? currentSchema?.metadata?.databaseId);
|
|
357
|
+
if (selectedTableId) {
|
|
358
|
+
selectTable(selectedTableId);
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
onError: (error) => {
|
|
362
|
+
console.error("Failed to delete index:", error);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/schema/schema-builder-core/lib/stack/card-widths.ts
|
|
368
|
+
var CARD_WIDTHS = {
|
|
369
|
+
/** Default for most forms (480px) */
|
|
370
|
+
medium: 480};
|
|
371
|
+
|
|
372
|
+
// src/schema/schema-builder-core/lib/keyboard-activation.ts
|
|
373
|
+
function handleActivationKeyDown(event, activate, disabled = false) {
|
|
374
|
+
if (event.target !== event.currentTarget || disabled) return;
|
|
375
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
376
|
+
event.preventDefault();
|
|
377
|
+
activate();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function cn(...inputs) {
|
|
381
|
+
return twMerge(clsx(inputs));
|
|
382
|
+
}
|
|
383
|
+
function DeleteConfirmDialog({
|
|
384
|
+
entityType,
|
|
385
|
+
entityName,
|
|
386
|
+
open,
|
|
387
|
+
onOpenChange,
|
|
388
|
+
onConfirm,
|
|
389
|
+
isPending
|
|
390
|
+
}) {
|
|
391
|
+
return /* @__PURE__ */ jsx(AlertDialog, { open, onOpenChange: (next) => !next && !isPending && onOpenChange(false), children: /* @__PURE__ */ jsxs(AlertDialogContent, { children: [
|
|
392
|
+
/* @__PURE__ */ jsxs(AlertDialogHeader, { children: [
|
|
393
|
+
/* @__PURE__ */ jsxs(AlertDialogTitle, { children: [
|
|
394
|
+
"Delete ",
|
|
395
|
+
entityType
|
|
396
|
+
] }),
|
|
397
|
+
/* @__PURE__ */ jsxs(AlertDialogDescription, { children: [
|
|
398
|
+
"Are you sure you want to delete",
|
|
399
|
+
" ",
|
|
400
|
+
/* @__PURE__ */ jsxs("strong", { className: "text-foreground", children: [
|
|
401
|
+
"\u201C",
|
|
402
|
+
entityName,
|
|
403
|
+
"\u201D"
|
|
404
|
+
] }),
|
|
405
|
+
"? This action cannot be undone."
|
|
406
|
+
] })
|
|
407
|
+
] }),
|
|
408
|
+
/* @__PURE__ */ jsxs(AlertDialogFooter, { children: [
|
|
409
|
+
/* @__PURE__ */ jsx(AlertDialogCancel, { disabled: isPending, children: "Cancel" }),
|
|
410
|
+
/* @__PURE__ */ jsxs(
|
|
411
|
+
AlertDialogAction,
|
|
412
|
+
{
|
|
413
|
+
onClick: onConfirm,
|
|
414
|
+
disabled: isPending,
|
|
415
|
+
className: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
|
|
416
|
+
children: [
|
|
417
|
+
isPending ? /* @__PURE__ */ jsx(Loader2, { className: "mr-2 h-4 w-4 animate-spin" }) : null,
|
|
418
|
+
isPending ? "Deleting..." : "Delete"
|
|
419
|
+
]
|
|
420
|
+
}
|
|
421
|
+
)
|
|
422
|
+
] })
|
|
423
|
+
] }) });
|
|
424
|
+
}
|
|
425
|
+
var THEME = {
|
|
426
|
+
primary: "#6366F1",
|
|
427
|
+
light: "#E0E7FF",
|
|
428
|
+
border: "#A5B4FC",
|
|
429
|
+
connector: "#6366F1",
|
|
430
|
+
connectorLight: "#C7D2FE"
|
|
431
|
+
};
|
|
432
|
+
var MAX_NAME_WIDTH = 120;
|
|
433
|
+
function truncateName(name, maxWidth) {
|
|
434
|
+
const avgCharWidth = 7;
|
|
435
|
+
const maxChars = Math.floor(maxWidth / avgCharWidth);
|
|
436
|
+
if (name.length <= maxChars) return name;
|
|
437
|
+
return name.slice(0, maxChars - 1) + "\u2026";
|
|
438
|
+
}
|
|
439
|
+
function TableFieldsNode({ tableName, fields, theme }) {
|
|
440
|
+
const displayFields = fields.length > 0 ? fields : ["field"];
|
|
441
|
+
const fieldsLabel = displayFields.join(", ");
|
|
442
|
+
const sizeClasses = "h-14 w-14";
|
|
443
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-1.5", children: [
|
|
444
|
+
/* @__PURE__ */ jsxs("div", { className: "relative", children: [
|
|
445
|
+
/* @__PURE__ */ jsxs(Fragment, { children: [
|
|
446
|
+
/* @__PURE__ */ jsx(
|
|
447
|
+
"div",
|
|
448
|
+
{
|
|
449
|
+
className: cn("absolute -top-1.5 -left-2 rounded-full border-2 bg-white", sizeClasses),
|
|
450
|
+
style: { borderColor: theme.border }
|
|
451
|
+
}
|
|
452
|
+
),
|
|
453
|
+
/* @__PURE__ */ jsx(
|
|
454
|
+
"div",
|
|
455
|
+
{
|
|
456
|
+
className: cn("absolute -top-0.5 -left-1 rounded-full border-2 bg-white", sizeClasses),
|
|
457
|
+
style: { borderColor: theme.border }
|
|
458
|
+
}
|
|
459
|
+
)
|
|
460
|
+
] }),
|
|
461
|
+
/* @__PURE__ */ jsx(
|
|
462
|
+
"div",
|
|
463
|
+
{
|
|
464
|
+
className: cn("relative flex items-center justify-center rounded-full border-2 shadow-sm", sizeClasses),
|
|
465
|
+
style: { borderColor: theme.border, backgroundColor: theme.light },
|
|
466
|
+
children: /* @__PURE__ */ jsx(Database, { className: "size-6", style: { color: theme.primary } })
|
|
467
|
+
}
|
|
468
|
+
)
|
|
469
|
+
] }),
|
|
470
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-0.5", children: [
|
|
471
|
+
/* @__PURE__ */ jsx(
|
|
472
|
+
"span",
|
|
473
|
+
{
|
|
474
|
+
className: "truncate text-sm font-medium text-gray-600 dark:text-gray-300",
|
|
475
|
+
style: { maxWidth: MAX_NAME_WIDTH },
|
|
476
|
+
title: tableName,
|
|
477
|
+
children: truncateName(tableName || "Table", MAX_NAME_WIDTH)
|
|
478
|
+
}
|
|
479
|
+
),
|
|
480
|
+
/* @__PURE__ */ jsx(
|
|
481
|
+
"span",
|
|
482
|
+
{
|
|
483
|
+
className: "truncate text-xs text-gray-600 dark:text-gray-400",
|
|
484
|
+
style: { maxWidth: MAX_NAME_WIDTH },
|
|
485
|
+
title: fieldsLabel,
|
|
486
|
+
children: truncateName(fieldsLabel, MAX_NAME_WIDTH)
|
|
487
|
+
}
|
|
488
|
+
)
|
|
489
|
+
] })
|
|
490
|
+
] });
|
|
491
|
+
}
|
|
492
|
+
function IndexNode({
|
|
493
|
+
indexName,
|
|
494
|
+
typeLabel,
|
|
495
|
+
theme,
|
|
496
|
+
isUnique
|
|
497
|
+
}) {
|
|
498
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2", children: [
|
|
499
|
+
/* @__PURE__ */ jsxs("div", { className: "relative", children: [
|
|
500
|
+
/* @__PURE__ */ jsxs(
|
|
501
|
+
"div",
|
|
502
|
+
{
|
|
503
|
+
className: "flex flex-col items-center gap-1 rounded-lg border-2 bg-white px-3 py-2",
|
|
504
|
+
style: { borderColor: theme.border, backgroundColor: theme.light },
|
|
505
|
+
children: [
|
|
506
|
+
/* @__PURE__ */ jsx(DatabaseZap, { className: "size-6", style: { color: theme.primary } }),
|
|
507
|
+
/* @__PURE__ */ jsx(
|
|
508
|
+
"span",
|
|
509
|
+
{
|
|
510
|
+
className: "max-w-[100px] truncate text-center text-xs font-medium",
|
|
511
|
+
style: { color: theme.primary },
|
|
512
|
+
title: typeLabel,
|
|
513
|
+
children: typeLabel
|
|
514
|
+
}
|
|
515
|
+
)
|
|
516
|
+
]
|
|
517
|
+
}
|
|
518
|
+
),
|
|
519
|
+
isUnique && /* @__PURE__ */ jsx(
|
|
520
|
+
"div",
|
|
521
|
+
{
|
|
522
|
+
className: "absolute -right-2 -bottom-2 flex h-6 w-6 items-center justify-center rounded-full border-2\n border-white shadow-sm",
|
|
523
|
+
style: { backgroundColor: theme.primary },
|
|
524
|
+
children: /* @__PURE__ */ jsx("span", { className: "text-[10px] font-bold text-white", children: "U" })
|
|
525
|
+
}
|
|
526
|
+
)
|
|
527
|
+
] }),
|
|
528
|
+
/* @__PURE__ */ jsx("span", { className: "max-w-[100px] truncate text-center text-sm text-gray-600 dark:text-gray-300", title: indexName, children: truncateName(indexName || "Index", 100) })
|
|
529
|
+
] });
|
|
530
|
+
}
|
|
531
|
+
function ArrowConnector({ theme, label }) {
|
|
532
|
+
return /* @__PURE__ */ jsxs("div", { className: "relative flex flex-col items-center justify-center", children: [
|
|
533
|
+
/* @__PURE__ */ jsx(
|
|
534
|
+
"div",
|
|
535
|
+
{
|
|
536
|
+
className: "absolute -top-2 left-1/2 -translate-x-1/2 rounded border px-1.5 py-px text-[10px] leading-tight\n font-medium whitespace-nowrap",
|
|
537
|
+
style: {
|
|
538
|
+
borderColor: theme.primary,
|
|
539
|
+
backgroundColor: theme.light,
|
|
540
|
+
color: theme.primary
|
|
541
|
+
},
|
|
542
|
+
children: label
|
|
543
|
+
}
|
|
544
|
+
),
|
|
545
|
+
/* @__PURE__ */ jsxs("svg", { width: 90, height: "40", viewBox: "0 0 90 40", fill: "none", children: [
|
|
546
|
+
/* @__PURE__ */ jsx("circle", { cx: "6", cy: "20", r: "4", fill: theme.connector }),
|
|
547
|
+
/* @__PURE__ */ jsx("line", { x1: "10", y1: "20", x2: "74", y2: "20", stroke: theme.connectorLight, strokeWidth: "2.5", strokeLinecap: "round" }),
|
|
548
|
+
/* @__PURE__ */ jsx("polygon", { points: "84,20 76,15 76,25", fill: theme.connector })
|
|
549
|
+
] })
|
|
550
|
+
] });
|
|
551
|
+
}
|
|
552
|
+
function IndexDiagram({ tableName, indexName, fields, indexType, isUnique }) {
|
|
553
|
+
const theme = THEME;
|
|
554
|
+
const safeIndexType = indexType && INDEX_TYPE_LABELS[indexType] ? indexType : "btree";
|
|
555
|
+
const typeLabel = INDEX_TYPE_LABELS[safeIndexType];
|
|
556
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-3", children: [
|
|
557
|
+
/* @__PURE__ */ jsx(IndexNode, { indexName: indexName || "Index", typeLabel, theme, isUnique }),
|
|
558
|
+
/* @__PURE__ */ jsx(ArrowConnector, { theme, label: "indexes" }),
|
|
559
|
+
/* @__PURE__ */ jsx(TableFieldsNode, { tableName, fields, theme })
|
|
560
|
+
] });
|
|
561
|
+
}
|
|
562
|
+
var FIELD_SUMMARY_LIMIT = 4;
|
|
563
|
+
var indexTypeOptions = Object.keys(INDEX_TYPE_LABELS).map((value) => ({
|
|
564
|
+
value,
|
|
565
|
+
label: value === "btree" ? `${INDEX_TYPE_LABELS[value]} (default)` : INDEX_TYPE_LABELS[value]
|
|
566
|
+
}));
|
|
567
|
+
function normalizeIndexType(type) {
|
|
568
|
+
const raw = type?.toLowerCase();
|
|
569
|
+
return raw && raw in INDEX_TYPE_LABELS ? raw : "btree";
|
|
570
|
+
}
|
|
571
|
+
function generateIndexName(tableName, existingIndexes) {
|
|
572
|
+
let counter = 1;
|
|
573
|
+
let candidateName = `${tableName}_index_${counter}`;
|
|
574
|
+
while (existingIndexes.some((idx) => idx.name === candidateName)) {
|
|
575
|
+
counter++;
|
|
576
|
+
candidateName = `${tableName}_index_${counter}`;
|
|
577
|
+
}
|
|
578
|
+
return candidateName;
|
|
579
|
+
}
|
|
580
|
+
var IndexCard = ({ editingIndex: initialEditingIndex, card }) => {
|
|
581
|
+
const { currentTable } = useSchemaBuilderSelectors();
|
|
582
|
+
const createIndexMutation = useCreateIndex();
|
|
583
|
+
const updateIndexMutation = useUpdateIndex();
|
|
584
|
+
const [isPendingTransition, startTransition] = useTransition();
|
|
585
|
+
const indexes = currentTable?.indexes || [];
|
|
586
|
+
const [editingIndex, setEditingIndex] = useState(() => {
|
|
587
|
+
if (initialEditingIndex) {
|
|
588
|
+
return {
|
|
589
|
+
...initialEditingIndex,
|
|
590
|
+
fields: [...initialEditingIndex.fields],
|
|
591
|
+
type: normalizeIndexType(initialEditingIndex.type)
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
return null;
|
|
595
|
+
});
|
|
596
|
+
const [originalIndex, setOriginalIndex] = useState(() => {
|
|
597
|
+
if (initialEditingIndex) {
|
|
598
|
+
return {
|
|
599
|
+
...initialEditingIndex,
|
|
600
|
+
fields: [...initialEditingIndex.fields],
|
|
601
|
+
type: normalizeIndexType(initialEditingIndex.type)
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
return null;
|
|
605
|
+
});
|
|
606
|
+
const fieldOptions = useMemo(
|
|
607
|
+
() => currentTable?.fields.map((field) => ({
|
|
608
|
+
label: field.name,
|
|
609
|
+
value: field.id
|
|
610
|
+
})) || [],
|
|
611
|
+
[currentTable?.fields]
|
|
612
|
+
);
|
|
613
|
+
const fieldNameMap = useMemo(() => {
|
|
614
|
+
const map = {};
|
|
615
|
+
currentTable?.fields.forEach((field) => {
|
|
616
|
+
map[field.id] = field.name;
|
|
617
|
+
});
|
|
618
|
+
return map;
|
|
619
|
+
}, [currentTable?.fields]);
|
|
620
|
+
useEffect(() => {
|
|
621
|
+
if (!initialEditingIndex && currentTable && !editingIndex) {
|
|
622
|
+
startTransition(() => {
|
|
623
|
+
const generatedName = generateIndexName(currentTable.name, indexes);
|
|
624
|
+
setEditingIndex({
|
|
625
|
+
id: `idx-${Date.now()}`,
|
|
626
|
+
name: generatedName,
|
|
627
|
+
fields: [],
|
|
628
|
+
unique: false,
|
|
629
|
+
type: "btree"
|
|
630
|
+
});
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
}, [initialEditingIndex, currentTable, indexes, editingIndex]);
|
|
634
|
+
const handleSaveIndex = useCallback(async () => {
|
|
635
|
+
if (!editingIndex || editingIndex.fields.length === 0 || !currentTable) return;
|
|
636
|
+
const trimmedName = editingIndex.name?.trim() ?? "";
|
|
637
|
+
if (!trimmedName) return;
|
|
638
|
+
const isExisting = indexes.some((index) => index.id === editingIndex.id);
|
|
639
|
+
try {
|
|
640
|
+
if (isExisting) {
|
|
641
|
+
await updateIndexMutation.mutateAsync({
|
|
642
|
+
id: editingIndex.id,
|
|
643
|
+
name: trimmedName,
|
|
644
|
+
fieldIds: editingIndex.fields,
|
|
645
|
+
isUnique: editingIndex.type === "btree" ? editingIndex.unique : false,
|
|
646
|
+
accessMethod: editingIndex.type
|
|
647
|
+
});
|
|
648
|
+
toast.success({
|
|
649
|
+
message: "Index updated",
|
|
650
|
+
description: `Index "${trimmedName}" has been updated`
|
|
651
|
+
});
|
|
652
|
+
} else {
|
|
653
|
+
await createIndexMutation.mutateAsync({
|
|
654
|
+
tableId: currentTable.id,
|
|
655
|
+
name: trimmedName,
|
|
656
|
+
fieldIds: editingIndex.fields,
|
|
657
|
+
isUnique: editingIndex.type === "btree" ? editingIndex.unique : false,
|
|
658
|
+
accessMethod: editingIndex.type
|
|
659
|
+
});
|
|
660
|
+
toast.success({
|
|
661
|
+
message: "Index created",
|
|
662
|
+
description: `Index "${trimmedName}" has been created`
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
card.close();
|
|
666
|
+
} catch (error) {
|
|
667
|
+
toast.error({
|
|
668
|
+
message: isExisting ? "Failed to update index" : "Failed to create index",
|
|
669
|
+
description: error instanceof Error ? error.message : "An error occurred"
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
}, [editingIndex, currentTable, indexes, updateIndexMutation, createIndexMutation, card]);
|
|
673
|
+
const handleNameChange = useCallback(
|
|
674
|
+
(event) => {
|
|
675
|
+
setEditingIndex((prev) => prev ? { ...prev, name: event.target.value } : null);
|
|
676
|
+
},
|
|
677
|
+
[]
|
|
678
|
+
);
|
|
679
|
+
const handleFieldsChange = useCallback((values) => {
|
|
680
|
+
setEditingIndex((prev) => prev ? { ...prev, fields: values } : null);
|
|
681
|
+
}, []);
|
|
682
|
+
const handleTypeChange = useCallback((value) => {
|
|
683
|
+
setEditingIndex(
|
|
684
|
+
(prev) => prev ? {
|
|
685
|
+
...prev,
|
|
686
|
+
type: value,
|
|
687
|
+
unique: value === "btree" ? prev.unique : false
|
|
688
|
+
} : null
|
|
689
|
+
);
|
|
690
|
+
}, []);
|
|
691
|
+
const handleUniqueChange = useCallback((checked) => {
|
|
692
|
+
setEditingIndex((prev) => prev ? { ...prev, unique: checked } : null);
|
|
693
|
+
}, []);
|
|
694
|
+
const handleClose = useCallback(() => {
|
|
695
|
+
card.close();
|
|
696
|
+
}, [card]);
|
|
697
|
+
const hasChanges = useMemo(() => {
|
|
698
|
+
if (originalIndex === null) return true;
|
|
699
|
+
if (!editingIndex) return false;
|
|
700
|
+
return editingIndex.name !== originalIndex.name || editingIndex.type !== originalIndex.type || editingIndex.unique !== originalIndex.unique || editingIndex.fields.length !== originalIndex.fields.length || editingIndex.fields.some((f, i) => f !== originalIndex.fields[i]);
|
|
701
|
+
}, [editingIndex, originalIndex]);
|
|
702
|
+
if (!currentTable) return null;
|
|
703
|
+
const isEditMode = initialEditingIndex !== null;
|
|
704
|
+
const selectedIndexType = editingIndex?.type ?? "btree";
|
|
705
|
+
const selectedFieldNames = editingIndex?.fields.map((id) => fieldNameMap[id] || id) || [];
|
|
706
|
+
const canSave = editingIndex && editingIndex.fields.length > 0 && (editingIndex.name?.trim().length ?? 0) > 0 && hasChanges;
|
|
707
|
+
const isPending = createIndexMutation.isPending || updateIndexMutation.isPending;
|
|
708
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col", children: [
|
|
709
|
+
/* @__PURE__ */ jsx(ScrollArea, { className: "min-h-0 flex-1", children: /* @__PURE__ */ jsxs("div", { className: "space-y-6 p-6", children: [
|
|
710
|
+
/* @__PURE__ */ jsx(ResponsiveDiagram, { className: "py-8", children: /* @__PURE__ */ jsx(
|
|
711
|
+
IndexDiagram,
|
|
712
|
+
{
|
|
713
|
+
tableName: currentTable.name,
|
|
714
|
+
indexName: editingIndex?.name,
|
|
715
|
+
fields: selectedFieldNames,
|
|
716
|
+
indexType: selectedIndexType,
|
|
717
|
+
isUnique: editingIndex?.unique ?? false
|
|
718
|
+
}
|
|
719
|
+
) }),
|
|
720
|
+
editingIndex && /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
|
|
721
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
|
|
722
|
+
/* @__PURE__ */ jsx(Label, { className: "block", htmlFor: "index-name", children: "Index name" }),
|
|
723
|
+
/* @__PURE__ */ jsx(
|
|
724
|
+
Input,
|
|
725
|
+
{
|
|
726
|
+
id: "index-name",
|
|
727
|
+
value: editingIndex.name,
|
|
728
|
+
onChange: handleNameChange,
|
|
729
|
+
placeholder: "e.g. table_index_1"
|
|
730
|
+
}
|
|
731
|
+
)
|
|
732
|
+
] }),
|
|
733
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
|
|
734
|
+
/* @__PURE__ */ jsx(Label, { className: "block", children: "Fields" }),
|
|
735
|
+
/* @__PURE__ */ jsx(
|
|
736
|
+
MultiSelect,
|
|
737
|
+
{
|
|
738
|
+
options: fieldOptions,
|
|
739
|
+
defaultValue: editingIndex.fields,
|
|
740
|
+
onValueChange: handleFieldsChange,
|
|
741
|
+
placeholder: "Select fields",
|
|
742
|
+
dropdownMaxHeight: "300px",
|
|
743
|
+
maxCount: FIELD_SUMMARY_LIMIT
|
|
744
|
+
}
|
|
745
|
+
)
|
|
746
|
+
] }),
|
|
747
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
|
|
748
|
+
/* @__PURE__ */ jsx(Label, { className: "block", children: "Index type" }),
|
|
749
|
+
/* @__PURE__ */ jsxs(Select, { value: selectedIndexType, onValueChange: handleTypeChange, children: [
|
|
750
|
+
/* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "Select index type" }) }),
|
|
751
|
+
/* @__PURE__ */ jsx(SelectContent, { children: indexTypeOptions.map((option) => /* @__PURE__ */ jsx(SelectItem, { value: option.value, children: option.label }, option.value)) })
|
|
752
|
+
] })
|
|
753
|
+
] }),
|
|
754
|
+
selectedIndexType === "btree" && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
755
|
+
/* @__PURE__ */ jsx(Label, { htmlFor: "index-unique", children: "Unique" }),
|
|
756
|
+
/* @__PURE__ */ jsx(
|
|
757
|
+
Switch,
|
|
758
|
+
{
|
|
759
|
+
id: "index-unique",
|
|
760
|
+
checked: editingIndex.unique ?? false,
|
|
761
|
+
onCheckedChange: handleUniqueChange
|
|
762
|
+
}
|
|
763
|
+
)
|
|
764
|
+
] })
|
|
765
|
+
] })
|
|
766
|
+
] }) }),
|
|
767
|
+
/* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2 border-t p-4", children: [
|
|
768
|
+
/* @__PURE__ */ jsx(Button, { variant: "outline", disabled: isPending, onClick: handleClose, children: "Cancel" }),
|
|
769
|
+
/* @__PURE__ */ jsxs(Button, { onClick: handleSaveIndex, disabled: !canSave || isPending, children: [
|
|
770
|
+
isPending && /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin" }),
|
|
771
|
+
isPending ? "Saving..." : isEditMode ? "Update index" : "Create index"
|
|
772
|
+
] })
|
|
773
|
+
] })
|
|
774
|
+
] });
|
|
775
|
+
};
|
|
776
|
+
function EmptyStateCard({
|
|
777
|
+
icon: Icon,
|
|
778
|
+
title,
|
|
779
|
+
description,
|
|
780
|
+
actionLabel,
|
|
781
|
+
actionIcon: ActionIcon,
|
|
782
|
+
onAction,
|
|
783
|
+
actionTestId,
|
|
784
|
+
features,
|
|
785
|
+
className
|
|
786
|
+
}) {
|
|
787
|
+
return /* @__PURE__ */ jsx("div", { className: cn("@container flex w-full items-center justify-center py-12", className), children: /* @__PURE__ */ jsxs("div", { className: "bg-card border-border max-w-2xl rounded-lg border p-6 @md:p-8", children: [
|
|
788
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center text-center", children: [
|
|
789
|
+
/* @__PURE__ */ jsx("div", { className: "border-primary/20 bg-primary/10 mb-4 flex h-20 w-20 items-center justify-center rounded-2xl border", children: /* @__PURE__ */ jsx(Icon, { className: "text-primary h-10 w-10" }) }),
|
|
790
|
+
/* @__PURE__ */ jsx("h2", { className: "mb-2 text-balance text-xl font-semibold", children: title }),
|
|
791
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground mb-6 max-w-lg text-sm", children: description }),
|
|
792
|
+
actionLabel && onAction && /* @__PURE__ */ jsxs(Button, { onClick: onAction, size: "xl", className: "px-12", "data-testid": actionTestId, children: [
|
|
793
|
+
ActionIcon && /* @__PURE__ */ jsx(ActionIcon, { className: "mr-2 h-4 w-4" }),
|
|
794
|
+
actionLabel
|
|
795
|
+
] })
|
|
796
|
+
] }),
|
|
797
|
+
features && features.length > 0 && /* @__PURE__ */ jsx("div", { className: "border-border mt-8 grid grid-cols-1 gap-4 border-t pt-6 @sm:grid-cols-3", children: features.map((feature) => /* @__PURE__ */ jsxs(
|
|
798
|
+
"div",
|
|
799
|
+
{
|
|
800
|
+
className: "flex flex-row items-center gap-3 text-left @sm:flex-col @sm:items-center @sm:gap-0 @sm:text-center",
|
|
801
|
+
children: [
|
|
802
|
+
/* @__PURE__ */ jsx("div", { className: "bg-muted flex h-10 w-10 shrink-0 items-center justify-center rounded-lg @sm:mb-2", children: /* @__PURE__ */ jsx(feature.icon, { className: "text-muted-foreground h-5 w-5" }) }),
|
|
803
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
804
|
+
/* @__PURE__ */ jsx("h3", { className: "mb-0.5 text-balance text-sm font-medium @sm:mb-1", children: feature.title }),
|
|
805
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground text-xs", children: feature.description })
|
|
806
|
+
] })
|
|
807
|
+
]
|
|
808
|
+
},
|
|
809
|
+
feature.title
|
|
810
|
+
)) })
|
|
811
|
+
] }) });
|
|
812
|
+
}
|
|
813
|
+
function IndexEmptyState({ onCreateClick, tableName }) {
|
|
814
|
+
return /* @__PURE__ */ jsx(
|
|
815
|
+
EmptyStateCard,
|
|
816
|
+
{
|
|
817
|
+
icon: ListChecks,
|
|
818
|
+
title: tableName ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
819
|
+
"No indexes for ",
|
|
820
|
+
tableName
|
|
821
|
+
] }) : "Create your first index",
|
|
822
|
+
description: tableName ? `Add an index to improve query performance on ${tableName}. Indexes speed up data retrieval by creating efficient lookup structures.` : "Create indexes to optimize query performance. Indexes help the database find and retrieve data faster.",
|
|
823
|
+
actionLabel: "Create Index",
|
|
824
|
+
actionIcon: ListChecks,
|
|
825
|
+
onAction: onCreateClick,
|
|
826
|
+
features: [
|
|
827
|
+
{ icon: Zap, title: "Faster Queries", description: "Speed up data retrieval operations" },
|
|
828
|
+
{ icon: Search, title: "Efficient Lookups", description: "Quick access to specific rows" },
|
|
829
|
+
{ icon: Shield, title: "Unique Constraints", description: "Enforce data uniqueness rules" }
|
|
830
|
+
]
|
|
831
|
+
}
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
var FIELD_SUMMARY_LIMIT2 = 4;
|
|
835
|
+
function buildFieldSummary(fieldIds, tableFields, limit = FIELD_SUMMARY_LIMIT2) {
|
|
836
|
+
const fieldNameList = fieldIds.map((fieldId) => tableFields.find((field) => field.id === fieldId)?.name || fieldId).filter(Boolean);
|
|
837
|
+
const visibleFields = fieldNameList.slice(0, limit);
|
|
838
|
+
const remainingCount = Math.max(fieldNameList.length - visibleFields.length, 0);
|
|
839
|
+
const hasFields = fieldNameList.length > 0;
|
|
840
|
+
const fieldSummary = hasFields ? `${visibleFields.join(", ")}${remainingCount > 0 ? `, +${remainingCount} more` : ""}` : "None";
|
|
841
|
+
return {
|
|
842
|
+
fieldSummary,
|
|
843
|
+
fieldNameList,
|
|
844
|
+
hasFields
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
function NoTableSelectedState() {
|
|
848
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex min-h-0 flex-1 flex-col items-center justify-center p-6", children: [
|
|
849
|
+
/* @__PURE__ */ jsx("div", { className: "bg-muted/50 mb-4 flex h-16 w-16 items-center justify-center rounded-full", children: /* @__PURE__ */ jsx(Table2, { className: "text-muted-foreground h-8 w-8" }) }),
|
|
850
|
+
/* @__PURE__ */ jsx("h3", { className: "mb-1 text-balance text-lg font-semibold", children: "No table selected" }),
|
|
851
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground max-w-sm text-center text-sm", children: "Select a table from the sidebar to view and manage its indexes." })
|
|
852
|
+
] });
|
|
853
|
+
}
|
|
854
|
+
function IndexesView() {
|
|
855
|
+
const [deleteTarget, setDeleteTarget] = useState(null);
|
|
856
|
+
const [isDeleting, setIsDeleting] = useState(false);
|
|
857
|
+
const stack = useCardStack();
|
|
858
|
+
const { currentTable } = useSchemaBuilderSelectors();
|
|
859
|
+
const deleteIndexMutation = useDeleteIndex();
|
|
860
|
+
if (!currentTable) {
|
|
861
|
+
return /* @__PURE__ */ jsx(NoTableSelectedState, {});
|
|
862
|
+
}
|
|
863
|
+
const indexes = currentTable.indexes || [];
|
|
864
|
+
const hasIndexes = indexes.length > 0;
|
|
865
|
+
const handleOpenCreate = () => {
|
|
866
|
+
stack.push({
|
|
867
|
+
id: "index-new",
|
|
868
|
+
title: "Create Index",
|
|
869
|
+
Component: IndexCard,
|
|
870
|
+
props: { editingIndex: null },
|
|
871
|
+
width: CARD_WIDTHS.medium
|
|
872
|
+
});
|
|
873
|
+
};
|
|
874
|
+
const handleOpenEdit = (index) => {
|
|
875
|
+
stack.push({
|
|
876
|
+
id: `index-${index.id}`,
|
|
877
|
+
title: "Edit Index",
|
|
878
|
+
Component: IndexCard,
|
|
879
|
+
props: { editingIndex: index },
|
|
880
|
+
width: CARD_WIDTHS.medium
|
|
881
|
+
});
|
|
882
|
+
};
|
|
883
|
+
const handleDeleteRequest = (indexId, indexName) => {
|
|
884
|
+
setDeleteTarget({ id: indexId, name: indexName });
|
|
885
|
+
};
|
|
886
|
+
const handleDeleteConfirm = async () => {
|
|
887
|
+
if (!deleteTarget) return;
|
|
888
|
+
setIsDeleting(true);
|
|
889
|
+
try {
|
|
890
|
+
await deleteIndexMutation.mutateAsync({ id: deleteTarget.id });
|
|
891
|
+
toast.success({ message: "Index deleted" });
|
|
892
|
+
setDeleteTarget(null);
|
|
893
|
+
} catch (error) {
|
|
894
|
+
toast.error({
|
|
895
|
+
message: "Failed to delete index",
|
|
896
|
+
description: error instanceof Error ? error.message : "An error occurred"
|
|
897
|
+
});
|
|
898
|
+
} finally {
|
|
899
|
+
setIsDeleting(false);
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
903
|
+
/* @__PURE__ */ jsx("div", { className: "flex min-h-0 flex-1 flex-col overflow-auto p-6", children: !hasIndexes ? /* @__PURE__ */ jsx(IndexEmptyState, { onCreateClick: handleOpenCreate, tableName: currentTable.name }) : /* @__PURE__ */ jsxs("div", { className: "mx-auto w-full max-w-4xl space-y-6", children: [
|
|
904
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
905
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
906
|
+
/* @__PURE__ */ jsx("h2", { className: "text-balance text-xl font-semibold tracking-tight", children: "Indexes" }),
|
|
907
|
+
/* @__PURE__ */ jsxs("p", { className: "text-muted-foreground mt-1 text-sm", children: [
|
|
908
|
+
indexes.length,
|
|
909
|
+
" index",
|
|
910
|
+
indexes.length !== 1 ? "es" : "",
|
|
911
|
+
" on",
|
|
912
|
+
" ",
|
|
913
|
+
/* @__PURE__ */ jsx("code", { className: "bg-muted rounded px-1.5 py-0.5 font-mono text-xs", children: currentTable.name })
|
|
914
|
+
] })
|
|
915
|
+
] }),
|
|
916
|
+
/* @__PURE__ */ jsxs(Button, { onClick: handleOpenCreate, children: [
|
|
917
|
+
/* @__PURE__ */ jsx(Plus, { className: "mr-2 h-4 w-4" }),
|
|
918
|
+
"Add Index"
|
|
919
|
+
] })
|
|
920
|
+
] }),
|
|
921
|
+
/* @__PURE__ */ jsx("div", { className: "space-y-3", children: indexes.map((index) => {
|
|
922
|
+
const { fieldNameList, hasFields } = buildFieldSummary(index.fields, currentTable.fields);
|
|
923
|
+
return /* @__PURE__ */ jsxs(
|
|
924
|
+
"div",
|
|
925
|
+
{
|
|
926
|
+
role: "button",
|
|
927
|
+
tabIndex: 0,
|
|
928
|
+
onClick: () => handleOpenEdit(index),
|
|
929
|
+
onKeyDown: (event) => {
|
|
930
|
+
handleActivationKeyDown(event, () => handleOpenEdit(index));
|
|
931
|
+
},
|
|
932
|
+
className: cn(
|
|
933
|
+
`group relative cursor-pointer rounded-xl border p-4 transition-[background-color,border-color,box-shadow,scale]
|
|
934
|
+
duration-150 ease-out motion-safe:active:scale-[0.96]`,
|
|
935
|
+
"border-border/60 hover:border-border/80 hover:bg-muted/30"
|
|
936
|
+
),
|
|
937
|
+
children: [
|
|
938
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
|
|
939
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
940
|
+
/* @__PURE__ */ jsx(ListChecks, { className: "text-muted-foreground h-4 w-4" }),
|
|
941
|
+
/* @__PURE__ */ jsx("p", { className: "truncate text-sm font-semibold", children: index.name || "Unnamed index" }),
|
|
942
|
+
/* @__PURE__ */ jsx(
|
|
943
|
+
"span",
|
|
944
|
+
{
|
|
945
|
+
className: cn(
|
|
946
|
+
"rounded-full px-2 py-0.5 text-[10px] font-semibold tracking-wide uppercase",
|
|
947
|
+
"border border-blue-500/30 bg-blue-500/10 text-blue-600 dark:text-blue-400"
|
|
948
|
+
),
|
|
949
|
+
children: INDEX_TYPE_LABELS[index.type ?? "btree"] ?? index.type ?? "B-tree"
|
|
950
|
+
}
|
|
951
|
+
),
|
|
952
|
+
index.unique && /* @__PURE__ */ jsx(
|
|
953
|
+
"span",
|
|
954
|
+
{
|
|
955
|
+
className: cn(
|
|
956
|
+
"rounded-full px-2 py-0.5 text-[10px] font-semibold tracking-wide uppercase",
|
|
957
|
+
"border border-emerald-500/30 bg-emerald-500/10 text-emerald-600 dark:text-emerald-400"
|
|
958
|
+
),
|
|
959
|
+
children: "Unique"
|
|
960
|
+
}
|
|
961
|
+
)
|
|
962
|
+
] }),
|
|
963
|
+
/* @__PURE__ */ jsx(
|
|
964
|
+
Button,
|
|
965
|
+
{
|
|
966
|
+
variant: "ghost",
|
|
967
|
+
size: "sm",
|
|
968
|
+
"aria-label": "Delete index",
|
|
969
|
+
className: "text-muted-foreground hover:text-destructive hover:bg-destructive/10 h-8 w-8 shrink-0 p-0",
|
|
970
|
+
onClick: (e) => {
|
|
971
|
+
e.stopPropagation();
|
|
972
|
+
handleDeleteRequest(index.id, index.name || "this index");
|
|
973
|
+
},
|
|
974
|
+
children: /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" })
|
|
975
|
+
}
|
|
976
|
+
)
|
|
977
|
+
] }),
|
|
978
|
+
/* @__PURE__ */ jsx("div", { className: "mt-2.5 flex flex-wrap items-center gap-1.5", children: hasFields ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
979
|
+
fieldNameList.slice(0, FIELD_SUMMARY_LIMIT2).map((name) => /* @__PURE__ */ jsx(
|
|
980
|
+
"span",
|
|
981
|
+
{
|
|
982
|
+
className: "bg-muted/60 text-foreground/80 rounded-md px-2 py-0.5 font-mono text-xs",
|
|
983
|
+
children: name
|
|
984
|
+
},
|
|
985
|
+
name
|
|
986
|
+
)),
|
|
987
|
+
fieldNameList.length > FIELD_SUMMARY_LIMIT2 && /* @__PURE__ */ jsxs(Tooltip, { children: [
|
|
988
|
+
/* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxs("span", { className: "text-muted-foreground rounded-md px-1.5 py-0.5 text-xs", children: [
|
|
989
|
+
"+",
|
|
990
|
+
fieldNameList.length - FIELD_SUMMARY_LIMIT2,
|
|
991
|
+
" more"
|
|
992
|
+
] }) }),
|
|
993
|
+
/* @__PURE__ */ jsx(TooltipContent, { className: "max-w-xs", children: fieldNameList.join(", ") })
|
|
994
|
+
] })
|
|
995
|
+
] }) : /* @__PURE__ */ jsx("span", { className: "text-muted-foreground text-xs", children: "No fields" }) })
|
|
996
|
+
]
|
|
997
|
+
},
|
|
998
|
+
index.id
|
|
999
|
+
);
|
|
1000
|
+
}) })
|
|
1001
|
+
] }) }),
|
|
1002
|
+
/* @__PURE__ */ jsx(
|
|
1003
|
+
DeleteConfirmDialog,
|
|
1004
|
+
{
|
|
1005
|
+
entityType: "Index",
|
|
1006
|
+
entityName: deleteTarget?.name ?? null,
|
|
1007
|
+
open: !!deleteTarget,
|
|
1008
|
+
onOpenChange: () => setDeleteTarget(null),
|
|
1009
|
+
onConfirm: handleDeleteConfirm,
|
|
1010
|
+
isPending: isDeleting
|
|
1011
|
+
}
|
|
1012
|
+
)
|
|
1013
|
+
] });
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
export { IndexesView, useCreateIndex, useDeleteIndex, useUpdateIndex };
|
|
1017
|
+
//# sourceMappingURL=indexes.js.map
|
|
1018
|
+
//# sourceMappingURL=indexes.js.map
|