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

fix(mongodb): MongoDB Aggregation improvements #3366

Merged
merged 26 commits into from
May 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2698b30
Update docs
DaddyWarbucks Dec 1, 2023
8a4840f
Allow _get method to use pipeline
DaddyWarbucks Dec 2, 2023
39ac4a1
Add comment
DaddyWarbucks Dec 2, 2023
a2a1d55
Disallow pipeline with mongodb param
DaddyWarbucks Dec 2, 2023
47962fb
Use findOneAndReplace in _update method
DaddyWarbucks Dec 5, 2023
39b0387
Use findOneAndUpdate in patch method
DaddyWarbucks Dec 5, 2023
4795929
Allow $sort with _delete method
DaddyWarbucks Dec 5, 2023
bf252e5
Allow pipeline and better mongo methods in _delete
DaddyWarbucks Dec 5, 2023
8a49c54
Sort before select in pipeline
DaddyWarbucks Dec 5, 2023
60c1819
USe mongo projection in create instead of common select
DaddyWarbucks Dec 5, 2023
ec45ecf
Simplify getProjection method
DaddyWarbucks Dec 5, 2023
c0e3c05
Start working on pipeline count, add pipeline to _update
DaddyWarbucks Dec 5, 2023
55100a4
Improve _update and _get, add tests
DaddyWarbucks Dec 5, 2023
d0b5b11
Cleanup formatting
DaddyWarbucks Dec 5, 2023
ca59178
Better pipeline in _patch
DaddyWarbucks Dec 5, 2023
f77ff98
Count documents
DaddyWarbucks Dec 9, 2023
64ee399
Cleanup comments
DaddyWarbucks Dec 9, 2023
ced2cdc
Fix count and pagination
DaddyWarbucks Jan 24, 2024
06790a0
Add tests
DaddyWarbucks Jan 24, 2024
cd067fd
Use aggregation in create
DaddyWarbucks Mar 31, 2024
a7f683d
Update docs
DaddyWarbucks Mar 31, 2024
4f52ad8
Add test
DaddyWarbucks Mar 31, 2024
cf0c9d5
Fix typos
DaddyWarbucks Apr 18, 2024
dd6fcc4
Add $sort/$limit tests
DaddyWarbucks Apr 25, 2024
3908eee
Remove unused method.
DaddyWarbucks Apr 26, 2024
4be451b
Fix tests
DaddyWarbucks May 10, 2024
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
Prev Previous commit
Next Next commit
Use findOneAndUpdate in patch method
DaddyWarbucks committed Dec 5, 2023

Verified

This commit was signed with the committer’s verified signature.
elim Takeru Naito
commit 39b0387459b6be43d55853708ef6f44d71c6608d
64 changes: 44 additions & 20 deletions packages/mongodb/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
CountDocumentsOptions,
ReplaceOptions,
FindOneAndReplaceOptions,
FindOneAndUpdateOptions,
Document
} from 'mongodb'
import { BadRequest, MethodNotAllowed, NotFound } from '@feathersjs/errors'
@@ -345,8 +346,8 @@ export class MongoDbAdapter<
query,
filters: { $select }
} = this.filterQuery(id, params)
const updateOptions = { ...params.mongodb }
const modifier = Object.keys(data).reduce((current, key) => {

const replacement = Object.keys(data).reduce((current, key) => {
const value = (data as any)[key]

if (key.charAt(0) !== '$') {
@@ -360,28 +361,51 @@ export class MongoDbAdapter<

return current
}, {} as any)
const originalIds = await this._findOrGet(id, {
...params,
query: {
...query,
$select: [this.id]
},
paginate: false
})
const items = Array.isArray(originalIds) ? originalIds : [originalIds]
const idList = items.map((item: any) => item[this.id])
const findParams = {
...params,
paginate: false,
query: {
[this.id]: { $in: idList },
$select

if (id === null) {
const updateOptions = { ...params.mongodb }
const originalIds = await this._findOrGet(id, {
...params,
query: {
...query,
$select: [this.id]
},
paginate: false
})
const items = Array.isArray(originalIds) ? originalIds : [originalIds]
const idList = items.map((item: any) => item[this.id])
const findParams = {
...params,
paginate: false,
query: {
[this.id]: { $in: idList },
$select
}
}

await model.updateMany(query, replacement, updateOptions)

return this._findOrGet(id, findParams).catch(errorHandler)
}

await model.updateMany(query, modifier, updateOptions)
const projection = $select
? {
...this.getSelect($select),
[this.id]: 1
}
: {}

return this._findOrGet(id, findParams).catch(errorHandler)
const result = await model.findOneAndUpdate(query, replacement, {
...(params.mongodb as FindOneAndUpdateOptions),
returnDocument: 'after',
projection
})

if (result.value === null) {
throw new NotFound(`No record found for id '${id}'`)
}

return result.value as Result
}

async _update(id: AdapterId, data: Data, params: ServiceParams = {} as ServiceParams): Promise<Result> {