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

update() breaks when sequelize configured with raw:true #99

Closed
buske opened this issue Mar 24, 2017 · 7 comments
Closed

update() breaks when sequelize configured with raw:true #99

buske opened this issue Mar 24, 2017 · 7 comments

Comments

@buske
Copy link
Contributor

buske commented Mar 24, 2017

Affects master, v1.4.3, etc.

A call to update() results in a Model.findById call:
/~https://github.com/feathersjs/feathers-sequelize/blob/master/src/index.js#L168

If sequelize is configured with raw: false (the default), this returns a DAO, with a toJSON method, which is then called several lines later:
/~https://github.com/feathersjs/feathers-sequelize/blob/master/src/index.js#L174

However, if sequelize is configured with raw: true, the findById call returns a JSON object without this method.

There are two simple fixes:

  1. Only call toJSON if it exists and is a function
  2. pass {raw: false} as a second argument to the findById call to force the expected behaviour
@daffl
Copy link
Member

daffl commented Mar 24, 2017

Makes sense. This can probably easily be fixed by changing the line you pointed out to

Object.keys(typeof instance.toJSON === 'function' ? instance.toJSON() : instance).forEach(key => {

@daffl
Copy link
Member

daffl commented Mar 24, 2017

Thanks to @lowip this has been fixed in #100 and released as v1.4.4

@daffl daffl closed this as completed Mar 24, 2017
@lowip
Copy link
Contributor

lowip commented Mar 24, 2017

Unfortunately my PR might have been too fast

In order to update, the DOA instance is used:
/~https://github.com/feathersjs/feathers-sequelize/blob/master/src/index.js#L182

return instance.update(copy, options);

@lowip
Copy link
Contributor

lowip commented Mar 24, 2017

The best solution I see would be replacing:
/~https://github.com/feathersjs/feathers-sequelize/blob/master/src/index.js#L168

return this.Model.findById(id).then(instance => {

with

return this.Model.findById(id, { raw: false }).then(instance => {

@lowip lowip mentioned this issue Mar 24, 2017
3 tasks
@daffl
Copy link
Member

daffl commented Mar 25, 2017

Ah, I see. Well at least we know it didn't break anything else ;)

I think we should do what patch does and use this.Model.update(omit(data, this.id), options) instead of calling save on the model instance.

@daffl daffl reopened this Mar 25, 2017
@lowip
Copy link
Contributor

lowip commented Mar 25, 2017

I updated the PR #101, would love some feedback on the issue I'm raising here:
b6a4128

Thanks!

@daffl
Copy link
Member

daffl commented Mar 26, 2017

Now it's been fixed via #101 in v1.4.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants