Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle method passes again null instead of empty object when doucument doesn't exists #100

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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