update schema + working sandbox creation route

This commit is contained in:
Ishaan Dey 2024-04-24 02:22:06 -04:00
parent b6097df612
commit f4c3d43b87
5 changed files with 142 additions and 11 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE `sandbox` DROP COLUMN `bucket`;--> statement-breakpoint
ALTER TABLE `sandbox` DROP COLUMN `init`;

View File

@ -0,0 +1,111 @@
{
"version": "5",
"dialect": "sqlite",
"id": "739c5df4-de1b-408b-bc8c-5783688c5297",
"prevId": "9c09f493-3a05-496c-997e-b527c8f17cf9",
"tables": {
"sandbox": {
"name": "sandbox",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"type": {
"name": "type",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"sandbox_id_unique": {
"name": "sandbox_id_unique",
"columns": [
"id"
],
"isUnique": true
}
},
"foreignKeys": {
"sandbox_user_id_user_id_fk": {
"name": "sandbox_user_id_user_id_fk",
"tableFrom": "sandbox",
"tableTo": "user",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"user": {
"name": "user",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"user_id_unique": {
"name": "user_id_unique",
"columns": [
"id"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}

View File

@ -8,6 +8,13 @@
"when": 1713849093097,
"tag": "0000_blushing_surge",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1713937589365,
"tag": "0001_magenta_tenebrous",
"breakpoints": true
}
]
}

View File

@ -16,6 +16,7 @@ export interface Env {
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const success = new Response("Success", { status: 200 });
const invalidRequest = new Response("Invalid Request", { status: 400 });
const notFound = new Response("Not Found", { status: 404 });
const methodNotAllowed = new Response("Method Not Allowed", { status: 405 });
@ -25,17 +26,29 @@ export default {
const db = drizzle(env.DB, { schema });
// if (path === "/api/sandbox/create") {
// if (method === "POST") {}
// else return methodNotAllowed;
// } else if (path === "/api/sandbox/init") {
// const params = url.searchParams;
if (path === "/api/sandbox/create" && method === "POST") {
const initSchema = z.object({
type: z.enum(["react", "node"]),
name: z.string(),
userId: z.string(),
});
const body = await request.json();
const { type, name, userId } = initSchema.parse(body);
const sb = await db.insert(sandbox).values({ type, name, userId }).returning().get();
console.log("sb:", sb);
await fetch("https://storage.ishaan1013.workers.dev/api/init", {
method: "POST",
body: JSON.stringify({ sandboxId: sb.id, type }),
headers: { "Content-Type": "application/json" },
});
return success;
// await db.update(sandbox).set({ init: true }).where(eq(sandbox.id, sandboxId));
// } else if (path === "/api/sandbox/files") {
// } else
if (path === "/api/user") {
} else if (path === "/api/user") {
if (method === "GET") {
const params = url.searchParams;

View File

@ -24,8 +24,6 @@ export const sandbox = sqliteTable("sandbox", {
.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),