How check associations?
See original GitHub issueHi 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:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
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 useraw: false
, etc. I don’t mind taking this responsibility.