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 all methods to return plain JS objects #106

Merged
merged 3 commits into from
May 3, 2017

Conversation

DesignByOnyx
Copy link
Contributor

@DesignByOnyx DesignByOnyx commented May 2, 2017

Summary

This updates all service methods to return plain javascript objects by default ({ raw: true }). This is a breaking change (see below for an easy fix).

This ensures that feathers-sequelize will play nicely and consistently with common hooks and other 3rd party hooks and integrations. Users can pass { raw: false } to any of the service methods and get ORM-wrapped objects if they so choose. There are tests covering this new functionality located in this PR in feathers-service-hooks.

This PR also includes hooks so users can easily hydrate hook results into ORM objects and conversely dehydrate those objects back into plain javascript objects.

const hydrate = require('feathers-sequelize/hooks/hydrate');
const dehydrate = require('feathers-sequelize/hooks/dehydrate');
const { populate } = require('feathers-hooks-common');

hooks.after.find = [hydrate(), doSomethingCustom(), dehydrate(), populate()];

The conversation around this issue is spread out across multiple issues in multiple repositories. This is my best attempt to aggregate this conversion:

#18
#19
#24
feathersjs-ecosystem/feathers-hooks-common#144
feathersjs-ecosystem/feathers-hooks-common#146
feathersjs-ecosystem/feathers-hooks-common#150

@DesignByOnyx
Copy link
Contributor Author

DesignByOnyx commented May 2, 2017

This is a breaking change

Service methods will no longer return ORM wrapped objects. This could break code which relied on hook.result to contain Sequelize model instances instead of plain objects. There are several ways this can be fixed:

  1. Pass { raw: false } to the service constructor:
    app.use('/people', service({
        Model,
        raw: false
    }));
  2. Use a "before" hook to set { raw: false }. This hook can be used on any or all of the service methods:
    function makeRaw(hook) {
        if (!hook.params.sequelize) hook.params.sequelize = {};
        Object.assign(hook.params.sequelize, { raw: false });
        return hook;
    }
    hooks.find.before = [makeRaw];
  3. Use the new hydrate hook in the "after" phase. This can be used on any or all of the service methods:
    const hydrate = require('feathers-sequelize/hooks/hydrate');
    hooks.after.all = [hydrate()];

@ekryski
Copy link
Member

ekryski commented May 3, 2017

Looks good! Thanks!

@ekryski ekryski merged commit c375111 into feathersjs-ecosystem:master May 3, 2017
@ekryski
Copy link
Member

ekryski commented May 3, 2017

Released as v2.0.0

@eddyystop
Copy link

I am really happy to see this. ❤️ I now won't have to answer the same question over and over again. Thanks bunches!

@petermikitsh
Copy link

This is an excellent default to have! Happy to see it made it in. Wooot!

@chasenlehara chasenlehara mentioned this pull request Jul 11, 2017
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

Successfully merging this pull request may close these issues.

4 participants