Skip to content

Commit

Permalink
Merge pull request #33 from lowdefy/disable-no-match-error
Browse files Browse the repository at this point in the history
Disable no match error
  • Loading branch information
SamTolmay authored Nov 29, 2023
2 parents 380a885 + 6ffde8a commit 375f7b6
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-trains-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lowdefy/community-plugin-mongodb': patch
---

Update dependency mongodb to v6.3.0.
6 changes: 6 additions & 0 deletions .changeset/short-rice-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lowdefy/community-plugin-mongodb': major
---

The `MongoDBUpdateOne` request now throws an error if no document was matched and updated. This behaviour can be disabled by setting the new `disableNoMatchError` property.

66 changes: 66 additions & 0 deletions apps/docs/community-plugin-mongodb/MongoDB.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,75 @@ _ref:
## Requests
Request types:
- MongoDBUpdateOne
- MongoDBInsertConsecutiveId
- MongoDBInsertManyConsecutiveIds
### MongoDBUpdateOne
#### Properties
The `MongoDBUpdateOne` request modifies the standard [MongoDBUpdateOne](https://docs.lowdefy.com/MongoDB) to throw an error if no document was matched and updated. This behaviour can be disabled by setting the `disableNoMatchError` property.
- `disableNoMatchError: boolean`: Set to true to disable the no match error when updating. It is set to false by default. _This is not the standard MongoDBUpdateOne behaviour and will throw an error when there is no matching document._
The request returns the following:
If a log collection is not set on the connection
- `acknowledged: boolean` - Acknowledgement of the insertion.
- `matchedCount: number` - The number of matched documents.
- `modifiedCount: number` - The number of modified documents.
- `upsertedId: string` - The ID of the upserted document.
- `upsertedCount: number` - The number of upserts.
If a log collection is set on the connection
- `lastErrorObject: object` - An object containing data on whether an existing document was updated or not.
- `ok: number` - Status of the request, 1 if the request was successful and 0 otherwise.
- `'$clusterTime': object` - An object containing data on the cluster time and signature.
- `operationTime: date` - Timestamp object of the operation time.
#### Example
###### The disableNoMatchError property can be useful on a request that is in an action chain of other requests that should be executed whether or not it fails.
```yaml
requests:
- id: item_request_1
type: MongoDBUpdateOne
connectionId: items
payload:
id:
_state: item._id
properties:
disableNoMatchError: true
filter:
_id:
_payload: id
status: In Progress
update:
- $set:
...
- id: item_request_2
...
...
blocks:
- id: update_items
type: Button
properties:
title: Update Item
events:
onClick:
- id: item_request_1
type: Request
params: item_request_1
- id: item_request_2
type: Request
params: item_request_2
messages:
success: Item updated
```
### MongoDBInsertConsecutiveId
#### Properties
Expand Down
2 changes: 1 addition & 1 deletion plugins/community-plugin-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dependencies": {
"@lowdefy/connection-mongodb": "^4.0.0-rc.12",
"@lowdefy/helpers": "^4.0.0-rc.12",
"mongodb": "5.6.0",
"mongodb": "6.3.0",
"next-auth": "4",
"saslprep": "1.0.3",
"uuid": "9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ async function MongodbDeleteOne({
let response;
try {
if (logCollection) {
const { value, ...responseWithoutValue } = await collection.findOneAndDelete(filter, options);
const { value, ...responseWithoutValue } = await collection.findOneAndDelete(filter, {
...options,
includeResultMetadata: true,
});
response = responseWithoutValue;
await logCollection.insertOne({
args: { filter, options },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ async function MongodbUpdateOne({
payload,
}) {
const deserializedRequest = deserialize(request);
const { filter, update, options } = deserializedRequest;
const { filter, update, options, disableNoMatchError } = deserializedRequest;
const { collection, client, logCollection } = await getCollection({ connection });
let response;
try {
if (logCollection) {
const { value, ...responseWithoutValue } = await collection.findOneAndUpdate(
filter,
update,
options
);
const { value, ...responseWithoutValue } = await collection.findOneAndUpdate(filter, update, {
...options,
includeResultMetadata: true,
});
response = responseWithoutValue;
const after = await collection.findOne({
_id: value ? value._id : response.lastErrorObject?.upserted,
Expand All @@ -55,8 +54,14 @@ async function MongodbUpdateOne({
type: 'MongoDBUpdateOne',
meta: connection.changeLog?.meta,
});
if (!disableNoMatchError && !response.lastErrorObject.updatedExisting) {
throw new Error('No matching record to update.');
}
} else {
response = await collection.updateOne(filter, update, options);
if (!disableNoMatchError && response.matchedCount === 0) {
throw new Error('No matching record to update.');
}
}
} catch (error) {
await client.close();
Expand Down
72 changes: 52 additions & 20 deletions pnpm-lock.yaml

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

0 comments on commit 375f7b6

Please sign in to comment.