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

How check associations? #127

Closed
evgarik opened this issue Jun 16, 2017 · 6 comments
Closed

How check associations? #127

evgarik opened this issue Jun 16, 2017 · 6 comments
Labels

Comments

@evgarik
Copy link

evgarik commented Jun 16, 2017

Hi guys, Help me please.
i'am read documentation - https://sequelize.readthedocs.io/en/v3/docs/associations/#check-associations
How to execute a check on the service hook.

function include() {
  return function (hook) {
    const productPrice = hook.app.service('product-prices').Model;
    const currencies = hook.app.service('currencies').Model;
    const edizm = hook.app.service('edizm').Model;


    const pricesShema = { model: productPrice,
      attributes: ['price', 'price_promo', 'price_card','publish'],
      include: [
        {
          model: currencies,
          attributes: ['title', 'socr']
        },
      ]
    };

    const edizmShema = { model: edizm,
      attributes: ['title', 'detail']
    };

    let association={};
    switch (hook.method){
      case "find":
        association = { include: [edizmShema,pricesShema] };
        break;
      case "get":
        association = { include: [edizmShema,pricesShema] };
        break;
    }


    hook.params.sequelize = Object.assign(association,
      { raw: false },
      {
        attributes: [
        'id',
        'title',
        'count',
        'image',
        'detail',
        'sostav',
        'edizm_id'
      ]});

    return Promise.resolve(hook);
  }
}


module.exports = {

  before: {
    all: [include()],
    find: [],
    get: [],
    create: disallow(),
    update: disallow(),
    patch: disallow(),
    remove: disallow()
  },

  after: {
    all: [include()],
    find: [],
    get: [],
    create: disallow(),
    update: disallow(),
    patch: disallow(),
    remove: disallow()
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: disallow(),
    update: disallow(),
    patch: disallow(),
    remove: disallow()
  }
};
@eddyystop
Copy link

eddyystop commented Jun 16, 2017

Feathers hooks work with POJO's not with DB ORM's, and your raw: false returns an ORM.

If you really can't use raw: true then convert the ORM to a POJO:

const dehydrate = require('feathers-sequelize/hooks/dehydrate');
hooks.after.find = [dehydrate(), discard('password')]

You can convert back to an ORM (if you really need to). https://github.com/feathersjs/feathers-sequelize/tree/master/src/hooks

@daffl
Copy link
Member

daffl commented Jun 17, 2017

I think include shouldn't be registered as a before and after hook. If you set raw: false we can check the association in a custom after hook though (and then convert it back to a plain JavaScript object as @eddyystop suggested).

I'm going to reopen for now because I think this might be worth looking into a little more (unless I'm missing something and @eddyystop already provided everything that is needed). Related Stackoverflow question: https://stackoverflow.com/questions/44593261/how-to-check-associations-feathers-sequelize-in-service-hook

@daffl daffl reopened this Jun 17, 2017
@evgarik
Copy link
Author

evgarik commented Jun 17, 2017

@daffl , Thanks for the help. I'll wait for your decision.

@DesignByOnyx
Copy link
Contributor

I think this falls under the the umbrella of working with model instances. As soon as someone wants to do something sequelize-specific (such as foo.hasUser()), they need to instantiate the data. There are several ways to do this, and "which one" depends on the situation. A recurring theme is we need to do a better job of making people aware of when to "hydrate", when to "dehydrate", when to use raw: false, etc. I don't mind taking this responsibility.

@musicformellons
Copy link

General idea is clear, but the docs confuse me. The 2 ways are documented as alternatives it seems, but in the quoted gist they are both implemented, which is odd (and thus raises a question by someone in the gist, but it is not answered). I suppose alternative 1 (using { raw: false }) would work in both before- and after-hooks, whereas alternative 2 (hydrate (dehydrate)) works only for after-hooks!?

A 'general best practice' example would clarify a lot. For after-hooks is it as simple as hydrating before and dehydrating after each customhook (or each sequence of customhooks)? And for before-hooks the 'equivalent' is to set { raw: false } for each customhook?

@stale
Copy link

stale bot commented Apr 25, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Apologies if the issue could not be resolved. FeathersJS ecosystem modules are community maintained so there may be a chance that there isn't anybody available to address the issue at the moment. For other ways to get help see here.

@stale stale bot added the wontfix label Apr 25, 2018
@stale stale bot closed this as completed May 2, 2018
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