Skip to content

Commit 7c3c4ce

Browse files
authored
Merge pull request #75 from tablelandnetwork/table-schema
Standardize table schema format
2 parents 99678b2 + 8f45f90 commit 7c3c4ce

File tree

17 files changed

+249
-189
lines changed

17 files changed

+249
-189
lines changed

package-lock.json

Lines changed: 49 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "",
1414
"license": "ISC",
1515
"dependencies": {
16-
"@tableland/sdk": "^4.5.1",
16+
"@tableland/sdk": "^4.5.2",
1717
"@tableland/studio-mail": "^0.0.0",
1818
"@tableland/studio-store": "^1.0.0",
1919
"@trpc/server": "^10.38.4",

packages/api/src/routers/tables.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import { Validator, helpers } from "@tableland/sdk";
2-
import { Store } from "@tableland/studio-store";
2+
import { Schema, Store } from "@tableland/studio-store";
33
import { TRPCError } from "@trpc/server";
44
import { z } from "zod";
55
import { projectProcedure, publicProcedure, router } from "../trpc";
66

7+
const schemaSchema: z.ZodType<Schema> = z.object({
8+
columns: z.array(
9+
z.object({
10+
name: z.string().nonempty(),
11+
type: z.string().nonempty(),
12+
constraints: z.array(z.string().nonempty()).optional(),
13+
}),
14+
),
15+
table_constraints: z.array(z.string().nonempty()).optional(),
16+
});
17+
718
export function tablesRouter(store: Store) {
819
return router({
920
projectTables: publicProcedure
@@ -15,8 +26,8 @@ export function tablesRouter(store: Store) {
1526
.input(
1627
z.object({
1728
name: z.string(),
18-
schema: z.string(),
1929
description: z.string().nonempty(),
30+
schema: schemaSchema,
2031
}),
2132
)
2233
.mutation(async ({ input }) => {
@@ -47,12 +58,11 @@ export function tablesRouter(store: Store) {
4758
tableId: input.tableId,
4859
});
4960

50-
// TODO: Figure out a standard way of encoding schema for both Tables created in Studio and imported tables.
5161
const table = await store.tables.createTable(
5262
input.projectId,
5363
input.name,
5464
input.description,
55-
JSON.stringify(tablelandTable.schema),
65+
tablelandTable.schema,
5666
);
5767
const createdAttr = tablelandTable.attributes?.find(
5868
(attr) => attr.traitType === "created",

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"license": "MIT AND Apache-2.0",
4646
"dependencies": {
47-
"@tableland/sdk": "^4.5.1",
47+
"@tableland/sdk": "^4.5.2",
4848
"@tableland/sqlparser": "^1.3.0",
4949
"@tableland/studio-client": "^0.0.0",
5050
"@toruslabs/broadcast-channel": "^8.0.0",

packages/store/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "",
1414
"license": "ISC",
1515
"dependencies": {
16-
"@tableland/sdk": "^4.5.1",
16+
"@tableland/sdk": "^4.5.2",
1717
"drizzle-orm": "^0.28.5",
1818
"iron-session": "^6.3.1"
1919
}

packages/store/src/api/tables.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Database } from "@tableland/sdk";
22
import { randomUUID } from "crypto";
33
import { eq } from "drizzle-orm";
44
import { DrizzleD1Database } from "drizzle-orm/d1";
5+
import { Schema } from "../custom-types";
56
import * as schema from "../schema";
67
import { Table, projectTables, tables, teamProjects, teams } from "../schema";
78
import { slugify } from "./utils";
@@ -15,7 +16,7 @@ export function initTables(
1516
projectId: string,
1617
name: string,
1718
description: string,
18-
schema: string,
19+
schema: Schema,
1920
) {
2021
const tableId = randomUUID();
2122
const slug = slugify(name);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Schema as SDKSchema } from "@tableland/sdk";
2+
import { customType } from "drizzle-orm/sqlite-core";
3+
4+
export type Schema = SDKSchema;
5+
6+
export const schema = customType<{
7+
data: Schema;
8+
}>({
9+
dataType() {
10+
return "text";
11+
},
12+
fromDriver(value: unknown): Schema {
13+
return value as Schema;
14+
},
15+
toDriver(value: Schema): string {
16+
return JSON.stringify(value);
17+
},
18+
});

packages/store/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { init } from "./api";
2+
import { Schema } from "./custom-types";
23
import * as schema from "./schema";
34

45
type Store = ReturnType<typeof init>;
56

6-
export { Store, init, schema };
7+
export { Schema, Store, init, schema };

packages/store/src/schema/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
text,
77
uniqueIndex,
88
} from "drizzle-orm/sqlite-core";
9+
import { schema } from "../custom-types";
910

1011
export const users = sqliteTable(
1112
"users",
@@ -80,7 +81,7 @@ export const tables = sqliteTable("tables", {
8081
slug: text("slug").notNull(),
8182
name: text("name").notNull(),
8283
description: text("description").notNull(),
83-
schema: text("schema").notNull(),
84+
schema: schema("schema").notNull(),
8485
});
8586

8687
export const projectTables = sqliteTable(

packages/web/app/[team]/[project]/(project)/deployments/[[...slug]]/_components/exec-deployment.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
SelectTrigger,
1919
SelectValue,
2020
} from "@/components/ui/select";
21+
import { generateCreateTableStatement } from "@/lib/schema";
2122
import { cn } from "@/lib/utils";
2223
import {
2324
Database,
@@ -108,8 +109,9 @@ export default function ExecDeployment({
108109
baseUrl: helpers.getBaseUrl(chainId),
109110
autoWait: false,
110111
});
111-
// TODO: Table.schema will be JSON, convert it to SQL create table statement.
112-
const res = await tbl.exec(table.schema);
112+
113+
const stmt = generateCreateTableStatement(table.name, table.schema);
114+
const res = await tbl.exec(stmt);
113115
if (res.error) {
114116
throw new Error(res.error);
115117
}

0 commit comments

Comments
 (0)