fix db relations + display sandboxes on dash

This commit is contained in:
Ishaan Dey
2024-04-18 00:13:40 -04:00
parent 14d76c605e
commit 19c2992824
13 changed files with 458 additions and 36 deletions

View File

@ -7,6 +7,7 @@ import { json } from "itty-router-extras";
import { ZodError, z } from "zod";
import { user, sandbox } from "./schema";
import * as schema from "./schema";
export interface Env {
DB: D1Database;
@ -18,35 +19,53 @@ export default {
const path = url.pathname;
const method = request.method;
const db = drizzle(env.DB);
const db = drizzle(env.DB, { schema });
if (path === "/api/user") {
if (method === "GET") {
if (path.startsWith("/api/user")) {
if (path === "/api/user") {
if (method === "GET") {
const params = url.searchParams;
if (params.has("id")) {
const id = params.get("id") as string;
const res = await db.select().from(user).where(eq(user.id, id)).get();
console.log(res);
return json(res ?? {});
} else {
const res = await db.select().from(user).all();
return json(res ?? {});
}
} else if (method === "POST") {
const userSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email(),
});
const body = await request.json();
const { id, name, email } = userSchema.parse(body);
const res = await db.insert(user).values({ id, name, email }).returning().get();
return json({ res });
} else {
return new Response("Method Not Allowed", { status: 405 });
}
} else if (path === "/api/user/sandbox") {
const params = url.searchParams;
if (params.has("id")) {
if (method === "GET" && params.has("id")) {
const id = params.get("id") as string;
const res = await db.select().from(user).where(eq(user.id, id)).get();
console.log(res);
const res = await db.query.user.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: {
sandbox: true,
},
});
return json(res ?? {});
} else {
const res = await db.select().from(user).all();
return json(res ?? {});
return new Response("Method Not Allowed", { status: 405 });
}
} else if (method === "POST") {
const userSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email(),
});
const body = await request.json();
const { id, name, email } = userSchema.parse(body);
const res = await db.insert(user).values({ id, name, email }).returning().get();
return json({ res });
} else {
return new Response("Method Not Allowed", { status: 405 });
return new Response("Not Found", { status: 404 });
}
} else return new Response("Not Found", { status: 404 });
},

View File

@ -1,5 +1,6 @@
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { createId } from "@paralleldrive/cuid2";
import { relations } from "drizzle-orm";
export const user = sqliteTable("user", {
id: text("id")
@ -10,14 +11,30 @@ export const user = sqliteTable("user", {
email: text("email").notNull(),
});
export type User = typeof user.$inferSelect;
export const userRelations = relations(user, ({ many }) => ({
sandbox: many(sandbox),
}));
export const sandbox = sqliteTable("sandbox", {
id: text("id")
.$defaultFn(() => createId())
.primaryKey()
.unique(),
name: text("name").notNull(),
type: text("text", { enum: ["react", "node"] }).notNull(),
type: text("type", { enum: ["react", "node"] }).notNull(),
bucket: text("bucket"),
userId: text("user_id")
.notNull()
.references(() => user.id),
});
export type Sandbox = typeof sandbox.$inferSelect;
export const sandboxRelations = relations(sandbox, ({ one }) => ({
author: one(user, {
fields: [sandbox.userId],
references: [user.id],
}),
}));