openreader/src/lib/server/documents/register-upload.ts
Richard R 25342371a4 refactor(db): remove legacy PDF parse fields from documents schema
Eliminate parseState and parsedJsonKey columns from the documents table in both
Postgres and SQLite schemas, including related migration scripts and type
removal in document registration logic. This aligns the schema with the new
worker-based PDF parse model and reduces legacy field clutter.
2026-06-04 20:17:23 -06:00

67 lines
1.7 KiB
TypeScript

import { db } from '@/db';
import { documents } from '@/db/schema';
import {
enqueueDocumentPreview,
} from '@/lib/server/documents/previews';
import { errorToLog, serverLogger } from '@/lib/server/logger';
import type { BaseDocument, DocumentType } from '@/types/documents';
type RegisterUploadedDocumentInput = {
documentId: string;
userId: string;
namespace: string | null;
name: string;
type: DocumentType;
size: number;
lastModified: number;
};
export async function registerUploadedDocument(input: RegisterUploadedDocumentInput): Promise<BaseDocument> {
await db
.insert(documents)
.values({
id: input.documentId,
userId: input.userId,
name: input.name,
type: input.type,
size: input.size,
lastModified: input.lastModified,
filePath: input.documentId,
})
.onConflictDoUpdate({
target: [documents.id, documents.userId],
set: {
name: input.name,
type: input.type,
size: input.size,
lastModified: input.lastModified,
filePath: input.documentId,
},
});
await enqueueDocumentPreview(
{
id: input.documentId,
type: input.type,
lastModified: input.lastModified,
},
input.namespace,
).catch((error) => {
serverLogger.warn({
event: 'documents.preview.enqueue.failed',
degraded: true,
fallbackPath: 'skip_preview_enqueue',
documentId: input.documentId,
error: errorToLog(error),
}, 'Failed to enqueue document preview');
});
return {
id: input.documentId,
name: input.name,
type: input.type,
size: input.size,
lastModified: input.lastModified,
scope: 'user',
};
}