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

Add lean attribute to automatically call toJSON? #19

Closed
ekryski opened this issue Feb 26, 2016 · 5 comments
Closed

Add lean attribute to automatically call toJSON? #19

ekryski opened this issue Feb 26, 2016 · 5 comments
Labels

Comments

@ekryski
Copy link
Member

ekryski commented Feb 26, 2016

This isn't officially an attribute in sequelize but it's pretty handy for feathers-mongoose I'm wondering if we should implement it here as well.

@kulakowka
Copy link

I'm sure that this must be done

@petermikitsh
Copy link

Consistent results would really be nice. You really want to avoid the ORM objects at all times, if possible. Here's what I've been doing as a stop gap solution...

app.hooks({
  after(hook) {
    if (hook.method === 'create' && typeof _.get(hook, 'result.toJSON') === 'function') {
      hook.result = hook.result.toJSON();
    }
  }
});

One side effect I recently encountered was that, in my service filter functions, I'd have to inspect to see whether or not the first parameter (data) was a sequelize DAO or a plain, javascript object. This is because create (by default) returns a DAO, while all other service methods will return POJO's when you add query: {raw: true} to your sequelize connection object (more on that here). This filter solves the need for that check.

But yeah, avoiding the ORM-specific logic inside of service code is, imo, a good direction for this adapter.

@eddyystop
Copy link

Bump.

@MichaelErmer
Copy link
Contributor

This would be good, it would save a lot of extra calls in hooks like:

      // If it's a mongoose model then
      if (typeof obj.toObject === 'function') {
        obj = obj.toObject(); 
      }
      // If it's a Sequelize model   
      else if (typeof obj.toJSON === 'function') {
        obj = obj.toJSON();
      }

@ekryski
Copy link
Member Author

ekryski commented May 3, 2017

The attribute has been changed to raw: true by default. v2.0.0 now returns regular JavaScript objects by default and there are hooks to covert to and from Sequelize models. See #106 for more info.

@ekryski ekryski closed this as completed May 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants