refactor: update data structures to match new API schema

Align interfaces and components with nested schema, expanded fields, and new naming conventions

#VERCEL_SKIP

Co-authored-by: Allan K. Koech <44490960+allankoechke@users.noreply.github.com>
This commit is contained in:
v0
2025-06-26 19:05:57 +00:00
parent cce3208edc
commit c063668d65
9 changed files with 522 additions and 167 deletions

View File

@@ -18,7 +18,7 @@ import {
} from "@/components/ui/dialog"
import { Textarea } from "@/components/ui/textarea"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import type { ApiClient, TableMetadata } from "@/lib/api"
import type { ApiClient, TableMetadata, TableField } from "@/lib/api"
interface AddTableDialogProps {
apiClient: ApiClient
@@ -38,6 +38,7 @@ export function AddTableDialog({ apiClient, onTablesUpdate, children }: AddTable
unique?: boolean
isFile?: boolean
isSystem?: boolean
required?: boolean
}>
>([])
const [sqlQuery, setSqlQuery] = React.useState("")
@@ -46,10 +47,12 @@ export function AddTableDialog({ apiClient, onTablesUpdate, children }: AddTable
const dataTypes = [
"string",
"int",
"bigint",
"double",
"float",
"int8",
"int16",
"int32",
"int64",
"float32",
"float64",
"boolean",
"date",
"datetime",
@@ -66,17 +69,17 @@ export function AddTableDialog({ apiClient, onTablesUpdate, children }: AddTable
React.useEffect(() => {
if (tableType === "base") {
setColumns([
{ name: "id", type: "string", primaryKey: true, nullable: false, isSystem: true },
{ name: "created", type: "datetime", primaryKey: false, nullable: false, isSystem: true },
{ name: "updated", type: "datetime", primaryKey: false, nullable: false, isSystem: true },
{ name: "id", type: "string", primaryKey: true, nullable: false, isSystem: true, required: true },
{ name: "created", type: "datetime", primaryKey: false, nullable: false, isSystem: true, required: true },
{ name: "updated", type: "datetime", primaryKey: false, nullable: false, isSystem: true, required: true },
])
} else if (tableType === "auth") {
setColumns([
{ name: "id", type: "string", primaryKey: true, nullable: false, isSystem: true },
{ name: "email", type: "string", primaryKey: false, nullable: false, isSystem: true },
{ name: "password", type: "string", primaryKey: false, nullable: false, isSystem: true },
{ name: "created", type: "datetime", primaryKey: false, nullable: false, isSystem: true },
{ name: "updated", type: "datetime", primaryKey: false, nullable: false, isSystem: true },
{ name: "id", type: "string", primaryKey: true, nullable: false, isSystem: true, required: true },
{ name: "email", type: "string", primaryKey: false, nullable: false, isSystem: true, required: true },
{ name: "password", type: "string", primaryKey: false, nullable: false, isSystem: true, required: true },
{ name: "created", type: "datetime", primaryKey: false, nullable: false, isSystem: true, required: true },
{ name: "updated", type: "datetime", primaryKey: false, nullable: false, isSystem: true, required: true },
])
} else {
setColumns([])
@@ -84,7 +87,10 @@ export function AddTableDialog({ apiClient, onTablesUpdate, children }: AddTable
}, [tableType])
const addColumn = () => {
setColumns([...columns, { name: "", type: "string", primaryKey: false, nullable: true, isSystem: false }])
setColumns([
...columns,
{ name: "", type: "string", primaryKey: false, nullable: true, isSystem: false, required: false },
])
}
const removeColumn = (index: number) => {
@@ -119,7 +125,18 @@ export function AddTableDialog({ apiClient, onTablesUpdate, children }: AddTable
if (tableType === "view") {
tableData.sql = sqlQuery
} else {
tableData.fields = columns
tableData.fields = columns.map(
(col): TableField => ({
name: col.name,
type: col.type,
primaryKey: col.primaryKey,
nullable: col.nullable,
unique: col.unique,
isFile: col.isFile,
system: col.isSystem,
required: col.required,
}),
)
}
await apiClient.call("/api/v1/tables", {

View File

@@ -28,7 +28,7 @@ export function ColumnVisibilityDropdown({ table, visibleColumns, onVisibilityCh
}
const showAll = () => {
onVisibilityChange(table.fields?.map((field) => field.name) || [])
onVisibilityChange(table.schema.fields?.map((field) => field.name) || [])
}
const hideAll = () => {
@@ -58,7 +58,7 @@ export function ColumnVisibilityDropdown({ table, visibleColumns, onVisibilityCh
</Button>
</div>
<DropdownMenuSeparator />
{table.fields?.map((field) => (
{table.schema.fields?.map((field) => (
<DropdownMenuCheckboxItem
key={field.name}
checked={visibleColumns.includes(field.name)}

View File

@@ -218,7 +218,7 @@ export function DatabaseSection({ apiClient, tables, onTablesUpdate }: DatabaseS
</div>
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">Fields:</span>
<span className="font-medium">{table.fields?.length || 0}</span>
<span className="font-medium">{table.schema.fields?.length || 0}</span>
</div>
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">Created:</span>

View File

@@ -62,8 +62,8 @@ export function EditItemDrawer({ table, item, apiClient, open, onClose, onItemUp
setHasUnsavedChanges(true)
}
const isSystemField = (fieldName: string) => {
return ["id", "created", "updated"].includes(fieldName)
const isSystemField = (field: any) => {
return field.system === true
}
const handleClose = () => {
@@ -96,7 +96,7 @@ export function EditItemDrawer({ table, item, apiClient, open, onClose, onItemUp
<div className="flex-1 overflow-hidden">
<ScrollArea className="h-full">
<div className="p-6 space-y-6">
{table.fields?.map((field) => (
{table.schema.fields?.map((field) => (
<div key={field.name} className="space-y-2">
<Label htmlFor={field.name} className="text-sm font-medium capitalize">
{field.name}
@@ -105,15 +105,19 @@ export function EditItemDrawer({ table, item, apiClient, open, onClose, onItemUp
<Input
id={field.name}
type={
field.name === "password" ? "password" : field.type === "datetime" ? "datetime-local" : "text"
field.name === "password"
? "password"
: field.type === "datetime" || field.type === "timestamp"
? "datetime-local"
: "text"
}
value={formData[field.name] || ""}
onChange={(e) => handleFieldChange(field.name, e.target.value)}
disabled={isSystemField(field.name)}
className={`w-full ${isSystemField(field.name) ? "bg-muted" : ""}`}
disabled={isSystemField(field)}
className={`w-full ${isSystemField(field) ? "bg-muted" : ""}`}
placeholder={`Enter ${field.name}`}
/>
{isSystemField(field.name) && (
{isSystemField(field) && (
<p className="text-xs text-muted-foreground">System field - cannot be modified</p>
)}
</div>

View File

@@ -26,7 +26,7 @@ interface EditSchemaDialogProps {
}
export function EditSchemaDialog({ table, apiClient, onClose, onTableUpdate }: EditSchemaDialogProps) {
const [columns, setColumns] = React.useState(table.fields || [])
const [columns, setColumns] = React.useState(table.schema.fields || [])
const [isLoading, setIsLoading] = React.useState(false)
const dataTypes = [
@@ -49,13 +49,12 @@ export function EditSchemaDialog({ table, apiClient, onClose, onTableUpdate }: E
]
const addColumn = () => {
setColumns([...columns, { name: "", type: "string", primaryKey: false, nullable: true }])
setColumns([...columns, { name: "", type: "string", primaryKey: false, required: false, system: false }])
}
const removeColumn = (index: number) => {
const column = columns[index]
const isSystemColumn = ["id", "created", "updated", "email", "password"].includes(column.name)
if (columns.length > 1 && !isSystemColumn) {
if (columns.length > 1 && !column.system) {
setColumns(columns.filter((_, i) => i !== index))
}
}
@@ -70,7 +69,7 @@ export function EditSchemaDialog({ table, apiClient, onClose, onTableUpdate }: E
try {
const updatedTable = await apiClient.call<TableMetadata>(`/api/v1/tables/${table.id}`, {
method: "PATCH",
body: JSON.stringify({ fields: columns }),
body: JSON.stringify({ schema: { fields: columns } }),
})
onTableUpdate(updatedTable)
} catch (error) {
@@ -80,8 +79,8 @@ export function EditSchemaDialog({ table, apiClient, onClose, onTableUpdate }: E
}
}
const isSystemColumn = (columnName: string) => {
return ["id", "created", "updated", "email", "password"].includes(columnName)
const isSystemColumn = (column: any) => {
return column.system
}
return (
@@ -109,7 +108,7 @@ export function EditSchemaDialog({ table, apiClient, onClose, onTableUpdate }: E
<div className="space-y-3 max-h-[400px] overflow-y-auto">
{columns.map((column, index) => {
const isSystem = isSystemColumn(column.name)
const isSystem = isSystemColumn(column)
return (
<div
key={index}
@@ -130,6 +129,51 @@ export function EditSchemaDialog({ table, apiClient, onClose, onTableUpdate }: E
</Badge>
)}
</div>
<div className="flex items-center gap-2 mt-2">
<Input
placeholder="Auto Generate Pattern"
value={column.autoGeneratePattern || ""}
onChange={(e) => updateColumn(index, "autoGeneratePattern", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div className="flex items-center gap-2 mt-2">
<Input
placeholder="Default Value"
value={column.defaultValue || ""}
onChange={(e) => updateColumn(index, "defaultValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div className="flex items-center gap-2 mt-2">
<Input
placeholder="Max Value"
value={column.maxValue || ""}
onChange={(e) => updateColumn(index, "maxValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div className="flex items-center gap-2 mt-2">
<Input
placeholder="Min Value"
value={column.minValue || ""}
onChange={(e) => updateColumn(index, "minValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div className="flex items-center gap-2 mt-2">
<Input
placeholder="Validator"
value={column.validator || ""}
onChange={(e) => updateColumn(index, "validator", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
</div>
<div className="w-32">
<Select
@@ -180,8 +224,8 @@ export function EditSchemaDialog({ table, apiClient, onClose, onTableUpdate }: E
<input
type="checkbox"
id={`required-${index}`}
checked={!column.nullable}
onChange={(e) => updateColumn(index, "nullable", !e.target.checked)}
checked={column.required || false}
onChange={(e) => updateColumn(index, "required", e.target.checked)}
disabled={isSystem}
className="rounded"
/>

View File

@@ -31,7 +31,7 @@ interface TableConfigDrawerProps {
}
export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpdate }: TableConfigDrawerProps) {
const [columns, setColumns] = React.useState(table.fields || [])
const [columns, setColumns] = React.useState(table.schema?.fields || [])
const [rules, setRules] = React.useState(table.rules)
const [isLoading, setIsLoading] = React.useState(false)
const [activeTab, setActiveTab] = React.useState("schema")
@@ -40,7 +40,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
React.useEffect(() => {
if (open) {
setColumns(table.fields || [])
setColumns(table.schema?.fields || [])
setRules(table.rules)
setHasUnsavedChanges(false)
setSystemFieldsCollapsed(true)
@@ -73,7 +73,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
const removeColumn = (index: number) => {
const column = columns[index]
const isSystemColumn = ["id", "created", "updated", "email", "password"].includes(column.name)
const isSystemColumn = column.system
if (columns.length > 1 && !isSystemColumn) {
setColumns(columns.filter((_, i) => i !== index))
}
@@ -90,7 +90,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
try {
const updatedTable = await apiClient.call<TableMetadata>(`/api/v1/tables/${table.id}`, {
method: "PATCH",
body: JSON.stringify({ fields: columns }),
body: JSON.stringify({ schema: { fields: columns } }),
})
onTableUpdate(updatedTable)
} catch (error) {
@@ -105,7 +105,15 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
try {
const updatedTable = await apiClient.call<TableMetadata>(`/api/v1/tables/${table.id}`, {
method: "PATCH",
body: JSON.stringify({ rules }),
body: JSON.stringify({
rules: {
listRule: rules.listRule,
getRule: rules.getRule,
addRule: rules.addRule,
updateRule: rules.updateRule,
deleteRule: rules.deleteRule,
},
}),
})
onTableUpdate(updatedTable)
} catch (error) {
@@ -115,8 +123,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
}
}
const isSystemColumn = (columnName: string) => {
return ["id", "created", "updated", "email", "password"].includes(columnName)
const isSystemColumn = (column: any) => {
return column.system
}
const handleClose = () => {
@@ -174,14 +182,14 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<h5 className="font-medium mb-3">SQL Query</h5>
<ScrollArea className="h-32">
<pre className="text-sm whitespace-pre-wrap">
<code>{table.sql}</code>
<code>{table.schema?.sql}</code>
</pre>
</ScrollArea>
</div>
) : (
<>
{/* System Fields - Collapsible */}
{columns.filter((col) => isSystemColumn(col.name)).length > 0 && (
{columns.filter((col) => isSystemColumn(col)).length > 0 && (
<Collapsible
open={!systemFieldsCollapsed}
onOpenChange={(open) => setSystemFieldsCollapsed(!open)}
@@ -189,7 +197,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<CollapsibleTrigger asChild>
<Button variant="ghost" className="w-full justify-between p-2 mb-4">
<span className="text-sm font-medium">
System Fields ({columns.filter((col) => isSystemColumn(col.name)).length})
System Fields ({columns.filter((col) => isSystemColumn(col)).length})
</span>
<ChevronDown
className={`h-4 w-4 transition-transform ${systemFieldsCollapsed ? "" : "rotate-180"}`}
@@ -198,7 +206,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
</CollapsibleTrigger>
<CollapsibleContent className="space-y-4 mb-6">
{columns.map((column, index) => {
const isSystem = isSystemColumn(column.name)
const isSystem = isSystemColumn(column)
if (!isSystem) return null
return (
<div key={index} className="border rounded-lg bg-muted/50">
@@ -262,8 +270,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<input
type="checkbox"
id={`required-${index}`}
checked={!column.nullable}
onChange={(e) => updateColumn(index, "nullable", !e.target.checked)}
checked={column.required}
onChange={(e) => updateColumn(index, "required", e.target.checked)}
disabled={isSystem}
className="rounded"
/>
@@ -313,6 +321,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Label className="text-sm font-medium mb-2 block">Default Value</Label>
<Input
placeholder="Default value"
value={column.defaultValue || ""}
onChange={(e) => updateColumn(index, "defaultValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
@@ -322,15 +332,55 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Input
type="number"
placeholder="Max length"
value={column.maxValue || ""}
onChange={(e) => updateColumn(index, "maxValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">Min Length</Label>
<Input
type="number"
placeholder="Min length"
value={column.minValue || ""}
onChange={(e) => updateColumn(index, "minValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">
Auto Generate Pattern
</Label>
<Input
placeholder="Auto Generate Pattern"
value={column.autoGeneratePattern || ""}
onChange={(e) =>
updateColumn(index, "autoGeneratePattern", e.target.value)
}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">Validator</Label>
<Textarea
placeholder="Column validator"
value={column.validator || ""}
onChange={(e) => updateColumn(index, "validator", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
rows={2}
/>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">Description</Label>
<Textarea
placeholder="Column description"
value={column.description || ""}
onChange={(e) => updateColumn(index, "description", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
rows={2}
@@ -364,7 +414,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
{/* Regular Fields */}
<div className="space-y-4">
{columns.map((column, index) => {
const isSystem = isSystemColumn(column.name)
const isSystem = isSystemColumn(column)
if (isSystem) return null // Skip system fields as they're in collapsible section
return (
<div key={index} className="border rounded-lg">
@@ -428,8 +478,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<input
type="checkbox"
id={`required-${index}`}
checked={!column.nullable}
onChange={(e) => updateColumn(index, "nullable", !e.target.checked)}
checked={column.required}
onChange={(e) => updateColumn(index, "required", e.target.checked)}
disabled={isSystem}
className="rounded"
/>
@@ -479,6 +529,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Label className="text-sm font-medium mb-2 block">Default Value</Label>
<Input
placeholder="Default value"
value={column.defaultValue || ""}
onChange={(e) => updateColumn(index, "defaultValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
@@ -488,15 +540,51 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Input
type="number"
placeholder="Max length"
value={column.maxValue || ""}
onChange={(e) => updateColumn(index, "maxValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">Min Length</Label>
<Input
type="number"
placeholder="Min length"
value={column.minValue || ""}
onChange={(e) => updateColumn(index, "minValue", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">Auto Generate Pattern</Label>
<Input
placeholder="Auto Generate Pattern"
value={column.autoGeneratePattern || ""}
onChange={(e) => updateColumn(index, "autoGeneratePattern", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
/>
</div>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">Validator</Label>
<Textarea
placeholder="Column validator"
value={column.validator || ""}
onChange={(e) => updateColumn(index, "validator", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
rows={2}
/>
</div>
<div>
<Label className="text-sm font-medium mb-2 block">Description</Label>
<Textarea
placeholder="Column description"
value={column.description || ""}
onChange={(e) => updateColumn(index, "description", e.target.value)}
disabled={isSystem}
className={isSystem ? "bg-muted" : ""}
rows={2}
@@ -550,8 +638,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Textarea
id="list-rule"
placeholder='e.g., "True", "auth.id != None", ""'
value={rules.list}
onChange={(e) => setRules({ ...rules, list: e.target.value })}
value={rules.listRule}
onChange={(e) => setRules({ ...rules, listRule: e.target.value })}
className="mt-2"
rows={3}
/>
@@ -565,8 +653,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Textarea
id="get-rule"
placeholder='e.g., "True", "auth.id == record.user_id"'
value={rules.get}
onChange={(e) => setRules({ ...rules, get: e.target.value })}
value={rules.getRule}
onChange={(e) => setRules({ ...rules, getRule: e.target.value })}
className="mt-2"
rows={3}
/>
@@ -582,8 +670,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Textarea
id="add-rule"
placeholder='e.g., "auth.id != None", "auth.role == "admin""'
value={rules.add}
onChange={(e) => setRules({ ...rules, add: e.target.value })}
value={rules.addRule}
onChange={(e) => setRules({ ...rules, addRule: e.target.value })}
className="mt-2"
rows={3}
/>
@@ -597,8 +685,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Textarea
id="update-rule"
placeholder='e.g., "auth.id == record.user_id", ""'
value={rules.update}
onChange={(e) => setRules({ ...rules, update: e.target.value })}
value={rules.updateRule}
onChange={(e) => setRules({ ...rules, updateRule: e.target.value })}
className="mt-2"
rows={3}
/>
@@ -612,8 +700,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
<Textarea
id="delete-rule"
placeholder='e.g., "auth.role == "admin"", ""'
value={rules.delete}
onChange={(e) => setRules({ ...rules, delete: e.target.value })}
value={rules.deleteRule}
onChange={(e) => setRules({ ...rules, deleteRule: e.target.value })}
className="mt-2"
rows={3}
/>

View File

@@ -31,7 +31,9 @@ export function TableDetailView({ table, onBack, apiClient, onTableUpdate }: Tab
const [docsOpen, setDocsOpen] = React.useState(false)
const [editingItem, setEditingItem] = React.useState<any>(null)
const [selectedItems, setSelectedItems] = React.useState<string[]>([])
const [visibleColumns, setVisibleColumns] = React.useState<string[]>(table.fields?.map((field) => field.name) || [])
const [visibleColumns, setVisibleColumns] = React.useState<string[]>(
table.schema.fields?.map((field) => field.name) || [],
)
const [isDeleting, setIsDeleting] = React.useState(false)
const [filterTerm, setFilterTerm] = React.useState("")
const [appliedFilter, setAppliedFilter] = React.useState("")
@@ -41,8 +43,8 @@ export function TableDetailView({ table, onBack, apiClient, onTableUpdate }: Tab
}, [currentPage, appliedFilter])
React.useEffect(() => {
setVisibleColumns(table.fields?.map((field) => field.name) || [])
}, [table.fields])
setVisibleColumns(table.schema.fields?.map((field) => field.name) || [])
}, [table.schema.fields])
const loadTableData = async () => {
setIsLoading(true)
@@ -52,7 +54,7 @@ export function TableDetailView({ table, onBack, apiClient, onTableUpdate }: Tab
const id = `${currentPage}-${i + 1}`
const baseData: any = { id }
table.fields?.forEach((field) => {
table.schema.fields?.forEach((field) => {
if (field.name === "id") return
if (field.name === "created" || field.name === "updated") {
baseData[field.name] = new Date().toISOString()
@@ -149,7 +151,7 @@ export function TableDetailView({ table, onBack, apiClient, onTableUpdate }: Tab
}
}
const filteredFields = table.fields?.filter((field) => visibleColumns.includes(field.name)) || []
const filteredFields = table.schema.fields?.filter((field) => visibleColumns.includes(field.name)) || []
return (
<div className="space-y-6">

View File

@@ -56,7 +56,7 @@ export function TableDocsDrawer({ table, open, onClose }: TableDocsDrawerProps)
operation="get"
/>
{table.type !== "view" && (
{table.schema.type !== "view" && (
<>
<ApiEndpointCard
method="POST"
@@ -137,7 +137,7 @@ function ApiEndpointCard({
if (operation === "create" || operation === "update") {
const sampleData: any = {}
table.fields?.forEach((field) => {
table.schema.fields?.forEach((field) => {
if (field.name === "id" || field.name === "created" || field.name === "updated") return
if (field.name === "email") {
sampleData[field.name] = "user@example.com"
@@ -157,7 +157,7 @@ function ApiEndpointCard({
const generateResponseExample = () => {
if (operation === "list") {
const sampleRecord: any = {}
table.fields?.forEach((field) => {
table.schema.fields?.forEach((field) => {
if (field.name === "id") {
sampleRecord[field.name] = "123e4567-e89b-12d3-a456-426614174000"
} else if (field.name === "created" || field.name === "updated") {
@@ -192,7 +192,7 @@ function ApiEndpointCard({
// Single record response
const sampleRecord: any = {}
table.fields?.forEach((field) => {
table.schema.fields?.forEach((field) => {
if (field.name === "id") {
sampleRecord[field.name] = "123e4567-e89b-12d3-a456-426614174000"
} else if (field.name === "created" || field.name === "updated") {

View File

@@ -2,65 +2,288 @@
import type { AppMode } from "./app-state"
// Mock data for testing
const mockTables = [
// Updated interfaces to match the API response format
export interface TableField {
autoGeneratePattern: string | null
defaultValue: string | null
maxValue: number | null
minValue: number | null
name: string
primaryKey: boolean
required: boolean
system: boolean
type: string
unique: boolean
validator: string | null
}
export interface TableSchema {
addRule: string
deleteRule: string
fields: TableField[]
getRule: string
has_api: boolean
id: string
listRule: string
name: string
system: boolean
type: "base" | "auth" | "view"
updateRule: string
sql?: string
}
export interface TableMetadata {
created: string
has_api: boolean
id: string
name: string
schema: TableSchema
type: "base" | "auth" | "view"
updated: string
}
export interface Admin {
id: string
email: string
created: string
updated: string
}
export interface AppSettings {
appName: string
baseUrl: string
version: string
maintenanceMode: boolean
maxFileSize: string
allowRegistration: boolean
emailVerificationRequired: boolean
sessionTimeout: number
mode: AppMode
}
// Mock data updated to match new format
const mockTables: TableMetadata[] = [
{
id: "1",
created: "2025-06-21 22:04:15",
has_api: true,
id: "mt_11699830787864871553",
name: "users",
type: "auth" as const,
created: "2024-01-15T10:00:00Z",
updated: "2024-01-15T10:00:00Z",
rules: {
list: "auth.id != None",
get: "auth.id == record.id",
add: "True",
update: "auth.id == record.id",
delete: "auth.role == 'admin'",
schema: {
addRule: "",
deleteRule: "",
fields: [
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: null,
name: "id",
primaryKey: true,
required: true,
system: true,
type: "string",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: null,
name: "created",
primaryKey: false,
required: true,
system: true,
type: "date",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: null,
name: "updated",
primaryKey: false,
required: true,
system: true,
type: "date",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: 5.0,
name: "email",
primaryKey: false,
required: true,
system: true,
type: "string",
unique: true,
validator: "email",
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: 8.0,
name: "password",
primaryKey: false,
required: true,
system: true,
type: "string",
unique: false,
validator: "password",
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: 2.0,
name: "name",
primaryKey: false,
required: true,
system: true,
type: "string",
unique: false,
validator: null,
},
],
getRule: "",
has_api: true,
id: "mt_11699830787864871553",
listRule: "",
name: "users",
system: false,
type: "auth",
updateRule: "",
},
fields: [
{ name: "id", type: "string", primaryKey: true, nullable: false },
{ name: "email", type: "string", primaryKey: false, nullable: false },
{ name: "password", type: "string", primaryKey: false, nullable: false },
{ name: "created", type: "datetime", primaryKey: false, nullable: false },
{ name: "updated", type: "datetime", primaryKey: false, nullable: false },
],
type: "auth",
updated: "2025-06-21 22:04:15",
},
{
id: "2",
created: "2025-06-20 10:00:00",
has_api: true,
id: "mt_22699830787864871554",
name: "products",
type: "base" as const,
created: "2024-01-20T10:00:00Z",
updated: "2024-01-20T10:00:00Z",
rules: {
list: "True",
get: "True",
add: "auth.id != None",
update: "auth.id != None",
delete: "auth.role == 'admin'",
schema: {
addRule: "auth.id != None",
deleteRule: "auth.role == 'admin'",
fields: [
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: null,
name: "id",
primaryKey: true,
required: true,
system: true,
type: "string",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: null,
name: "created",
primaryKey: false,
required: true,
system: true,
type: "date",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: null,
name: "updated",
primaryKey: false,
required: true,
system: true,
type: "date",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: 1.0,
name: "name",
primaryKey: false,
required: true,
system: false,
type: "string",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: 999999.0,
minValue: 0.0,
name: "price",
primaryKey: false,
required: true,
system: false,
type: "float64",
unique: false,
validator: null,
},
{
autoGeneratePattern: null,
defaultValue: null,
maxValue: null,
minValue: null,
name: "category",
primaryKey: false,
required: false,
system: false,
type: "string",
unique: false,
validator: null,
},
],
getRule: "True",
has_api: true,
id: "mt_22699830787864871554",
listRule: "True",
name: "products",
system: false,
type: "base",
updateRule: "auth.id != None",
},
fields: [
{ name: "id", type: "string", primaryKey: true, nullable: false },
{ name: "name", type: "string", primaryKey: false, nullable: false },
{ name: "price", type: "double", primaryKey: false, nullable: false },
{ name: "category", type: "string", primaryKey: false, nullable: true },
{ name: "created", type: "datetime", primaryKey: false, nullable: false },
{ name: "updated", type: "datetime", primaryKey: false, nullable: false },
],
type: "base",
updated: "2025-06-20 10:00:00",
},
{
id: "3",
created: "2025-06-19 15:30:00",
has_api: true,
id: "mt_33699830787864871555",
name: "active_users_view",
type: "view" as const,
created: "2024-02-01T10:00:00Z",
updated: "2024-02-01T10:00:00Z",
rules: {
list: "auth.role == 'admin'",
get: "auth.role == 'admin'",
add: "",
update: "",
delete: "",
schema: {
addRule: "",
deleteRule: "",
fields: [],
getRule: "auth.role == 'admin'",
has_api: true,
id: "mt_33699830787864871555",
listRule: "auth.role == 'admin'",
name: "active_users_view",
system: false,
type: "view",
updateRule: "",
sql: "SELECT id, email, created FROM users WHERE updated > NOW() - INTERVAL 30 DAY",
},
sql: "SELECT id, email, created FROM users WHERE updated > NOW() - INTERVAL 30 DAY",
type: "view",
updated: "2025-06-19 15:30:00",
},
]
@@ -101,49 +324,6 @@ interface ApiResponse<T> {
// Simulate API delay
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
export interface TableMetadata {
id: string
name: string
type: "base" | "auth" | "view"
created: string
updated: string
rules: {
list: string
get: string
add: string
update: string
delete: string
}
fields?: Array<{
name: string
type: string
primaryKey: boolean
nullable: boolean
unique?: boolean
isFile?: boolean
}>
sql?: string
}
export interface Admin {
id: string
email: string
created: string
updated: string
}
export interface AppSettings {
appName: string
baseUrl: string
version: string
maintenanceMode: boolean
maxFileSize: string
allowRegistration: boolean
emailVerificationRequired: boolean
sessionTimeout: number
mode: AppMode
}
export class ApiClient {
private token: string
private onUnauthorized: () => void
@@ -215,10 +395,26 @@ export class ApiClient {
if (method === "POST") {
const body = JSON.parse(options.body as string)
const newTable: TableMetadata = {
id: String(mockTables.length + 1),
...body,
created: new Date().toISOString(),
updated: new Date().toISOString(),
id: `mt_${Date.now()}${Math.floor(Math.random() * 1000)}`,
name: body.name,
type: body.type,
has_api: true,
created: new Date().toISOString().replace("T", " ").slice(0, 19),
updated: new Date().toISOString().replace("T", " ").slice(0, 19),
schema: {
id: `mt_${Date.now()}${Math.floor(Math.random() * 1000)}`,
name: body.name,
type: body.type,
system: false,
has_api: true,
fields: body.fields || [],
addRule: body.addRule || "",
deleteRule: body.deleteRule || "",
getRule: body.getRule || "",
listRule: body.listRule || "",
updateRule: body.updateRule || "",
sql: body.sql,
},
}
mockTables.push(newTable)
return { data: newTable as T, status: 201 }
@@ -233,8 +429,12 @@ export class ApiClient {
const body = JSON.parse(options.body as string)
mockTables[tableIndex] = {
...mockTables[tableIndex],
...body,
updated: new Date().toISOString(),
updated: new Date().toISOString().replace("T", " ").slice(0, 19),
schema: {
...mockTables[tableIndex].schema,
...body,
fields: body.fields || mockTables[tableIndex].schema.fields,
},
}
return { data: mockTables[tableIndex] as T, status: 200 }
}