Files
SnapOtter/apps/api/src/db/schema.ts
T

141 lines
5.1 KiB
TypeScript
Raw Normal View History

2026-03-25 09:21:17 +08:00
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: text("id").primaryKey(),
username: text("username").notNull().unique(),
passwordHash: text("password_hash"),
role: text("role").notNull().default("user"),
2026-03-25 01:13:25 +08:00
team: text("team").notNull().default("Default"),
mustChangePassword: integer("must_change_password", { mode: "boolean" }).notNull().default(true),
authProvider: text("auth_provider").notNull().default("local"),
externalId: text("external_id"),
email: text("email"),
2026-03-25 09:21:17 +08:00
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
analyticsEnabled: integer("analytics_enabled", { mode: "boolean" }),
analyticsConsentShownAt: integer("analytics_consent_shown_at", { mode: "timestamp" }),
analyticsConsentRemindAt: integer("analytics_consent_remind_at", { mode: "timestamp" }),
2026-03-25 09:21:17 +08:00
});
export const teams = sqliteTable("teams", {
id: text("id").primaryKey(),
name: text("name").notNull().unique(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const sessions = sqliteTable("sessions", {
id: text("id").primaryKey(),
2026-03-25 09:21:17 +08:00
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
idToken: text("id_token"),
2026-03-25 09:21:17 +08:00
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const settings = sqliteTable("settings", {
key: text("key").primaryKey(),
value: text("value").notNull(),
2026-03-25 09:21:17 +08:00
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const jobs = sqliteTable("jobs", {
id: text("id").primaryKey(),
type: text("type").notNull(),
2026-03-25 09:21:17 +08:00
status: text("status", { enum: ["queued", "processing", "completed", "failed"] })
.notNull()
.default("queued"),
progress: real("progress").notNull().default(0),
inputFiles: text("input_files").notNull(),
outputPath: text("output_path"),
settings: text("settings"),
error: text("error"),
2026-03-25 09:21:17 +08:00
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
completedAt: integer("completed_at", { mode: "timestamp" }),
});
export const apiKeys = sqliteTable("api_keys", {
id: text("id").primaryKey(),
2026-03-25 09:21:17 +08:00
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
keyHash: text("key_hash").notNull(),
keyPrefix: text("key_prefix"),
name: text("name").notNull().default("Default API Key"),
permissions: text("permissions"),
2026-03-25 09:21:17 +08:00
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
lastUsedAt: integer("last_used_at", { mode: "timestamp" }),
expiresAt: integer("expires_at", { mode: "timestamp" }),
});
export const pipelines = sqliteTable("pipelines", {
id: text("id").primaryKey(),
userId: text("user_id").references(() => users.id, { onDelete: "cascade" }),
name: text("name").notNull(),
description: text("description"),
steps: text("steps").notNull(), // JSON array of { toolId, settings }
2026-03-25 09:21:17 +08:00
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
2026-03-25 01:13:25 +08:00
export const auditLog = sqliteTable("audit_log", {
id: text("id").primaryKey(),
actorId: text("actor_id").references(() => users.id, { onDelete: "set null" }),
actorUsername: text("actor_username").notNull(),
action: text("action").notNull(),
targetType: text("target_type"),
targetId: text("target_id"),
details: text("details"),
ipAddress: text("ip_address"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const roles = sqliteTable("roles", {
id: text("id").primaryKey(),
name: text("name").notNull().unique(),
description: text("description").notNull().default(""),
permissions: text("permissions").notNull(),
isBuiltin: integer("is_builtin", { mode: "boolean" }).notNull().default(false),
createdBy: text("created_by").references(() => users.id, { onDelete: "set null" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
2026-03-25 01:13:25 +08:00
export const userFiles = sqliteTable("user_files", {
id: text("id").primaryKey(),
userId: text("user_id").references(() => users.id, { onDelete: "cascade" }),
originalName: text("original_name").notNull(),
storedName: text("stored_name").notNull(),
mimeType: text("mime_type").notNull(),
size: integer("size").notNull(),
width: integer("width"),
height: integer("height"),
version: integer("version").notNull().default(1),
parentId: text("parent_id"),
toolChain: text("tool_chain"),
2026-03-25 09:21:17 +08:00
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
2026-03-25 01:13:25 +08:00
});