split db and storage workers

This commit is contained in:
Ishaan Dey
2024-04-22 00:30:50 -04:00
parent 5375301040
commit 54617877f9
36 changed files with 3631 additions and 176 deletions

View File

@ -0,0 +1,72 @@
import type { DrizzleD1Database } from "drizzle-orm/d1";
import { drizzle } from "drizzle-orm/d1";
import { json } from "itty-router-extras";
import { ZodError, z } from "zod";
import { user, sandbox } from "./schema";
import * as schema from "./schema";
import { eq } from "drizzle-orm";
const success = new Response("Success", { status: 200 });
const notFound = new Response("Not Found", { status: 404 });
const methodNotAllowed = new Response("Method Not Allowed", { status: 405 });
export interface Env {
DB: D1Database;
R2: R2Bucket;
}
// https://github.com/drizzle-team/drizzle-orm/tree/main/examples/cloudflare-d1
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
const path = url.pathname;
const method = request.method;
const db = drizzle(env.DB, { schema });
// ^\/api\/sandbox\/([^\/]+)\/((init|files))$
if (new RegExp("^/api/sandbox/([^/]+)/((init|files))$").test(path)) {
const sandboxId = path.split("/")[2];
const command = path.split("/")[3] as "init" | "files";
if (command === "init") {
await db.update(sandbox).set({ init: true }).where(eq(sandbox.id, sandboxId));
}
} else 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.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 ?? {});
}
} 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 methodNotAllowed;
}
} else return notFound;
},
};

View File

@ -0,0 +1,41 @@
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")
.$defaultFn(() => createId())
.primaryKey()
.unique(),
name: text("name").notNull(),
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("type", { enum: ["react", "node"] }).notNull(),
bucket: text("bucket"),
init: integer("init", { mode: "boolean" }).default(false),
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],
}),
}));