Skip to content

Commit

Permalink
Merge pull request #34 from lowdefy/fix-mongodb-update-one
Browse files Browse the repository at this point in the history
Fix mongodb update one
  • Loading branch information
SamTolmay authored Nov 30, 2023
2 parents 375f7b6 + 4c3ad0c commit 6f99a29
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-ants-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lowdefy/community-plugin-mongodb': patch
---

Handle upsert true for error throw.
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -202,43 +197,95 @@ 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',
meta: { meta: true },
});
});

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,
Expand All @@ -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,
Expand All @@ -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({
Expand All @@ -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',
Expand Down

0 comments on commit 6f99a29

Please sign in to comment.