Skip to content

Commit

Permalink
Handle method passes again null instead of empty object when doucumen…
Browse files Browse the repository at this point in the history
…t doesn't exists

This fixes regression introduced by #99
  • Loading branch information
oskardudycz committed Nov 19, 2024
1 parent 202433b commit 7e86922
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/pongo/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/pongo/src/commandLine/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:'));
Expand Down
10 changes: 6 additions & 4 deletions src/packages/pongo/src/core/collection/pongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export const pongoCollection = <
const existing = (await collection.findOne(
byId,
options,
)) as WithVersion<T>;
)) as WithVersion<T> | null;

const expectedVersion = expectedVersionValue(version);

Expand All @@ -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<PongoHandleResult<T>>(
{
successful: true,
document: existing as T,
document: existing as T | null,
},
{ operationName: 'handle', collectionName, errors },
);
Expand Down
19 changes: 19 additions & 0 deletions src/packages/pongo/src/e2e/postgres.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>('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<User>('handleCollection');
const nonExistingId = uuid() as unknown as ObjectId;
Expand Down

0 comments on commit 7e86922

Please sign in to comment.