Specify custom function for children order by
See original GitHub issueIssue Description
Is your feature request related to a problem? Please describe.
I have a model Exercise
and I would like to specify a function for order by for children association.
For example, I have the following scope defined in Exercise
:
// to deal with the horrible order by clauses generically generated
orderByClauses(fields) {
// To handle order by clauses
// the last item in the values array will be the choice of the user (instead of the '')
const ORDER_BY_FIELDS = {
"state": ["state", ''],
"id": ["id", ''],
"title": ["title", ''],
"date": ['updatedAt', ''],
"avg_score": [
{model: sequelize.models.Exercise_Metrics, as: "metrics"},
'avg_vote_score',
''
],
"vote_count": [
{model: sequelize.models.Exercise_Metrics, as: "metrics"},
'vote_count',
''
],
};
return {
order: fields.map(field => {
let order_by_clause = ORDER_BY_FIELDS[field.field];
// Remove the last empty value so that we can add
order_by_clause.pop();
order_by_clause.push(field.value);
return order_by_clause;
})
};
}
Currently, it works as expected but I would like a custom filter on array so I added a new order by clause possibility in ORDER_BY_FIELDS
:
{
"tags_count": [
{model: sequelize.models.Exercise_Metrics, as: "metrics"},
Sequelize.fn("COALESCE",
Sequelize.fn(
"array_length",
Sequelize.col("tags_ids"),
1
),
0
),
''
]
}
But Sequelize generates something wrong :
-- ... SOME SELECT STUFF
FROM "exercises_library"."Exercises" AS "Exercise"
INNER JOIN "exercises_library"."Exercises_Metrics" AS "metrics"
ORDER BY "metrics".COALESCE(array_length("tags_ids", 1), 0) DESC
-- ... REST, LIKE OFFSET OR LIMIT
Describe the solution you’d like
As showcase in docs, we could use a complex object for ordering column
[{model: Task, as: 'Task'}, 'createdAt', 'DESC']
So why not allow something like this ?
const customFn = Sequelize.fn("COALESCE",
Sequelize.fn(
"array_length",
Sequelize.col("tags_ids"),
1
),
0
);
[{model: sequelize.models.Exercise_Metrics, as: "metrics", fn: customFn }, 'DESC']
That way, no breaking change and still backwards compatibility.
Why should this be in Sequelize
It could be useful for order by clauses for many functions : COALESCE
, LENGTH
, etc …
Describe alternatives/workarounds you’ve considered
- string manipulation with fn : no maintenable in long term
- include properties ( but not possible in many case )
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
React - How to send a custom function from a child to parent ...
I've tried setting State in the wizard, "nextClbRegistered", and through a send the "setNextClbRegistered" to children to pass the custom ...
Read more >wp_get_post_terms order by parent not ordering children and ...
I want to get the list of custom taxonomies for custom post type, for that i use the function wp_get_post_terms to list the...
Read more >How to Write Custom Sort Functions in Python
Discover how to write custom sort functions in Python, create a custom order in Python, and perform comparisons in Python.
Read more >Add custom function to a theme with child theme's functions.php
Set WP_DEBUG to true in your wp-config.php file. This will tell you exactly what your error is. – ngearing. May 18, 2015 at...
Read more >@CHILDREN - Oracle Help Center
This order is important to consider when you use the @CHILDREN member set function with certain forecasting and statistical functions. Example.
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 Free
Top 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
🤣 I have to agree with you on this. Recently I have been trying to refactor a lot of stuff before proceeding with more fixes…
Hmm what comes to my mind right now actually is: would you like to help me refactor stuff around? So that the codebase gets more approachable? That is probably the best path to enable better features in the future
I identified the parts of the code that should be updated to handle this new behaviour
To not prepend model name when we alias the attributes getQueryOrders