Skip to content

Mongo .update with query #3363

Closed
Closed

Description

@DaddyWarbucks

The update method is currently breaking when using data that affects the query.

For example,

const dave = await app.service('people').create({ name: 'Dave' })

// Now mutate AND query the name property.
const result = await app
  .service('people')
  .update(dave._id, { name: 'Marshal' }, { query: { name: 'Dave' } })

The problem originates here:

return this._findOrGet(id, params).catch(errorHandler)

We have updated the data, but then this following _findOrGet is using the original query. Because our data mutated a value in that query, the request throws a NotFound (even though the record was updated); This likely needs to be handled similarly to patch/remove.

I am currently working on a mongoJoinQuery for a project that will ultimately end up in feathers-fletching. So I am taking a deep dive in the new Mongo adapter. I will eventually create a PR for this problem and any others I stumble across.

Activity

  1. DaddyWarbucks commented on Dec 1, 2023

    @DaddyWarbucks
    MemberAuthor

    Furthermore, this should probably be a test in the common adapters test suite.

  2. daffl commented on Dec 1, 2023

    @daffl
    Member

    Ah, same problem we had with .patch where it now uses a list of original ids instead. Since update doesn't support multiple updates, should it just remove the query?

    return this._findOrGet(id, {
      ...params,
      query: {}
    }).catch(errorHandler)
  3. DaddyWarbucks commented on Dec 2, 2023

    @DaddyWarbucks
    MemberAuthor

    I like the query because it throws a NotFound actually. I often use this for some security benefits where I stub on some extra params in the query serverside.

    // Serverside hook after auth hook
    const permissionsHook = (context) => {
      context.params.query.organizationId = context.params.user.organizationId
    }
    // This will throw a NotFound it item with id: 1 is not in my organization
    const item = await app.service('items').get(1)
  4. DaddyWarbucks commented on Dec 2, 2023

    @DaddyWarbucks
    MemberAuthor

    I am taking a deep dive on this new version of the adapter because I need some fixes/features for a particular project.
    See: dove...DaddyWarbucks:feathers:mongodb/aggregate

    So I will open a PR soon with

    • Whatever fix we decide for this problem
    • Allow aggregation in the _get method
    • Possibly allow params.mongodb to be used with the aggregation framework
    • Whatever else I stumble across
  5. DaddyWarbucks commented on May 29, 2024

    @DaddyWarbucks
    MemberAuthor

    Closed via #3366

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions