Files
illusory-mapp/packages/db/schema/file.schema.ts
2026-03-01 06:17:43 +02:00

88 lines
2.8 KiB
TypeScript

import {
boolean,
integer,
json,
pgTable,
serial,
text,
timestamp,
varchar,
} from "drizzle-orm/pg-core";
import { user } from "./better.auth.schema";
import { relations } from "drizzle-orm";
export const file = pgTable("file", {
id: text("id").primaryKey(), // UUID
// File Information
filename: text("filename").notNull(),
originalName: text("original_name").notNull(),
mimeType: text("mime_type").notNull(),
size: integer("size").notNull(), // in bytes
hash: text("hash").notNull(), // SHA-256 for deduplication
// R2 Storage Information
bucketName: text("bucket_name").notNull(),
objectKey: text("object_key").notNull(),
r2Url: text("r2_url").notNull(),
// Metadata
metadata: json("metadata").$type<Record<string, any>>(),
tags: json("tags").$type<string[]>(),
// Access Control
visibility: varchar("visibility", { length: 16 })
.default("private")
.notNull(), // "public", "private", "restricted"
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
// File Status
status: varchar("status", { length: 16 }).default("processing").notNull(), // "processing", "ready", "error", "deleted"
processingError: text("processing_error"),
// Timestamps
uploadedAt: timestamp("uploaded_at").notNull(),
lastAccessedAt: timestamp("last_accessed_at"),
expiresAt: timestamp("expires_at"), // For temporary files
createdAt: timestamp("created_at").notNull(),
updatedAt: timestamp("updated_at").notNull(),
});
export const fileAccess = pgTable("file_access", {
id: serial("id").primaryKey(),
fileId: text("file_id")
.notNull()
.references(() => file.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
// Access permissions
canRead: boolean("can_read").default(false).notNull(),
canWrite: boolean("can_write").default(false).notNull(),
canDelete: boolean("can_delete").default(false).notNull(),
canShare: boolean("can_share").default(false).notNull(),
// Access tracking
accessedAt: timestamp("accessed_at"),
grantedAt: timestamp("granted_at").notNull(),
expiresAt: timestamp("expires_at"),
createdAt: timestamp("created_at").notNull(),
updatedAt: timestamp("updated_at").notNull(),
});
// Relations
export const filesRelations = relations(file, ({ one, many }) => ({
owner: one(user, { fields: [file.userId], references: [user.id] }),
fileAccess: many(fileAccess),
}));
export const fileAccessRelations = relations(fileAccess, ({ one }) => ({
file: one(file, { fields: [fileAccess.fileId], references: [file.id] }),
user: one(user, { fields: [fileAccess.userId], references: [user.id] }),
}));