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

Added deep equals to object comparison in handle method to support correctly mutable entities #99

Merged
merged 2 commits into from
Oct 27, 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.1",
"version": "0.16.2",
"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.1",
"version": "0.16.2",
"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.1)'));
console.log(chalk.green('Starting Pongo Shell (version: 0.16.2)'));

if (options.logging.printOptions) {
console.log(chalk.green('With Options:'));
Expand Down
5 changes: 3 additions & 2 deletions src/packages/pongo/src/core/collection/pongoCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@event-driven-io/dumbo';
import { v7 as uuid } from 'uuid';
import {
deepEquals,
expectedVersionValue,
operationResult,
type CollectionOperationOptions,
Expand Down Expand Up @@ -368,9 +369,9 @@ export const pongoCollection = <
);
}

const result = await handle(existing as T);
const result = await handle({ ...existing } as T);

if (existing === result)
if (deepEquals(existing as T, result))
return operationResult<PongoHandleResult<T>>(
{
successful: true,
Expand Down
1 change: 1 addition & 0 deletions src/packages/pongo/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './pongoSession';
export * from './pongoTransaction';
export * from './schema';
export * from './typing';
export * from './utils';
56 changes: 56 additions & 0 deletions src/packages/pongo/src/core/utils/deepEquals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export const deepEquals = <T>(left: T, right: T): boolean => {
if (isEquatable(left)) {
return left.equals(right);
}

if (Array.isArray(left)) {
return (
Array.isArray(right) &&
left.length === right.length &&
left.every((val, index) => deepEquals(val, right[index]))
);
}

if (
typeof left !== 'object' ||
typeof right !== 'object' ||
left === null ||
right === null
) {
return left === right;
}

if (Array.isArray(right)) return false;

const keys1 = Object.keys(left);
const keys2 = Object.keys(right);

if (
keys1.length !== keys2.length ||
!keys1.every((key) => keys2.includes(key))
)
return false;

for (const key in left) {
if (left[key] instanceof Function && right[key] instanceof Function)
continue;

const isEqual = deepEquals(left[key], right[key]);
if (!isEqual) {
return false;
}
}

return true;
};

export type Equatable<T> = { equals: (right: T) => boolean } & T;

export const isEquatable = <T>(left: T): left is Equatable<T> => {
return (
left &&
typeof left === 'object' &&
'equals' in left &&
typeof left['equals'] === 'function'
);
};
1 change: 1 addition & 0 deletions src/packages/pongo/src/core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './deepEquals';
37 changes: 37 additions & 0 deletions src/packages/pongo/src/e2e/postgres.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,43 @@ void describe('MongoDB Compatibility Tests', () => {
});
});

void it('should make the change if the handler returns the existing document changed', async () => {
const pongoCollection = pongoDb.collection<User>('handleCollection');

const existingDoc: User = { name: 'John', age: 25 };

const pongoInsertResult = await pongoCollection.insertOne(existingDoc);

const handle = (existing: User | null) => {
if (existing) existing.name = 'New';
return existing;
};

const resultPongo = await pongoCollection.handle(
pongoInsertResult.insertedId!,
handle,
);

assert(resultPongo.successful);
assert.deepStrictEqual(resultPongo.document, {
...existingDoc,
_id: pongoInsertResult.insertedId,
name: 'New',
_version: 2n,
});

const pongoDoc = await pongoCollection.findOne({
_id: pongoInsertResult.insertedId!,
});

assert.deepStrictEqual(pongoDoc, {
...existingDoc,
_id: pongoInsertResult.insertedId,
name: 'New',
_version: 2n,
});
});

void describe('No filter', () => {
void it('should filter and count without filter specified', async () => {
const pongoCollection = pongoDb.collection<User>('nofilter');
Expand Down