question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How check associations?

See original GitHub issue

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()
  }
};

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
musicformellonscommented, Dec 1, 2017

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?

1reaction
DesignByOnyxcommented, Jul 21, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing ActiveRecord associations in the Rails console
Check the schema to verify that the tables have the correct structure. Test the ActiveRecord models associations.
Read more >
Check and visualize associations between features and ...
This function computes different measures of association between features and the label and visualizes the results.
Read more >
Harvard Implicit Association Test - Harvard University
On the next page you'll be asked to select an Implicit Association Test (IAT) from a list of possible topics . We will...
Read more >
Is there any way to check that has_many association exists ...
You could probably use respond_to? class ActiveRecord::Base def self.has_many_association_exists?(related) self.class.associations ...
Read more >
Active Record Associations
This guide covers the association features of Active Record. After reading this guide, you will know: How to declare associations between Active Record...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found