mirror of
https://github.com/khairul169/code-share.git
synced 2026-10-24 11:53:58 +07:00
feat: rebuild app using vike
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import path from "node:path";
|
||||
import Database from "better-sqlite3";
|
||||
import * as schema from "./schema/_schema";
|
||||
|
||||
const sqlite = new Database(path.join(process.cwd(), "storage/database.db"));
|
||||
const db = drizzle(sqlite, { schema });
|
||||
|
||||
export default db;
|
||||
@@ -0,0 +1,5 @@
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import db from "./index";
|
||||
|
||||
migrate(db, { migrationsFolder: __dirname + "/drizzle" });
|
||||
process.exit();
|
||||
@@ -0,0 +1,2 @@
|
||||
export { user } from "./user";
|
||||
export { file } from "./file";
|
||||
@@ -0,0 +1,47 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import {
|
||||
integer,
|
||||
sqliteTable,
|
||||
text,
|
||||
foreignKey,
|
||||
} from "drizzle-orm/sqlite-core";
|
||||
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { user } from "./user";
|
||||
|
||||
export const file = sqliteTable(
|
||||
"files",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
parentId: integer("parent_id"),
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
path: text("path").notNull().unique(),
|
||||
filename: text("filename").notNull(),
|
||||
isDirectory: integer("is_directory", { mode: "boolean" })
|
||||
.notNull()
|
||||
.default(false),
|
||||
isFile: integer("is_file", { mode: "boolean" }).notNull().default(false),
|
||||
isPinned: integer("is_pinned", { mode: "boolean" })
|
||||
.notNull()
|
||||
.default(false),
|
||||
content: text("content"),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`CURRENT_TIMESTAMP`),
|
||||
deletedAt: text("deleted_at"),
|
||||
},
|
||||
(table) => ({
|
||||
parentFk: foreignKey({
|
||||
columns: [table.parentId],
|
||||
foreignColumns: [table.id],
|
||||
name: "parent_id_fk",
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
export const insertFileSchema = createInsertSchema(file);
|
||||
export const selectFileSchema = createSelectSchema(file);
|
||||
|
||||
export type FileSchema = z.infer<typeof selectFileSchema>;
|
||||
@@ -0,0 +1,19 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
|
||||
export const user = sqliteTable("users", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
email: text("email").notNull().unique(),
|
||||
password: text("password").notNull(),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`CURRENT_TIMESTAMP`),
|
||||
deletedAt: text("deleted_at"),
|
||||
});
|
||||
|
||||
export const insertUserSchema = createInsertSchema(user);
|
||||
export const selectUserSchema = createSelectSchema(user);
|
||||
|
||||
export type UserSchema = z.infer<typeof selectUserSchema>;
|
||||
@@ -0,0 +1,63 @@
|
||||
import db from ".";
|
||||
import { hashPassword } from "../lib/crypto";
|
||||
import { file } from "./schema/file";
|
||||
import { user } from "./schema/user";
|
||||
|
||||
const main = async () => {
|
||||
const [adminUser] = await db
|
||||
.insert(user)
|
||||
.values({
|
||||
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>
|
||||
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
</head>
|
||||
<body>
|
||||
{CONTENT}
|
||||
<script src="script.js" type="module" defer></script>
|
||||
</body>
|
||||
</html>`,
|
||||
},
|
||||
])
|
||||
.execute();
|
||||
|
||||
process.exit();
|
||||
};
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user