Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(document-search): batch document search operations (#1065)

Corentin Thomassete821cc3d5ef9
diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.document-search-provider.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.document-search-provider.ts
index 51af73f8..d2457c92 100644
--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.document-search-provider.ts
+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.document-search-provider.ts
@@ -18,16 +18,16 @@ export function createDatabaseFts5DocumentSearchServices({ db }: { db: Database
};
},
- indexDocument: async ({ document }) => {
- await documentsSearchRepository.indexDocument({ document });
+ indexDocuments: async ({ documents }) => {
+ await documentsSearchRepository.indexDocuments({ documents });
},
- updateDocument: async ({ document, documentId }) => {
- await documentsSearchRepository.updateDocument({ documentId, document });
+ updateDocuments: async ({ updates }) => {
+ await documentsSearchRepository.updateDocuments({ updates });
},
- deleteDocument: async ({ documentId }) => {
- await documentsSearchRepository.deleteDocument({ documentId });
+ deleteDocuments: async ({ documentIds }) => {
+ await documentsSearchRepository.deleteDocuments({ documentIds });
},
};
diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts
index 13a614d9..39b70c65 100644
--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts
+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts
@@ -19,9 +19,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: searchResults } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -48,9 +46,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: searchResults1 } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -94,9 +90,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documentsCount, documents: searchDocuments } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -149,9 +143,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documentsCount, documents: searchDocuments } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -187,9 +179,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: firstPageDocuments, documentsCount: firstTotalCount } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -241,9 +231,7 @@ describe('database-fts5 repository', () => {
});
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: firstPageDocuments, documentsCount } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -284,9 +272,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const searches = [
{
@@ -388,9 +374,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
// Index all documents from both organizations
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
// Search in org_1 with a term that exists in both organizations
const { documents: org1Results } = await documentsSearchRepository.searchOrganizationDocuments({
@@ -442,9 +426,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: searchResults } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -458,7 +440,7 @@ describe('database-fts5 repository', () => {
});
});
- describe('indexDocument', () => {
+ describe('indexDocuments', () => {
test('adds a document to the FTS index and makes it searchable', async () => {
const { db } = await createInMemoryDatabase({
organizations: [{ id: 'org_1', name: 'Organization 1' }],
@@ -479,15 +461,14 @@ describe('database-fts5 repository', () => {
}).execute();
// Manually index the document to FTS
- await documentsSearchRepository.indexDocument({
- document: {
+ await documentsSearchRepository.indexDocuments({
+ documents: [{
id: 'doc_1',
name: 'New Document',
isDeleted: false,
organizationId: 'org_1',
- originalName: 'new-doc.pdf',
content: 'searchable content here',
- },
+ }],
});
// Verify document is searchable
@@ -501,9 +482,54 @@ describe('database-fts5 repository', () => {
expect(documents).to.have.length(1);
expect(documents[0]?.id).to.equal('doc_1');
});
+
+ test('indexes multiple documents in a single call', async () => {
+ const documents = [
+ { id: 'doc_1', organizationId: 'org_1', name: 'Alpha', originalName: 'alpha.pdf', content: 'alpha content', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash1', isDeleted: false },
+ { id: 'doc_2', organizationId: 'org_1', name: 'Bravo', originalName: 'bravo.pdf', content: 'bravo content', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash2', isDeleted: false },
+ { id: 'doc_3', organizationId: 'org_1', name: 'Charlie', originalName: 'charlie.pdf', content: 'charlie content', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash3', isDeleted: false },
+ ];
+
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'org_1', name: 'Organization 1' }],
+ documents,
+ });
+
+ const documentsSearchRepository = createDocumentSearchRepository({ db });
+
+ await documentsSearchRepository.indexDocuments({ documents });
+
+ const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
+ organizationId: 'org_1',
+ searchQuery: 'content',
+ pageIndex: 0,
+ pageSize: 10,
+ });
+
+ expect(results.map(d => d.id).toSorted()).to.eql(['doc_1', 'doc_2', 'doc_3']);
+ });
+
+ test('is a no-op when called with an empty list', async () => {
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'org_1', name: 'Organization 1' }],
+ });
+
+ const documentsSearchRepository = createDocumentSearchRepository({ db });
+
+ await documentsSearchRepository.indexDocuments({ documents: [] });
+
+ const { documentsCount } = await documentsSearchRepository.searchOrganizationDocuments({
+ organizationId: 'org_1',
+ searchQuery: '',
+ pageIndex: 0,
+ pageSize: 10,
+ });
+
+ expect(documentsCount).to.equal(0);
+ });
});
- describe('updateDocument', () => {
+ describe('updateDocuments', () => {
test('updates document fields in the FTS index to reflect changes in search results', async () => {
const { db } = await createInMemoryDatabase({
organizations: [{ id: 'org_1', name: 'Organization 1' }],
@@ -523,25 +549,25 @@ describe('database-fts5 repository', () => {
originalSha256Hash: 'hash1',
}).execute();
- await documentsSearchRepository.indexDocument({
- document: {
+ await documentsSearchRepository.indexDocuments({
+ documents: [{
id: 'doc_1',
name: 'Original Name',
- originalName: 'original.pdf',
content: 'original content',
isDeleted: false,
organizationId: 'org_1',
- },
+ }],
});
// Update the FTS index with new content
- await documentsSearchRepository.updateDocument({
- documentId: 'doc_1',
- document: {
- name: 'Updated Name',
- originalName: 'updated.pdf',
- content: 'updated content with new keywords',
- },
+ await documentsSearchRepository.updateDocuments({
+ updates: [{
+ documentId: 'doc_1',
+ document: {
+ name: 'Updated Name',
+ content: 'updated content with new keywords',
+ },
+ }],
});
const resultsWithOldKeyword = await documentsSearchRepository.searchOrganizationDocuments({
@@ -562,9 +588,59 @@ describe('database-fts5 repository', () => {
expect(resultsWithNewKeyword.documents).to.have.length(1);
expect(resultsWithNewKeyword.documents[0]?.id).to.equal('doc_1');
});
+
+ test('applies heterogeneous per-row patches in a single call', async () => {
+ const documents = [
+ { id: 'doc_1', organizationId: 'org_1', name: 'Alpha', originalName: 'alpha.pdf', content: 'alpha original', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash1', isDeleted: false },
+ { id: 'doc_2', organizationId: 'org_1', name: 'Bravo', originalName: 'bravo.pdf', content: 'bravo original', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash2', isDeleted: false },
+ ];
+
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'org_1', name: 'Organization 1' }],
+ documents,
+ });
+
+ const documentsSearchRepository = createDocumentSearchRepository({ db });
+
+ await documentsSearchRepository.indexDocuments({ documents });
+
+ await documentsSearchRepository.updateDocuments({
+ updates: [
+ { documentId: 'doc_1', document: { name: 'Alpha Renamed' } },
+ { documentId: 'doc_2', document: { content: 'bravo rewritten' } },
+ ],
+ });
+
+ const { documents: renameMatches } = await documentsSearchRepository.searchOrganizationDocuments({
+ organizationId: 'org_1',
+ searchQuery: 'Renamed',
+ pageIndex: 0,
+ pageSize: 10,
+ });
+
+ const { documents: rewriteMatches } = await documentsSearchRepository.searchOrganizationDocuments({
+ organizationId: 'org_1',
+ searchQuery: 'rewritten',
+ pageIndex: 0,
+ pageSize: 10,
+ });
+
+ expect(renameMatches.map(d => d.id)).to.eql(['doc_1']);
+ expect(rewriteMatches.map(d => d.id)).to.eql(['doc_2']);
+ });
+
+ test('is a no-op when called with an empty list', async () => {
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'org_1', name: 'Organization 1' }],
+ });
+
+ const documentsSearchRepository = createDocumentSearchRepository({ db });
+
+ await documentsSearchRepository.updateDocuments({ updates: [] });
+ });
});
- describe('deleteDocument', () => {
+ describe('deleteDocuments', () => {
test('removes a document from the FTS index and makes it unsearchable', async () => {
const { db } = await createInMemoryDatabase({
organizations: [{ id: 'org_1', name: 'Organization 1' }],
@@ -596,26 +672,23 @@ describe('database-fts5 repository', () => {
},
]).execute();
- await documentsSearchRepository.indexDocument({
- document: {
- id: 'doc_1',
- name: 'Document to Delete',
- originalName: 'delete-me.pdf',
- content: 'this will be deleted',
- isDeleted: false,
- organizationId: 'org_1',
- },
- });
-
- await documentsSearchRepository.indexDocument({
- document: {
- id: 'doc_2',
- name: 'Document to Keep',
- originalName: 'keep-me.pdf',
- content: 'this will stay',
- isDeleted: false,
- organizationId: 'org_1',
- },
+ await documentsSearchRepository.indexDocuments({
+ documents: [
+ {
+ id: 'doc_1',
+ name: 'Document to Delete',
+ content: 'this will be deleted',
+ isDeleted: false,
+ organizationId: 'org_1',
+ },
+ {
+ id: 'doc_2',
+ name: 'Document to Keep',
+ content: 'this will stay',
+ isDeleted: false,
+ organizationId: 'org_1',
+ },
+ ],
});
const beforeDelete = await documentsSearchRepository.searchOrganizationDocuments({
@@ -627,7 +700,7 @@ describe('database-fts5 repository', () => {
expect(beforeDelete.documents).to.have.length(1);
- await documentsSearchRepository.deleteDocument({ documentId: 'doc_1' });
+ await documentsSearchRepository.deleteDocuments({ documentIds: ['doc_1'] });
const afterDelete = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -638,6 +711,44 @@ describe('database-fts5 repository', () => {
expect(afterDelete.documents).to.have.length(0);
});
+
+ test('removes multiple documents from the FTS index in a single call', async () => {
+ const documents = [
+ { id: 'doc_1', organizationId: 'org_1', name: 'Alpha', originalName: 'alpha.pdf', content: 'shared keyword', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash1', isDeleted: false },
+ { id: 'doc_2', organizationId: 'org_1', name: 'Bravo', originalName: 'bravo.pdf', content: 'shared keyword', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash2', isDeleted: false },
+ { id: 'doc_3', organizationId: 'org_1', name: 'Charlie', originalName: 'charlie.pdf', content: 'shared keyword', originalStorageKey: '', mimeType: 'application/pdf', originalSha256Hash: 'hash3', isDeleted: false },
+ ];
+
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'org_1', name: 'Organization 1' }],
+ documents,
+ });
+
+ const documentsSearchRepository = createDocumentSearchRepository({ db });
+
+ await documentsSearchRepository.indexDocuments({ documents });
+
+ await documentsSearchRepository.deleteDocuments({ documentIds: ['doc_1', 'doc_3'] });
+
+ const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
+ organizationId: 'org_1',
+ searchQuery: 'shared',
+ pageIndex: 0,
+ pageSize: 10,
+ });
+
+ expect(results.map(d => d.id)).to.eql(['doc_2']);
+ });
+
+ test('is a no-op when called with an empty list', async () => {
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'org_1', name: 'Organization 1' }],
+ });
+
+ const documentsSearchRepository = createDocumentSearchRepository({ db });
+
+ await documentsSearchRepository.deleteDocuments({ documentIds: [] });
+ });
});
describe('custom property filters', () => {
@@ -664,9 +775,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
return { documentsSearchRepository };
};
@@ -746,9 +855,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -784,9 +891,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
return { documentsSearchRepository };
};
@@ -854,9 +959,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -897,9 +1000,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -940,9 +1041,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -985,9 +1084,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -1011,8 +1108,8 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await documentsSearchRepository.indexDocument({
- document: { id: 'doc_1', organizationId: 'org_1', name: 'File', originalName: 'f.pdf', content: '', isDeleted: false },
+ await documentsSearchRepository.indexDocuments({
+ documents: [{ id: 'doc_1', organizationId: 'org_1', name: 'File', content: '', isDeleted: false }],
});
const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
@@ -1050,11 +1147,11 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await documentsSearchRepository.indexDocument({
- document: { id: 'doc_1', organizationId: 'org_1', name: 'Org1 File', originalName: 'a.pdf', content: '', isDeleted: false },
- });
- await documentsSearchRepository.indexDocument({
- document: { id: 'doc_2', organizationId: 'org_2', name: 'Org2 File', originalName: 'b.pdf', content: '', isDeleted: false },
+ await documentsSearchRepository.indexDocuments({
+ documents: [
+ { id: 'doc_1', organizationId: 'org_1', name: 'Org1 File', content: '', isDeleted: false },
+ { id: 'doc_2', organizationId: 'org_2', name: 'Org2 File', content: '', isDeleted: false },
+ ],
});
const { documents: org1Results } = await documentsSearchRepository.searchOrganizationDocuments({
@@ -1095,9 +1192,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
return { documentsSearchRepository };
};
@@ -1164,9 +1259,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
const { documents: results } = await documentsSearchRepository.searchOrganizationDocuments({
organizationId: 'org_1',
@@ -1202,9 +1295,7 @@ describe('database-fts5 repository', () => {
const documentsSearchRepository = createDocumentSearchRepository({ db });
- await Promise.all(
- documents.map(async document => documentsSearchRepository.indexDocument({ document })),
- );
+ await documentsSearchRepository.indexDocuments({ documents });
return { documentsSearchRepository };
};
diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.ts
index cf371be5..0c6ecd52 100644
--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.ts
+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.ts
@@ -1,8 +1,9 @@
import type { Database } from '../../../app/database/database.types';
-import type { DocumentSearchableData } from '../document-search.types';
+import type { DocumentSearchableData, DocumentUpdate } from '../document-search.types';
import { injectArguments } from '@corentinth/chisels';
-import { desc, eq, getTableColumns, sql } from 'drizzle-orm';
+import { desc, eq, getTableColumns, inArray, sql } from 'drizzle-orm';
import { customPropertyDefinitionsTable } from '../../../custom-properties/custom-properties.table';
+import { isNonEmptyArray } from '../../../shared/arrays/arrays.utils';
import { omit, omitUndefined } from '../../../shared/objects';
import { documentsTable } from '../../documents.table';
import { makeSearchWhereClause } from './database-fts5.repository.models';
@@ -13,9 +14,9 @@ export type DocumentSearchRepository = ReturnType<typeof createDocumentSearchRep
export function createDocumentSearchRepository({ db }: { db: Database }) {
return injectArguments({
searchOrganizationDocuments,
- indexDocument,
- updateDocument,
- deleteDocument,
+ indexDocuments,
+ updateDocuments,
+ deleteDocuments,
}, { db });
}
@@ -56,36 +57,57 @@ async function searchOrganizationDocuments({ organizationId, searchQuery, pageIn
return { documents, documentsCount };
}
-async function indexDocument({ document, db }: { document: DocumentSearchableData; db: Database }) {
+async function indexDocuments({ documents, db }: { documents: DocumentSearchableData[]; db: Database }) {
+ if (documents.length === 0) {
+ return;
+ }
+
await db
.insert(documentsFtsTable)
- .values({
+ .values(documents.map(document => ({
documentId: document.id,
organizationId: document.organizationId,
name: document.name,
content: document.content,
- });
+ })));
}
-async function updateDocument({ documentId, document, db }: { documentId: string; document: { content?: string; name?: string; originalName?: string }; db: Database }) {
- const dataToUpdate = omitUndefined({
- name: document.name,
- originalName: document.originalName,
- content: document.content,
- });
+async function updateDocuments({ updates, db }: { updates: DocumentUpdate[]; db: Database }) {
+ if (updates.length === 0) {
+ return;
+ }
+
+ const queries = updates
+ .map(({ documentId, document }) => {
+ const dataToUpdate = omitUndefined({
+ name: document.name,
+ content: document.content,
+ });
- if (Object.keys(dataToUpdate).length === 0) {
+ if (Object.keys(dataToUpdate).length === 0) {
+ return null;
+ }
+
+ return db
+ .update(documentsFtsTable)
+ .set(dataToUpdate)
+ .where(eq(documentsFtsTable.documentId, documentId));
+ })
+ .filter(query => query !== null);
+
+ if (!isNonEmptyArray(queries)) {
return;
}
- await db
- .update(documentsFtsTable)
- .set(dataToUpdate)
- .where(eq(documentsFtsTable.documentId, documentId));
+ await db.batch(queries);
}
-async function deleteDocument({ documentId, db }: { documentId: string; db: Database }) {
+async function deleteDocuments({ documentIds, db }: { documentIds: string[]; db: Database }) {
+ if (documentIds.length === 0) {
+ return;
+ }
+
await db
.delete(documentsFtsTable)
- .where(eq(documentsFtsTable.documentId, documentId));
+ .where(inArray(documentsFtsTable.documentId, documentIds));
}
diff --git a/apps/papra-server/src/modules/documents/document-search/document-search.types.ts b/apps/papra-server/src/modules/documents/document-search/document-search.types.ts
index 8f8a74b2..cf801e77 100644
--- a/apps/papra-server/src/modules/documents/document-search/document-search.types.ts
+++ b/apps/papra-server/src/modules/documents/document-search/document-search.types.ts
@@ -3,7 +3,6 @@ import type { Document } from '../documents.types';
export type DocumentSearchableData = {
id: string;
name: string;
- originalName: string;
content: string;
isDeleted: boolean;
organizationId: string;
@@ -14,6 +13,11 @@ export type DocumentSearchResult = {
documentsCount: number;
};
+export type DocumentUpdate = {
+ documentId: string;
+ document: Partial<Omit<DocumentSearchableData, 'id'>>;
+};
+
export type DocumentSearchServices = {
name: string;
searchDocuments: (args: {
@@ -23,7 +27,7 @@ export type DocumentSearchServices = {
pageSize: number;
}) => Promise<DocumentSearchResult>;
- indexDocument: (args: { document: DocumentSearchableData }) => Promise<void>;
- updateDocument: (args: { documentId: string; document: Partial<Omit<DocumentSearchableData, 'id'>> }) => Promise<void>;
- deleteDocument: (args: { documentId: string }) => Promise<void>;
+ indexDocuments: (args: { documents: DocumentSearchableData[] }) => Promise<void>;
+ updateDocuments: (args: { updates: DocumentUpdate[] }) => Promise<void>;
+ deleteDocuments: (args: { documentIds: string[] }) => Promise<void>;
};
diff --git a/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.test.ts b/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.test.ts
index ed451e6d..86b36ebf 100644
--- a/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.test.ts
+++ b/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.test.ts
@@ -9,9 +9,9 @@ import { registerSyncDocumentSearchEventHandlers } from './sync-document-search.
function createTestSearchServices() {
const methodsArgs = {
searchDocuments: [] as Parameters<DocumentSearchServices['searchDocuments']>[0][],
- indexDocument: [] as Parameters<DocumentSearchServices['indexDocument']>[0][],
- updateDocument: [] as Parameters<DocumentSearchServices['updateDocument']>[0][],
- deleteDocument: [] as Parameters<DocumentSearchServices['deleteDocument']>[0][],
+ indexDocuments: [] as Parameters<DocumentSearchServices['indexDocuments']>[0][],
+ updateDocuments: [] as Parameters<DocumentSearchServices['updateDocuments']>[0][],
+ deleteDocuments: [] as Parameters<DocumentSearchServices['deleteDocuments']>[0][],
};
const searchServices: DocumentSearchServices = {
@@ -20,14 +20,14 @@ function createTestSearchServices() {
methodsArgs.searchDocuments.push(args);
return { documents: [], documentsCount: 0 };
},
- indexDocument: async (args) => {
- methodsArgs.indexDocument.push(args);
+ indexDocuments: async (args) => {
+ methodsArgs.indexDocuments.push(args);
},
- updateDocument: async (args) => {
- methodsArgs.updateDocument.push(args);
+ updateDocuments: async (args) => {
+ methodsArgs.updateDocuments.push(args);
},
- deleteDocument: async (args) => {
- methodsArgs.deleteDocument.push(args);
+ deleteDocuments: async (args) => {
+ methodsArgs.deleteDocuments.push(args);
},
};
@@ -77,9 +77,9 @@ describe('sync-document-search event handlers', () => {
expect(documentSearchServices.getMethodsArguments()).to.eql({
searchDocuments: [],
- indexDocument: [{ document }],
- updateDocument: [],
- deleteDocument: [],
+ indexDocuments: [{ documents: [document] }],
+ updateDocuments: [],
+ deleteDocuments: [],
});
});
@@ -126,12 +126,14 @@ describe('sync-document-search event handlers', () => {
expect(documentSearchServices.getMethodsArguments()).to.eql({
searchDocuments: [],
- indexDocument: [],
- updateDocument: [{
- documentId: 'doc-1',
- document: changes,
+ indexDocuments: [],
+ updateDocuments: [{
+ updates: [{
+ documentId: 'doc-1',
+ document: changes,
+ }],
}],
- deleteDocument: [],
+ deleteDocuments: [],
});
});
@@ -155,12 +157,14 @@ describe('sync-document-search event handlers', () => {
expect(documentSearchServices.getMethodsArguments()).to.eql({
searchDocuments: [],
- indexDocument: [],
- updateDocument: [{
- documentId: 'doc-1',
- document: { isDeleted: true },
+ indexDocuments: [],
+ updateDocuments: [{
+ updates: [{
+ documentId: 'doc-1',
+ document: { isDeleted: true },
+ }],
}],
- deleteDocument: [],
+ deleteDocuments: [],
});
});
@@ -184,12 +188,14 @@ describe('sync-document-search event handlers', () => {
expect(documentSearchServices.getMethodsArguments()).to.eql({
searchDocuments: [],
- indexDocument: [],
- updateDocument: [{
- documentId: 'doc-1',
- document: { isDeleted: false },
+ indexDocuments: [],
+ updateDocuments: [{
+ updates: [{
+ documentId: 'doc-1',
+ document: { isDeleted: false },
+ }],
}],
- deleteDocument: [],
+ deleteDocuments: [],
});
});
@@ -212,9 +218,9 @@ describe('sync-document-search event handlers', () => {
expect(documentSearchServices.getMethodsArguments()).to.eql({
searchDocuments: [],
- indexDocument: [],
- updateDocument: [],
- deleteDocument: [{ documentId: 'doc-1' }],
+ indexDocuments: [],
+ updateDocuments: [],
+ deleteDocuments: [{ documentIds: ['doc-1'] }],
});
});
@@ -292,22 +298,28 @@ describe('sync-document-search event handlers', () => {
expect(documentSearchServices.getMethodsArguments()).to.eql({
searchDocuments: [],
- indexDocument: [{ document }],
- updateDocument: [
+ indexDocuments: [{ documents: [document] }],
+ updateDocuments: [
{
- documentId: 'doc-1',
- document: { name: 'Updated Name' },
+ updates: [{
+ documentId: 'doc-1',
+ document: { name: 'Updated Name' },
+ }],
},
{
- documentId: 'doc-1',
- document: { isDeleted: true },
+ updates: [{
+ documentId: 'doc-1',
+ document: { isDeleted: true },
+ }],
},
{
- documentId: 'doc-1',
- document: { isDeleted: false },
+ updates: [{
+ documentId: 'doc-1',
+ document: { isDeleted: false },
+ }],
},
],
- deleteDocument: [{ documentId: 'doc-1' }],
+ deleteDocuments: [{ documentIds: ['doc-1'] }],
});
});
});
diff --git a/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.ts b/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.ts
index 4c1c2e64..fad6fff2 100644
--- a/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.ts
+++ b/apps/papra-server/src/modules/documents/document-search/events/sync-document-search.handlers.ts
@@ -12,7 +12,7 @@ export function registerSyncDocumentSearchEventHandlers({ eventServices, documen
eventName: 'document.created',
handlerName: 'index-document-in-search-service',
async handler({ document }) {
- await documentSearchServices.indexDocument({ document });
+ await documentSearchServices.indexDocuments({ documents: [document] });
},
});
@@ -20,7 +20,7 @@ export function registerSyncDocumentSearchEventHandlers({ eventServices, documen
eventName: 'document.updated',
handlerName: 'update-document-in-search-service',
async handler({ document, changes }) {
- await documentSearchServices.updateDocument({ document: changes, documentId: document.id });
+ await documentSearchServices.updateDocuments({ updates: [{ documentId: document.id, document: changes }] });
},
});
@@ -28,7 +28,7 @@ export function registerSyncDocumentSearchEventHandlers({ eventServices, documen
eventName: 'document.trashed',
handlerName: 'mark-document-deleted-in-search-service',
async handler({ documentId }) {
- await documentSearchServices.updateDocument({ documentId, document: { isDeleted: true } });
+ await documentSearchServices.updateDocuments({ updates: [{ documentId, document: { isDeleted: true } }] });
},
});
@@ -36,7 +36,7 @@ export function registerSyncDocumentSearchEventHandlers({ eventServices, documen
eventName: 'document.restored',
handlerName: 'restore-document-in-search-service',
async handler({ documentId }) {
- await documentSearchServices.updateDocument({ documentId, document: { isDeleted: false } });
+ await documentSearchServices.updateDocuments({ updates: [{ documentId, document: { isDeleted: false } }] });
},
});
@@ -44,7 +44,7 @@ export function registerSyncDocumentSearchEventHandlers({ eventServices, documen
eventName: 'document.deleted',
handlerName: 'remove-document-from-search-service',
async handler({ documentId }) {
- await documentSearchServices.deleteDocument({ documentId });
+ await documentSearchServices.deleteDocuments({ documentIds: [documentId] });
},
});
}
diff --git a/apps/papra-server/src/modules/shared/arrays/arrays.utils.test.ts b/apps/papra-server/src/modules/shared/arrays/arrays.utils.test.ts
new file mode 100644
index 00000000..46fe121c
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/arrays/arrays.utils.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, test } from 'vitest';
+import { ensureNonEmptyArray, isNonEmptyArray } from './arrays.utils';
+
+describe('arrays.utils', () => {
+ describe('ensureNonEmptyArray', () => {
+ describe('helper mainly used for type inference', () => {
+ test('throws an error if the array is nil or empty', () => {
+ expect(() => ensureNonEmptyArray(undefined)).toThrow('Expected a non-empty array');
+ expect(() => ensureNonEmptyArray([])).toThrow('Expected a non-empty array');
+ expect(() => ensureNonEmptyArray(null)).toThrow('Expected a non-empty array');
+
+ expect(ensureNonEmptyArray([1, 2, 3])).toEqual([1, 2, 3]);
+ expect(ensureNonEmptyArray(['a', 'b'])).toEqual(['a', 'b']);
+ });
+ });
+ });
+
+ describe('isNonEmptyArray', () => {
+ describe('helper mainly used for type inference', () => {
+ test('returns true if the array is non-empty and not nil', () => {
+ expect(isNonEmptyArray([1, 2, 3])).toBe(true);
+ expect(isNonEmptyArray(['a', 'b'])).toBe(true);
+
+ expect(isNonEmptyArray([])).toBe(false);
+ expect(isNonEmptyArray(undefined)).toBe(false);
+ expect(isNonEmptyArray(null)).toBe(false);
+ });
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/shared/arrays/arrays.utils.ts b/apps/papra-server/src/modules/shared/arrays/arrays.utils.ts
new file mode 100644
index 00000000..58527e06
--- /dev/null
+++ b/apps/papra-server/src/modules/shared/arrays/arrays.utils.ts
@@ -0,0 +1,14 @@
+import type { NonEmptyArray } from '../types';
+import { isNil } from '../utils';
+
+export function ensureNonEmptyArray<T>(array: T[] | undefined | null): NonEmptyArray<T> {
+ if (!array || array.length === 0) {
+ throw new Error('Expected a non-empty array');
+ }
+
+ return array as NonEmptyArray<T>;
+}
+
+export function isNonEmptyArray<T>(array: T[] | undefined | null): array is NonEmptyArray<T> {
+ return !isNil(array) && array.length > 0;
+}