-
Notifications
You must be signed in to change notification settings - Fork 1
Standardize table schema format #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4b6a256
f94f48b
c548837
e198e05
dc408e4
c0b5382
8f45f90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,20 @@ | ||
| import { Validator, helpers } from "@tableland/sdk"; | ||
| import { Store } from "@tableland/studio-store"; | ||
| import { Schema, Store } from "@tableland/studio-store"; | ||
| import { TRPCError } from "@trpc/server"; | ||
| import { z } from "zod"; | ||
| import { projectProcedure, publicProcedure, router } from "../trpc"; | ||
|
|
||
| const schemaSchema: z.ZodType<Schema> = z.object({ | ||
| columns: z.array( | ||
| z.object({ | ||
| name: z.string().nonempty(), | ||
| type: z.string().nonempty(), | ||
| constraints: z.array(z.string().nonempty()).optional(), | ||
| }), | ||
| ), | ||
| table_constraints: z.array(z.string().nonempty()).optional(), | ||
| }); | ||
|
|
||
| export function tablesRouter(store: Store) { | ||
| return router({ | ||
| projectTables: publicProcedure | ||
|
|
@@ -15,8 +26,8 @@ export function tablesRouter(store: Store) { | |
| .input( | ||
| z.object({ | ||
| name: z.string(), | ||
| schema: z.string(), | ||
| description: z.string().nonempty(), | ||
| schema: schemaSchema, | ||
| }), | ||
| ) | ||
| .mutation(async ({ input }) => { | ||
|
|
@@ -47,12 +58,11 @@ export function tablesRouter(store: Store) { | |
| tableId: input.tableId, | ||
| }); | ||
|
|
||
| // TODO: Figure out a standard way of encoding schema for both Tables created in Studio and imported tables. | ||
| const table = await store.tables.createTable( | ||
| input.projectId, | ||
| input.name, | ||
| input.description, | ||
| JSON.stringify(tablelandTable.schema), | ||
| tablelandTable.schema, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty cool, just send the |
||
| ); | ||
| const createdAttr = tablelandTable.attributes?.find( | ||
| (attr) => attr.traitType === "created", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Schema as SDKSchema } from "@tableland/sdk"; | ||
| import { customType } from "drizzle-orm/sqlite-core"; | ||
|
|
||
| export type Schema = SDKSchema; | ||
|
|
||
| export const schema = customType<{ | ||
| data: Schema; | ||
| }>({ | ||
| dataType() { | ||
| return "text"; | ||
| }, | ||
| fromDriver(value: unknown): Schema { | ||
| return value as Schema; | ||
| }, | ||
| toDriver(value: Schema): string { | ||
| return JSON.stringify(value); | ||
| }, | ||
| }); | ||
|
Comment on lines
+4
to
+18
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our custom drizzle column type. I had to use |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { init } from "./api"; | ||
| import { Schema } from "./custom-types"; | ||
| import * as schema from "./schema"; | ||
|
|
||
| type Store = ReturnType<typeof init>; | ||
|
|
||
| export { Store, init, schema }; | ||
| export { Schema, Store, init, schema }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { | |
| text, | ||
| uniqueIndex, | ||
| } from "drizzle-orm/sqlite-core"; | ||
| import { schema } from "../custom-types"; | ||
|
|
||
| export const users = sqliteTable( | ||
| "users", | ||
|
|
@@ -80,7 +81,7 @@ export const tables = sqliteTable("tables", { | |
| slug: text("slug").notNull(), | ||
| name: text("name").notNull(), | ||
| description: text("description").notNull(), | ||
| schema: text("schema").notNull(), | ||
| schema: schema("schema").notNull(), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the custom type in the table definition. I verified this doesn't effect the generated migration (create table statement) because it still just creates a |
||
| }); | ||
|
|
||
| export const projectTables = sqliteTable( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import { | |
| SelectTrigger, | ||
| SelectValue, | ||
| } from "@/components/ui/select"; | ||
| import { generateCreateTableStatement } from "@/lib/schema"; | ||
| import { cn } from "@/lib/utils"; | ||
| import { | ||
| Database, | ||
|
|
@@ -108,8 +109,9 @@ export default function ExecDeployment({ | |
| baseUrl: helpers.getBaseUrl(chainId), | ||
| autoWait: false, | ||
| }); | ||
| // TODO: Table.schema will be JSON, convert it to SQL create table statement. | ||
| const res = await tbl.exec(table.schema); | ||
|
|
||
| const stmt = generateCreateTableStatement(table.name, table.schema); | ||
| const res = await tbl.exec(stmt); | ||
|
Comment on lines
+112
to
+114
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convert the |
||
| if (res.error) { | ||
| throw new Error(res.error); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,7 @@ | ||
| "use client"; | ||
|
|
||
| import { newTable } from "@/app/actions"; | ||
| import SchemaBuilder, { | ||
| createTableStatementFromObject, | ||
| } from "@/components/schema-builder"; | ||
| import SchemaBuilder from "@/components/schema-builder"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { | ||
| Form, | ||
|
|
@@ -16,14 +14,13 @@ import { | |
| } from "@/components/ui/form"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Textarea } from "@/components/ui/textarea"; | ||
| import { createTableAtom } from "@/store/create-table"; | ||
| import { cleanSchema, generateCreateTableStatement } from "@/lib/schema"; | ||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import { helpers } from "@tableland/sdk"; | ||
| import { schema } from "@tableland/studio-store"; | ||
| import { useAtom } from "jotai"; | ||
| import { Schema, schema } from "@tableland/studio-store"; | ||
| import { Loader2 } from "lucide-react"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { useTransition } from "react"; | ||
| import { useState, useTransition } from "react"; | ||
| import { useFieldArray, useForm } from "react-hook-form"; | ||
| import * as z from "zod"; | ||
|
|
||
|
|
@@ -51,7 +48,7 @@ export default function NewTable({ project, team, envs }: Props) { | |
| const [pending, startTransition] = useTransition(); | ||
| const router = useRouter(); | ||
|
|
||
| const [createTable, setCreateTable] = useAtom(createTableAtom); | ||
| const [schema, setSchema] = useState<Schema>({ columns: [] }); | ||
|
|
||
| const form = useForm<z.infer<typeof formSchema>>({ | ||
| resolver: zodResolver(formSchema), | ||
|
|
@@ -73,17 +70,13 @@ export default function NewTable({ project, team, envs }: Props) { | |
|
|
||
| function onSubmit(values: z.infer<typeof formSchema>) { | ||
| startTransition(async () => { | ||
| const statement = createTableStatementFromObject( | ||
| createTable, | ||
| await newTable( | ||
| project, | ||
| values.name, | ||
| values.description, | ||
| cleanSchema(schema), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ); | ||
| if (!statement) { | ||
| console.error("No statement"); | ||
| return; | ||
| } | ||
| await newTable(project, values.name, statement, values.description); | ||
| router.replace(`/${team.slug}/${project.slug}`); | ||
| setCreateTable({ columns: [] }); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -128,8 +121,8 @@ export default function NewTable({ project, team, envs }: Props) { | |
| /> | ||
| <div className="space-y-2"> | ||
| <FormLabel>Columns</FormLabel> | ||
| <SchemaBuilder /> | ||
| <pre>{createTableStatementFromObject(createTable, name)}</pre> | ||
| <SchemaBuilder schema={schema} setSchema={setSchema} /> | ||
| <pre>{generateCreateTableStatement(name, schema)}</pre> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Display the equivalent create statement in real time. |
||
| </div> | ||
| {/* <div className="space-y-2"> | ||
| <FormLabel>Deployments</FormLabel> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the best way I could find to create an api input definition that conforms to the
Schematype. At least if theSchematype changes, this will cause a TS error.