58 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2024-02-22 20:59:20 +00:00
import { relations, sql } from "drizzle-orm";
2024-02-20 06:52:39 +00:00
import {
integer,
sqliteTable,
text,
foreignKey,
2024-02-22 20:59:20 +00:00
index,
2024-02-20 06:52:39 +00:00
} from "drizzle-orm/sqlite-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
2024-02-22 20:59:20 +00:00
import { project } from "./project";
2024-02-20 06:52:39 +00:00
export const file = sqliteTable(
"files",
{
id: integer("id").primaryKey({ autoIncrement: true }),
2024-02-22 20:59:20 +00:00
projectId: integer("project_id")
2024-02-20 06:52:39 +00:00
.notNull()
2024-02-22 20:59:20 +00:00
.references(() => project.id),
parentId: integer("parent_id"),
path: text("path").notNull(),
2024-02-21 02:01:35 +07:00
filename: text("filename").notNull(),
2024-02-20 06:52:39 +00:00
isDirectory: integer("is_directory", { mode: "boolean" })
.notNull()
.default(false),
isFile: integer("is_file", { mode: "boolean" }).notNull().default(false),
2024-02-21 02:01:35 +07:00
isPinned: integer("is_pinned", { mode: "boolean" })
.notNull()
.default(false),
2024-02-20 06:52:39 +00:00
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",
}),
2024-02-22 20:59:20 +00:00
pathIdx: index("file_path_idx").on(table.path),
filenameIdx: index("file_name_idx").on(table.filename),
2024-02-20 06:52:39 +00:00
})
);
2024-02-22 20:59:20 +00:00
export const fileRelations = relations(file, ({ one }) => ({
project: one(project, {
fields: [file.projectId],
references: [project.id],
}),
}));
2024-02-20 06:52:39 +00:00
export const insertFileSchema = createInsertSchema(file);
export const selectFileSchema = createSelectSchema(file);
export type FileSchema = z.infer<typeof selectFileSchema>;