feat: add project schema

This commit is contained in:
2024-02-22 20:59:20 +00:00
parent b232410d83
commit d579c1b7c5
27 changed files with 338 additions and 147 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
export { user } from "./user";
export { file } from "./file";
export { project, projectRelations } from "./project";
export { file, fileRelations } from "./file";
+16 -6
View File
@@ -1,23 +1,24 @@
import { sql } from "drizzle-orm";
import { relations, sql } from "drizzle-orm";
import {
integer,
sqliteTable,
text,
foreignKey,
index,
} from "drizzle-orm/sqlite-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import { user } from "./user";
import { project } from "./project";
export const file = sqliteTable(
"files",
{
id: integer("id").primaryKey({ autoIncrement: true }),
parentId: integer("parent_id"),
userId: integer("user_id")
projectId: integer("project_id")
.notNull()
.references(() => user.id),
path: text("path").notNull().unique(),
.references(() => project.id),
parentId: integer("parent_id"),
path: text("path").notNull(),
filename: text("filename").notNull(),
isDirectory: integer("is_directory", { mode: "boolean" })
.notNull()
@@ -38,9 +39,18 @@ export const file = sqliteTable(
foreignColumns: [table.id],
name: "parent_id_fk",
}),
pathIdx: index("file_path_idx").on(table.path),
filenameIdx: index("file_name_idx").on(table.filename),
})
);
export const fileRelations = relations(file, ({ one }) => ({
project: one(project, {
fields: [file.projectId],
references: [project.id],
}),
}));
export const insertFileSchema = createInsertSchema(file);
export const selectFileSchema = createSelectSchema(file);
+32
View File
@@ -0,0 +1,32 @@
import { relations, sql } from "drizzle-orm";
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import { user } from "./user";
import { file } from "./file";
export const project = sqliteTable("projects", {
id: integer("id").primaryKey({ autoIncrement: true }),
userId: integer("user_id")
.notNull()
.references(() => user.id),
slug: text("slug").notNull().unique(),
title: text("title").notNull(),
createdAt: text("created_at")
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
deletedAt: text("deleted_at"),
});
export const projectRelations = relations(project, ({ one, many }) => ({
files: many(file),
user: one(user, {
fields: [project.userId],
references: [user.id],
}),
}));
export const insertProjectSchema = createInsertSchema(project);
export const selectProjectSchema = createSelectSchema(project);
export type ProjectSchema = z.infer<typeof selectProjectSchema>;
+1
View File
@@ -5,6 +5,7 @@ import { z } from "zod";
export const user = sqliteTable("users", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
email: text("email").notNull().unique(),
password: text("password").notNull(),
createdAt: text("created_at")
+61 -46
View File
@@ -1,68 +1,83 @@
import db from ".";
import { hashPassword } from "../lib/crypto";
import { uid } from "../lib/utils";
import { file } from "./schema/file";
import { project } from "./schema/project";
import { user } from "./schema/user";
const main = async () => {
const [adminUser] = await db
.insert(user)
.values({
name: "Admin",
email: "[email protected]",
password: await hashPassword("123456"),
})
.returning();
// await db
// .insert(file)
// .values([
// {
// userId: adminUser.id,
// path: "index.html",
// filename: "index.html",
// content: '<p class="text-lg text-red-500">Hello world!</p>',
// },
// {
// userId: adminUser.id,
// path: "styles.css",
// filename: "styles.css",
// content: "body { padding: 16px; }",
// },
// {
// userId: adminUser.id,
// path: "script.js",
// filename: "script.js",
// content: "console.log('hello world!');",
// },
// {
// userId: adminUser.id,
// path: "_layout.html",
// filename: "_layout.html",
// content: `<!DOCTYPE html>
// <html lang="en">
// <head>
// <meta charset="UTF-8">
// <meta name="viewport" content="width=device-width, initial-scale=1.0">
// <title>Document</title>
const [vanillaProject] = await db
.insert(project)
.values({ userId: adminUser.id, slug: uid(), title: "Vanilla Project" })
.returning();
// <link rel="stylesheet" href="styles.css">
// vanilla html css js template
await db
.insert(file)
.values([
{
projectId: vanillaProject.id,
path: "index.html",
filename: "index.html",
content: "<p>Hello world!</p>",
isPinned: true,
},
{
projectId: vanillaProject.id,
path: "styles.css",
filename: "styles.css",
content: "body { padding: 16px; }",
isPinned: true,
},
{
projectId: vanillaProject.id,
path: "scripts.js",
filename: "scripts.js",
content: "console.log('hello world!');",
isPinned: true,
},
{
projectId: vanillaProject.id,
path: "_layout.html",
filename: "_layout.html",
content: `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
// <script src="https://cdn.tailwindcss.com"></script>
// </head>
// <body>
// {CONTENT}
// <script src="script.js" type="module" defer></script>
// </body>
// </html>`,
// },
// ])
// .execute();
<link rel="stylesheet" href="styles.css">
</head>
<body>
{CONTENT}
<script src="scripts.js"></script>
</body>
</html>`,
},
])
.execute();
const [reactProject] = await db
.insert(project)
.values({ userId: adminUser.id, slug: uid(), title: "React Project" })
.returning();
// react template
await db
.insert(file)
.values([
{
userId: adminUser.id,
projectId: reactProject.id,
path: "index.html",
filename: "index.html",
content: `<!doctype html>
@@ -84,7 +99,7 @@ const main = async () => {
`,
},
{
userId: adminUser.id,
projectId: reactProject.id,
path: "globals.css",
filename: "globals.css",
content: `@tailwind base;
@@ -97,7 +112,7 @@ body {
`,
},
{
userId: adminUser.id,
projectId: reactProject.id,
path: "index.jsx",
filename: "index.jsx",
content: `import React from "react";
@@ -109,7 +124,7 @@ root.render(<App />);
`,
},
{
userId: adminUser.id,
projectId: reactProject.id,
path: "App.jsx",
filename: "App.jsx",
isPinned: true,