add dedupe action

This commit is contained in:
Morten Olsen
2025-09-09 18:37:38 +02:00
parent 6104a0c599
commit 6b8bae55da
3 changed files with 17 additions and 1 deletions

View File

@@ -28,6 +28,7 @@
"version": "1.0.0",
"dependencies": {
"eventemitter3": "^5.0.1",
"fast-deep-equal": "^3.1.3",
"knex": "^3.1.0",
"knex-pglite": "^0.12.0",
"pg": "^8.16.3",

View File

@@ -1,8 +1,15 @@
import { EventEmitter } from 'eventemitter3';
import deepEqual from 'fast-deep-equal';
import { DatabaseService, tableNames, type TableRow } from '../database/database.ts';
import type { Document, DocumentSearchOptions, DocumentUpsert, DocumentUpsertEvent } from './documents.schemas.ts';
import {
documentUpsertEventSchema,
type Document,
type DocumentSearchOptions,
type DocumentUpsert,
type DocumentUpsertEvent,
} from './documents.schemas.ts';
import { buildMetaCondition } from './documents.query.ts';
import type { Services } from '#root/utils/services.ts';
@@ -34,6 +41,11 @@ class DocumentsService extends EventEmitter<DocumentEvents> {
updatedAt: new Date().toISOString(),
deletedAt: null,
};
const currentUpsert = documentUpsertEventSchema.parse(current);
const toInsertUpsert = documentUpsertEventSchema.parse(toInsert);
if (deepEqual(currentUpsert, toInsertUpsert)) {
return;
}
await db(tableNames.documents).update(toInsert).where({
uri: document.uri,
type: document.type,