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

@ -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
*/

View File

@ -0,0 +1 @@
ALTER TABLE `sandbox` RENAME COLUMN `text` TO `type`;

View File

@ -0,0 +1 @@
ALTER TABLE sandbox ADD `bucket` text;

View 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": {}
}
}

View 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\""
}
}
}

View 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": {}
}
}

View File

@ -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
}
]
}

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,8 +19,9 @@ export default {
const path = url.pathname;
const method = request.method;
const db = drizzle(env.DB);
const db = drizzle(env.DB, { schema });
if (path.startsWith("/api/user")) {
if (path === "/api/user") {
if (method === "GET") {
const params = url.searchParams;
@ -48,6 +50,23 @@ export default {
} else {
return new Response("Method Not Allowed", { status: 405 });
}
} else if (path === "/api/user/sandbox") {
const params = url.searchParams;
if (method === "GET" && 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 {
return new Response("Method Not Allowed", { status: 405 });
}
} else {
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],
}),
}));

View File

@ -2,6 +2,7 @@ import { UserButton, currentUser } from "@clerk/nextjs"
import { redirect } from "next/navigation"
import Dashboard from "@/components/dashboard"
import Navbar from "@/components/dashboard/navbar"
import { Sandbox } from "@/lib/types"
export default async function DashboardPage() {
const user = await currentUser()
@ -10,10 +11,17 @@ export default async function DashboardPage() {
redirect("/")
}
const res = await fetch(
`http://localhost:8787/api/user/sandbox?id=${user.id}`
)
const data = (await res.json()).sandbox as Sandbox[]
console.log(data)
return (
<div className="w-screen h-screen flex flex-col overflow-hidden overscroll-none">
<Navbar />
<Dashboard />
<Dashboard sandboxes={data} />
</div>
)
}

View File

@ -27,6 +27,8 @@ export default async function AppAuthLayout({
email: user.emailAddresses[0].emailAddress,
}),
})
console.log(res)
} else {
// user already exists in db
}

View File

@ -18,10 +18,11 @@ import {
import { useState } from "react"
import { Card } from "../ui/card"
import ProjectCard from "./projectCard"
import { Sandbox } from "@/lib/types"
type TScreen = "projects" | "shared" | "settings" | "search"
export default function Dashboard() {
export default function Dashboard({ sandboxes }: { sandboxes: Sandbox[] }) {
const [screen, setScreen] = useState<TScreen>("projects")
const activeScreen = (s: TScreen) => {
@ -80,9 +81,10 @@ export default function Dashboard() {
</div>
</div>
<div className="grow p-4 grid lg:grid-cols-4 xl:grid-cols-5 md:grid-cols-3 gap-4">
<ProjectCard>
{sandboxes.map((sandbox) => (
<ProjectCard key={sandbox.id}>
<div className="font-medium flex items-center whitespace-nowrap w-full text-ellipsis overflow-hidden">
React Project 1
{sandbox.name}
</div>
<div className="flex flex-col text-muted-foreground space-y-0.5 text-sm">
<div className="flex items-center">
@ -93,6 +95,7 @@ export default function Dashboard() {
</div>
</div>
</ProjectCard>
))}
</div>
</div>
)

15
frontend/lib/types.ts Normal file
View File

@ -0,0 +1,15 @@
// DB Types
export type User = {
id: string
name: string
email: string
}
export type Sandbox = {
id: string
name: string
type: "react" | "node"
bucket: string | null
userId: string
}