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

Patching multiple records that update the queried value doesn't emit any service events #393

Closed
danielang opened this issue Aug 18, 2016 · 5 comments
Labels

Comments

@danielang
Copy link

I tried this with the Mongoose and the NeDB service.

  • I create some records:
service.create({text: 'Test 1', number: 123});
service.create({text: 'Test 1', number: 123});
service.create({text: 'Test 1', number: 123});
  • I bind the patched event to a callback:
service.on('patched', function(patchedObject) { 
  debugger;
  console.log(patchedObject);
});
  • Then I patch the number property of all entries who has the text: 'Test 1':
service.patch(null, {number: 456}, { query: { text: 'Test 1' }});

The patch was successful in DB, the event-callback is fired for 3 times and the promise was resoved with an array[3].

  • I patch all entries who has the text: 'Test 1' to text: 'Test 2'
service.patch(null, {text: 'Test 2'}, { query: { text: 'Test 1' }});

The patch in the DB was successful, but no event was fired and the resolved promise contains an empty array.

@danielang
Copy link
Author

danielang commented Aug 18, 2016

Ok... I figured it out.
It is a 'bug' in the patch method. After you patched the datas in the DB, the service tries to find the patched data with the old query, but after the patch the values of the query has been changed.

A workaround could be this (I wrote this in the compiled ES5 file) in NeDB::patch

function patch(id, data, params) {
  var _this = this;

  var _multiOptions = (0, _utils.multiOptions)(id, this.id, params);

  var query = _multiOptions.query;
  var options = _multiOptions.options;

  // Run the query

  return (0, _utils.nfcall)(this.Model, 'update', query, {
    $set: (0, _lodash2.default)(data, this.id, '_id')
  }, options).then(function () {
    Object.keys(params.query).forEach(key => {
      if (data.hasOwnProperty(key)) {
        params.query[key] = data[key];
      }
    });


    return _this._findOrGet(id, params);
  });
}

@daffl
Copy link
Member

daffl commented Aug 18, 2016

Good catch. Might be tricky to fix though because you'd have to know how the database is querying records (e.g. in MongoDB we'd have to turn the new data into a dot-notation query). Ideally the db would return all changed records (or at least the id of all changed records) but SQL dbs seem to make that a little more difficult.

@daffl
Copy link
Member

daffl commented Sep 9, 2016

Related: feathersjs-ecosystem/feathers-knex#52

@ekryski ekryski added the Bug label Oct 17, 2016
@daffl daffl changed the title patching multiple records witch update the queried value doesn't emit any service events Patching multiple records that update the queried value doesn't emit any service events Nov 12, 2016
@daffl
Copy link
Member

daffl commented Nov 15, 2016

Tests have been added in feathersjs-ecosystem/feathers-service-tests@af1add3 and fixed and released in all official adapters.

@daffl daffl closed this as completed Nov 15, 2016
@lock
Copy link

lock bot commented Feb 7, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants