From 7e8692238698d73f7749541fa6e38c4ca8231334 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Tue, 19 Nov 2024 10:05:53 +0100 Subject: [PATCH] Handle method passes again null instead of empty object when doucument doesn't exists This fixes regression introduced by #99 --- src/package-lock.json | 6 +++--- src/package.json | 2 +- src/packages/pongo/package.json | 2 +- src/packages/pongo/src/commandLine/shell.ts | 2 +- .../src/core/collection/pongoCollection.ts | 10 ++++++---- .../pongo/src/e2e/postgres.e2e.spec.ts | 19 +++++++++++++++++++ 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/package-lock.json b/src/package-lock.json index 10d6a73..4e7b6a3 100644 --- a/src/package-lock.json +++ b/src/package-lock.json @@ -1,12 +1,12 @@ { "name": "@event-driven-io/pongo-core", - "version": "0.16.2", + "version": "0.16.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@event-driven-io/pongo-core", - "version": "0.16.2", + "version": "0.16.3", "workspaces": [ "packages/dumbo", "packages/pongo" @@ -8674,7 +8674,7 @@ }, "packages/pongo": { "name": "@event-driven-io/pongo", - "version": "0.16.2", + "version": "0.16.3", "bin": { "pongo": "dist/cli.js" }, diff --git a/src/package.json b/src/package.json index d1679c0..71a556f 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "@event-driven-io/pongo-core", - "version": "0.16.2", + "version": "0.16.3", "description": "Pongo - Mongo with strong consistency on top of Postgres", "type": "module", "engines": { diff --git a/src/packages/pongo/package.json b/src/packages/pongo/package.json index cb5299b..7101334 100644 --- a/src/packages/pongo/package.json +++ b/src/packages/pongo/package.json @@ -1,6 +1,6 @@ { "name": "@event-driven-io/pongo", - "version": "0.16.2", + "version": "0.16.3", "description": "Pongo - Mongo with strong consistency on top of Postgres", "type": "module", "scripts": { diff --git a/src/packages/pongo/src/commandLine/shell.ts b/src/packages/pongo/src/commandLine/shell.ts index d08c479..adcdc73 100644 --- a/src/packages/pongo/src/commandLine/shell.ts +++ b/src/packages/pongo/src/commandLine/shell.ts @@ -124,7 +124,7 @@ const startRepl = async (options: { setLogLevel(process.env.DUMBO_LOG_LEVEL ?? options.logging.logLevel); setLogStyle(process.env.DUMBO_LOG_STYLE ?? options.logging.logStyle); - console.log(chalk.green('Starting Pongo Shell (version: 0.16.2)')); + console.log(chalk.green('Starting Pongo Shell (version: 0.16.3)')); if (options.logging.printOptions) { console.log(chalk.green('With Options:')); diff --git a/src/packages/pongo/src/core/collection/pongoCollection.ts b/src/packages/pongo/src/core/collection/pongoCollection.ts index 82dee54..89a453e 100644 --- a/src/packages/pongo/src/core/collection/pongoCollection.ts +++ b/src/packages/pongo/src/core/collection/pongoCollection.ts @@ -348,7 +348,7 @@ export const pongoCollection = < const existing = (await collection.findOne( byId, options, - )) as WithVersion; + )) as WithVersion | null; const expectedVersion = expectedVersionValue(version); @@ -369,13 +369,15 @@ export const pongoCollection = < ); } - const result = await handle({ ...existing } as T); + const result = await handle( + existing !== null ? ({ ...existing } as T) : null, + ); - if (deepEquals(existing as T, result)) + if (deepEquals(existing as T | null, result)) return operationResult>( { successful: true, - document: existing as T, + document: existing as T | null, }, { operationName: 'handle', collectionName, errors }, ); diff --git a/src/packages/pongo/src/e2e/postgres.e2e.spec.ts b/src/packages/pongo/src/e2e/postgres.e2e.spec.ts index bdff085..05c7d93 100644 --- a/src/packages/pongo/src/e2e/postgres.e2e.spec.ts +++ b/src/packages/pongo/src/e2e/postgres.e2e.spec.ts @@ -1061,6 +1061,25 @@ void describe('MongoDB Compatibility Tests', () => { }); void describe('Handle Operations', () => { + void it(`should pass null to handle if document doesn't exist`, async () => { + const pongoCollection = pongoDb.collection('handleCollection'); + const nonExistingId = uuid() as unknown as ObjectId; + + const newDoc: User = { name: 'John', age: 25 }; + + let wasHandled = false; + + const handle = (existing: User | null) => { + wasHandled = true; + assert.equal(existing, null); + return newDoc; + }; + + await pongoCollection.handle(nonExistingId, handle); + + assert.ok(wasHandled); + }); + void it('should insert a new document if it does not exist', async () => { const pongoCollection = pongoDb.collection('handleCollection'); const nonExistingId = uuid() as unknown as ObjectId;