Files
FEDEO/backend/db/schema/document_imports.ts

85 lines
3.7 KiB
TypeScript

import {
bigint,
boolean,
index,
integer,
jsonb,
pgTable,
text,
timestamp,
uniqueIndex,
uuid,
} from "drizzle-orm/pg-core"
import { tenants } from "./tenants"
import { authUsers } from "./auth_users"
import { files } from "./files"
import { folders } from "./folders"
import { filetags } from "./filetags"
export const documentImportSources = pgTable("document_import_sources", {
id: uuid("id").primaryKey().defaultRandom(),
tenantId: bigint("tenant_id", { mode: "number" })
.notNull()
.references(() => tenants.id, { onDelete: "cascade", onUpdate: "cascade" }),
createdBy: uuid("created_by").references(() => authUsers.id, { onDelete: "set null" }),
name: text("name").notNull(),
provider: text("provider").notNull().default("imap"),
enabled: boolean("enabled").notNull().default(true),
mailboxAddressEncrypted: jsonb("mailbox_address_encrypted"),
passwordEncrypted: jsonb("password_encrypted"),
imapHostEncrypted: jsonb("imap_host_encrypted"),
imapPort: integer("imap_port").notNull().default(993),
imapSecure: boolean("imap_secure").notNull().default(true),
mailboxPath: text("mailbox_path").notNull().default("INBOX"),
targetFolderId: uuid("target_folder_id").references(() => folders.id, { onDelete: "set null" }),
defaultFiletypeId: uuid("default_filetype_id").references(() => filetags.id, { onDelete: "set null" }),
markAsSeen: boolean("mark_as_seen").notNull().default(true),
lastSyncedAt: timestamp("last_synced_at", { withTimezone: true }),
lastError: text("last_error"),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true }),
}, (table) => ({
tenantIdx: index("document_import_sources_tenant_idx").on(table.tenantId),
}))
export const documentImportStates = pgTable("document_import_states", {
id: uuid("id").primaryKey().defaultRandom(),
sourceId: uuid("source_id")
.notNull()
.references(() => documentImportSources.id, { onDelete: "cascade", onUpdate: "cascade" }),
mailboxPath: text("mailbox_path").notNull(),
uidValidity: bigint("uid_validity", { mode: "number" }),
highestUid: bigint("highest_uid", { mode: "number" }).notNull().default(0),
deltaLinkEncrypted: jsonb("delta_link_encrypted"),
updatedAt: timestamp("updated_at", { withTimezone: true }),
}, (table) => ({
sourceMailboxKey: uniqueIndex("document_import_states_source_mailbox_key")
.on(table.sourceId, table.mailboxPath),
}))
export const documentImportItems = pgTable("document_import_items", {
id: uuid("id").primaryKey().defaultRandom(),
tenantId: bigint("tenant_id", { mode: "number" })
.notNull()
.references(() => tenants.id, { onDelete: "cascade", onUpdate: "cascade" }),
sourceId: uuid("source_id")
.notNull()
.references(() => documentImportSources.id, { onDelete: "cascade", onUpdate: "cascade" }),
remoteMessageId: text("remote_message_id").notNull(),
attachmentKey: text("attachment_key").notNull(),
attachmentChecksum: text("attachment_checksum").notNull(),
filename: text("filename"),
status: text("status").notNull(),
error: text("error"),
fileId: uuid("file_id").references(() => files.id, { onDelete: "set null" }),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
}, (table) => ({
remoteAttachmentKey: uniqueIndex("document_import_items_remote_attachment_key")
.on(table.sourceId, table.remoteMessageId, table.attachmentKey),
checksumIdx: index("document_import_items_checksum_idx")
.on(table.sourceId, table.attachmentChecksum),
}))
export type DocumentImportSource = typeof documentImportSources.$inferSelect