diff --git a/backend/database/drizzle/0001_magenta_tenebrous.sql b/backend/database/drizzle/0001_magenta_tenebrous.sql new file mode 100644 index 0000000..45fb9f4 --- /dev/null +++ b/backend/database/drizzle/0001_magenta_tenebrous.sql @@ -0,0 +1,2 @@ +ALTER TABLE `sandbox` DROP COLUMN `bucket`;--> statement-breakpoint +ALTER TABLE `sandbox` DROP COLUMN `init`; \ No newline at end of file diff --git a/backend/database/drizzle/meta/0001_snapshot.json b/backend/database/drizzle/meta/0001_snapshot.json new file mode 100644 index 0000000..fcbe687 --- /dev/null +++ b/backend/database/drizzle/meta/0001_snapshot.json @@ -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": {} + } +} \ No newline at end of file diff --git a/backend/database/drizzle/meta/_journal.json b/backend/database/drizzle/meta/_journal.json index f2d7f20..12e8317 100644 --- a/backend/database/drizzle/meta/_journal.json +++ b/backend/database/drizzle/meta/_journal.json @@ -8,6 +8,13 @@ "when": 1713849093097, "tag": "0000_blushing_surge", "breakpoints": true + }, + { + "idx": 1, + "version": "5", + "when": 1713937589365, + "tag": "0001_magenta_tenebrous", + "breakpoints": true } ] } \ No newline at end of file diff --git a/backend/database/src/index.ts b/backend/database/src/index.ts index 2561d0b..1423579 100644 --- a/backend/database/src/index.ts +++ b/backend/database/src/index.ts @@ -16,6 +16,7 @@ export interface Env { export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { 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(), + }); - // await db.update(sandbox).set({ init: true }).where(eq(sandbox.id, sandboxId)); - // } else if (path === "/api/sandbox/files") { + const body = await request.json(); + const { type, name, userId } = initSchema.parse(body); - // } else - if (path === "/api/user") { + 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; + + // } else if (path === "/api/sandbox/files") { + } else if (path === "/api/user") { if (method === "GET") { const params = url.searchParams; diff --git a/backend/database/src/schema.ts b/backend/database/src/schema.ts index bf33478..8bf466c 100644 --- a/backend/database/src/schema.ts +++ b/backend/database/src/schema.ts @@ -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),