Skip to content

Commit 3fb1367

Browse files
node-mongo@3.4.0-beta.2: updateOne and updateMany enhancements (#382)
* feat(service): updateOne and updateMany enhancements - Added support for passing MongoDB update operators - Added tests for updateOne and updateMany methods * fix: Fix comments * chore: bump version to 3.4.0-beta.2 and update publish script to support custom tags --------- Co-authored-by: fruneen <fruneen@gmail.com>
1 parent d62e1ba commit 3fb1367

9 files changed

Lines changed: 995 additions & 55 deletions

File tree

docs/packages/node-mongo/api-reference/service.mdx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Fetches the first document that matches the filter. Returns `null` if document w
6464
```typescript
6565
updateOne = async <U extends T = T>(
6666
filter: Filter<U>,
67-
updateFn: (doc: U) => Partial<U>,
67+
updateFilterOrFn: (doc: U) => Partial<U> | UpdateFilter<U>,
6868
updateConfig: UpdateConfig = {},
6969
updateOptions: UpdateOptions = {},
7070
): Promise<U | null>
@@ -81,13 +81,18 @@ const updatedUser = await userService.updateOne(
8181
(doc) => ({ fullName: 'Updated fullname' }),
8282
{ publishEvents: false }
8383
);
84+
85+
const updatedUserWithUpdateFilter = await userService.updateOne(
86+
{ _id: u._id },
87+
{ $set: { fullName: 'Updated fullname' }},
88+
);
8489
```
8590

8691
Updates a single document and returns it. Returns `null` if document was not found.
8792

8893
**Parameters**
8994
- filter: [`Filter<U>`](https://mongodb.github.io/node-mongodb-native/4.10/modules.html#Filter);
90-
- updateFn: `(doc: U) => Partial<U>`;
95+
- updateFilterOrFn: `(doc: U) => Partial<U>` | [`UpdateFilter<U>`](https://mongodb.github.io/node-mongodb-native/4.10/modules.html#UpdateFilter);
9196
Function that accepts current document and returns object containing fields to update.
9297
- updateConfig: [`UpdateConfig`](#updateconfig);
9398
- updateOptions: [`UpdateOptions`](https://mongodb.github.io/node-mongodb-native/4.10/interfaces/UpdateOptions.html);
@@ -99,7 +104,7 @@ Updates a single document and returns it. Returns `null` if document was not fou
99104
```typescript
100105
updateMany = async <U extends T = T>(
101106
filter: Filter<U>,
102-
updateFn: (doc: U) => Partial<U>,
107+
updateFilterOrFn: (doc: U) => Partial<U> | UpdateFilter<U>,
103108
updateConfig: UpdateConfig = {},
104109
updateOptions: UpdateOptions = {},
105110
): Promise<U[]>
@@ -110,13 +115,18 @@ const updatedUsers = await userService.updateMany(
110115
{ status: 'active' },
111116
(doc) => ({ isEmailVerified: true }),
112117
);
118+
119+
const updatedUsersWithUpdateFilter = await userService.updateMany(
120+
{ status: 'active' },
121+
{ $set: { isEmailVerified: true } },
122+
);
113123
```
114124

115125
Updates multiple documents that match the query. Returns array with updated documents.
116126

117127
**Parameters**
118128
- filter: [`Filter<U>`](https://mongodb.github.io/node-mongodb-native/4.7/modules.html#Filter);
119-
- updateFn: `(doc: U) => Partial<U>`;
129+
- updateFilterOrFn: `(doc: U) => Partial<U>` | [`UpdateFilter<U>`](https://mongodb.github.io/node-mongodb-native/4.10/modules.html#UpdateFilter);
120130
Function that accepts current document and returns object containing fields to update.
121131
- updateConfig: [`UpdateConfig`](#updateconfig);
122132
- updateOptions: [`UpdateOptions`](https://mongodb.github.io/node-mongodb-native/4.10/interfaces/UpdateOptions.html);

packages/node-mongo/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.eslintrc.js
33
src/config
44
src/*.test.js
5+
.env

packages/node-mongo/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ Fetches the first document that matches the filter. Returns `null` if document w
256256
### `updateOne`
257257

258258
```typescript
259-
updateOne: (
260-
filter: Filter<T>,
261-
updateFn: (doc: T) => Partial<T>,
259+
updateOne<U extends T = T>: (
260+
filter: Filter<U>,
261+
updateFilterOrFn: (doc: U) => Partial<U> | UpdateFilter<U>,
262262
updateConfig: UpdateConfig = {},
263263
updateOptions: UpdateOptions = {},
264-
): Promise<T | null>
264+
): Promise<U | null>
265265
```
266266

267267
```typescript
@@ -292,12 +292,12 @@ Updates a single document and returns it. Returns `null` if document was not fou
292292
### `updateMany`
293293

294294
```typescript
295-
updateMany: (
296-
filter: Filter<T>,
297-
updateFn: (doc: T) => Partial<T>,
295+
updateMany<U extends T = T>: (
296+
filter: Filter<U>,
297+
updateFilterOrFn: (doc: U) => Partial<U> | UpdateFilter<U>,
298298
updateConfig: UpdateConfig = {},
299299
updateOptions: UpdateOptions = {},
300-
): Promise<T[]>
300+
): Promise<U[]>
301301
```
302302

303303
```typescript

packages/node-mongo/bin/publish.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ blue=`tput setaf 4`
1111
reset=`tput sgr0`
1212

1313
VERSION=$1
14+
TAG=${2:-latest}
1415
MODULE='node-mongo'
1516

1617
if [ -z "$1" ] ; then
@@ -35,7 +36,7 @@ MODULE_VERSION=$(grep version package.json | sed 's/.*"version": "\(.*\)".*/\1/'
3536

3637
echo ">>> Current version for module: ${blue}@paralect/$MODULE${reset} is ${yellow}$MODULE_VERSION${reset}"
3738
echo ">>> Bumping version to ${yellow}$VERSION${reset}"
38-
npm run test:eslint 1>/dev/null && npm run build 1>/dev/null && npm version $VERSION && npm publish
39+
npm run test:eslint 1>/dev/null && npm run build 1>/dev/null && npm version $VERSION && npm publish --tag $TAG
3940

4041
MODULE_VERSION_NEW=$(grep version package.json | sed 's/.*"version": "\(.*\)".*/\1/')
4142

packages/node-mongo/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/node-mongo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@paralect/node-mongo",
3-
"version": "3.4.0-beta.1",
3+
"version": "3.4.0-beta.2",
44
"author": "Paralect",
55
"description": "Reactive MongoDB wrapper for Node.JS",
66
"private": false,

0 commit comments

Comments
 (0)