fix db relations + display sandboxes on dash
This commit is contained in:
7
backend/database/drizzle/0001_right_omega_sentinel.sql
Normal file
7
backend/database/drizzle/0001_right_omega_sentinel.sql
Normal file
@ -0,0 +1,7 @@
|
||||
/*
|
||||
SQLite does not support "Dropping foreign key" out of the box, we do not generate automatic migration for that, so it has to be done manually
|
||||
Please refer to: https://www.techonthenet.com/sqlite/tables/alter_table.php
|
||||
https://www.sqlite.org/lang_altertable.html
|
||||
|
||||
Due to that we don't generate migration automatically and it has to be done manually
|
||||
*/
|
1
backend/database/drizzle/0002_curvy_darkhawk.sql
Normal file
1
backend/database/drizzle/0002_curvy_darkhawk.sql
Normal file
@ -0,0 +1 @@
|
||||
ALTER TABLE `sandbox` RENAME COLUMN `text` TO `type`;
|
1
backend/database/drizzle/0003_outgoing_hammerhead.sql
Normal file
1
backend/database/drizzle/0003_outgoing_hammerhead.sql
Normal file
@ -0,0 +1 @@
|
||||
ALTER TABLE sandbox ADD `bucket` text;
|
97
backend/database/drizzle/meta/0001_snapshot.json
Normal file
97
backend/database/drizzle/meta/0001_snapshot.json
Normal file
@ -0,0 +1,97 @@
|
||||
{
|
||||
"version": "5",
|
||||
"dialect": "sqlite",
|
||||
"id": "a6b962d3-cabf-464f-9098-c70ed82e94dc",
|
||||
"prevId": "af9c2bc3-e4c9-42f6-bf73-bb2399b894c7",
|
||||
"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
|
||||
},
|
||||
"text": {
|
||||
"name": "text",
|
||||
"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": {},
|
||||
"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": {}
|
||||
}
|
||||
}
|
113
backend/database/drizzle/meta/0002_snapshot.json
Normal file
113
backend/database/drizzle/meta/0002_snapshot.json
Normal file
@ -0,0 +1,113 @@
|
||||
{
|
||||
"version": "5",
|
||||
"dialect": "sqlite",
|
||||
"id": "dda436cb-b614-4fc2-8764-7c2b80ac9e07",
|
||||
"prevId": "a6b962d3-cabf-464f-9098-c70ed82e94dc",
|
||||
"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": {
|
||||
"\"sandbox\".\"text\"": "\"sandbox\".\"type\""
|
||||
}
|
||||
}
|
||||
}
|
118
backend/database/drizzle/meta/0003_snapshot.json
Normal file
118
backend/database/drizzle/meta/0003_snapshot.json
Normal file
@ -0,0 +1,118 @@
|
||||
{
|
||||
"version": "5",
|
||||
"dialect": "sqlite",
|
||||
"id": "90f199c9-66a1-4c89-9a19-f7fbd5a35236",
|
||||
"prevId": "dda436cb-b614-4fc2-8764-7c2b80ac9e07",
|
||||
"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
|
||||
},
|
||||
"bucket": {
|
||||
"name": "bucket",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"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": {}
|
||||
}
|
||||
}
|
@ -8,6 +8,27 @@
|
||||
"when": 1713306026634,
|
||||
"tag": "0000_sparkling_morg",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 1,
|
||||
"version": "5",
|
||||
"when": 1713411309767,
|
||||
"tag": "0001_right_omega_sentinel",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 2,
|
||||
"version": "5",
|
||||
"when": 1713411654625,
|
||||
"tag": "0002_curvy_darkhawk",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 3,
|
||||
"version": "5",
|
||||
"when": 1713412439820,
|
||||
"tag": "0003_outgoing_hammerhead",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
@ -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 });
|
||||
},
|
||||
|
@ -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],
|
||||
}),
|
||||
}));
|
||||
|
Reference in New Issue
Block a user