From 5932f0d94d85f1379a1ced5d7cf8691398087aee Mon Sep 17 00:00:00 2001 From: Ioannis Ktistakis <47716028+Yianni99@users.noreply.github.com> Date: Thu, 30 Nov 2023 09:20:13 +0200 Subject: [PATCH 1/3] fix(plugin-mongodb): Handle upsert true for error throw. --- .../MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js b/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js index df36836..f7e4b94 100644 --- a/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js +++ b/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.js @@ -54,12 +54,12 @@ async function MongodbUpdateOne({ type: 'MongoDBUpdateOne', meta: connection.changeLog?.meta, }); - if (!disableNoMatchError && !response.lastErrorObject.updatedExisting) { + if (!disableNoMatchError && !options?.upsert && !response.lastErrorObject.updatedExisting) { throw new Error('No matching record to update.'); } } else { response = await collection.updateOne(filter, update, options); - if (!disableNoMatchError && response.matchedCount === 0) { + if (!disableNoMatchError && !options?.upsert && response.matchedCount === 0) { throw new Error('No matching record to update.'); } } From 84d682bd79fdd6476577fb7be3055958f144ffc4 Mon Sep 17 00:00:00 2001 From: Ioannis Ktistakis <47716028+Yianni99@users.noreply.github.com> Date: Thu, 30 Nov 2023 09:21:02 +0200 Subject: [PATCH 2/3] fix(plugin-mongodb): Update MongoDBUpdateOne tests. --- .../MongoDBUpdateOne/MongoDBUpdateOne.test.js | 160 +++++++++++++++--- 1 file changed, 133 insertions(+), 27 deletions(-) diff --git a/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.test.js b/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.test.js index 266235f..4a66d58 100644 --- a/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.test.js +++ b/plugins/community-plugin-mongodb/src/connections/MongoDBCollection/MongoDBUpdateOne/MongoDBUpdateOne.test.js @@ -179,14 +179,9 @@ test('updateOne upsert false', async () => { collection, write: true, }; - const res = await MongoDBUpdateOne({ request, connection }); - expect(res).toEqual({ - acknowledged: true, - modifiedCount: 0, - upsertedId: null, - upsertedCount: 0, - matchedCount: 0, - }); + expect(async () => { + await MongoDBUpdateOne({ request, connection }); + }).rejects.toThrow('No matching record to update.'); }); test('updateOne upsert false logCollection', async () => { @@ -202,32 +197,83 @@ test('updateOne upsert false logCollection', async () => { changeLog: { collection: logCollection, meta: { meta: true } }, write: true, }; - const res = await MongoDBUpdateOne({ - request, + await expect(async () => { + await MongoDBUpdateOne({ + request, + blockId: 'blockId', + connectionId: 'connectionId', + pageId: 'pageId', + payload: { payload: true }, + requestId: 'updateOne_upsert_false', + connection, + }); + }).rejects.toThrow('No matching record to update.'); + const logged = await findLogCollectionRecordTestMongoDb({ + logCollection, + requestId: 'updateOne_upsert_false', + }); + expect(logged).toMatchObject({ blockId: 'blockId', connectionId: 'connectionId', pageId: 'pageId', payload: { payload: true }, requestId: 'updateOne_upsert_false', - connection, - }); - expect(res).toEqual({ - lastErrorObject: { - n: 0, - updatedExisting: false, - }, - ok: 1, + before: null, + after: null, + type: 'MongoDBUpdateOne', + meta: { meta: true }, }); +}); + +test('updateOne upsert default false', async () => { + const request = { + filter: { _id: 'updateOne_upsert_default_false' }, + update: { $set: { v: 'after' } }, + }; + const connection = { + databaseUri, + databaseName, + collection, + write: true, + }; + expect(async () => { + await MongoDBUpdateOne({ request, connection }); + }).rejects.toThrow('No matching record to update.'); +}); + +test('updateOne upsert default false logCollection', async () => { + const request = { + filter: { _id: 'updateOne_upsert_default_false' }, + update: { $set: { v: 'after' } }, + }; + const connection = { + databaseUri, + databaseName, + collection, + changeLog: { collection: logCollection, meta: { meta: true } }, + write: true, + }; + await expect(async () => { + await MongoDBUpdateOne({ + request, + blockId: 'blockId', + connectionId: 'connectionId', + pageId: 'pageId', + payload: { payload: true }, + requestId: 'updateOne_upsert_default_false', + connection, + }); + }).rejects.toThrow('No matching record to update.'); const logged = await findLogCollectionRecordTestMongoDb({ logCollection, - requestId: 'updateOne_upsert_false', + requestId: 'updateOne_upsert_default_false', }); expect(logged).toMatchObject({ blockId: 'blockId', connectionId: 'connectionId', pageId: 'pageId', payload: { payload: true }, - requestId: 'updateOne_upsert_false', + requestId: 'updateOne_upsert_default_false', before: null, after: null, type: 'MongoDBUpdateOne', @@ -235,10 +281,11 @@ test('updateOne upsert false logCollection', async () => { }); }); -test('updateOne upsert default false', async () => { +test('updateOne disableNoMatchError', async () => { const request = { - filter: { _id: 'updateOne_upsert_default_false' }, + filter: { _id: 'updateOne_disable_no_match_error' }, update: { $set: { v: 'after' } }, + disableNoMatchError: true, }; const connection = { databaseUri, @@ -256,10 +303,11 @@ test('updateOne upsert default false', async () => { }); }); -test('updateOne upsert default false logCollection', async () => { +test('updateOne disableNoMatchError logCollection', async () => { const request = { - filter: { _id: 'updateOne_upsert_default_false' }, + filter: { _id: 'updateOne_disable_no_match_error' }, update: { $set: { v: 'after' } }, + disableNoMatchError: true, }; const connection = { databaseUri, @@ -274,7 +322,7 @@ test('updateOne upsert default false logCollection', async () => { connectionId: 'connectionId', pageId: 'pageId', payload: { payload: true }, - requestId: 'updateOne_upsert_default_false', + requestId: 'updateOne_disable_no_match_error', connection, }); expect(res).toEqual({ @@ -286,14 +334,72 @@ test('updateOne upsert default false logCollection', async () => { }); const logged = await findLogCollectionRecordTestMongoDb({ logCollection, - requestId: 'updateOne_upsert_default_false', + requestId: 'updateOne_disable_no_match_error', }); expect(logged).toMatchObject({ blockId: 'blockId', connectionId: 'connectionId', pageId: 'pageId', payload: { payload: true }, - requestId: 'updateOne_upsert_default_false', + requestId: 'updateOne_disable_no_match_error', + before: null, + after: null, + type: 'MongoDBUpdateOne', + meta: { meta: true }, + }); +}); + +test('updateOne disableNoMatchError false', async () => { + const request = { + filter: { _id: 'updateOne_disable_no_match_error_false' }, + update: { $set: { v: 'after' } }, + disableNoMatchError: false, + }; + const connection = { + databaseUri, + databaseName, + collection, + write: true, + }; + expect(async () => { + await MongoDBUpdateOne({ request, connection }); + }).rejects.toThrow('No matching record to update.'); +}); + +test('updateOne disableNoMatchError false logCollection', async () => { + const request = { + filter: { _id: 'updateOne_disable_no_match_error_false' }, + update: { $set: { v: 'after' } }, + disableNoMatchError: false, + }; + const connection = { + databaseUri, + databaseName, + collection, + changeLog: { collection: logCollection, meta: { meta: true } }, + write: true, + }; + await expect(async () => { + await MongoDBUpdateOne({ + request, + blockId: 'blockId', + connectionId: 'connectionId', + pageId: 'pageId', + payload: { payload: true }, + requestId: 'updateOne_disable_no_match_error_false', + connection, + }); + }).rejects.toThrow('No matching record to update.'); + const logged = await findLogCollectionRecordTestMongoDb({ + logCollection, + requestId: 'updateOne_disable_no_match_error_false', + }); + expect(logged).toMatchObject({ + blockId: 'blockId', + connectionId: 'connectionId', + pageId: 'pageId', + payload: { payload: true }, + requestId: 'updateOne_disable_no_match_error_false', before: null, after: null, type: 'MongoDBUpdateOne', From 4c3ad0c7a26d8abaf092ac2c7e53462c5500e4c1 Mon Sep 17 00:00:00 2001 From: Ioannis Ktistakis <47716028+Yianni99@users.noreply.github.com> Date: Thu, 30 Nov 2023 09:23:28 +0200 Subject: [PATCH 3/3] chore: Add changeset. --- .changeset/shaggy-ants-pretend.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shaggy-ants-pretend.md diff --git a/.changeset/shaggy-ants-pretend.md b/.changeset/shaggy-ants-pretend.md new file mode 100644 index 0000000..9fda420 --- /dev/null +++ b/.changeset/shaggy-ants-pretend.md @@ -0,0 +1,5 @@ +--- +'@lowdefy/community-plugin-mongodb': patch +--- + +Handle upsert true for error throw.