Yesterday I opened a issue on objection.js (issue 1153) asking how to do a recursive eager with a additional relation object.
Basically I wanted to convert a existing expression ([user, replies.[user, replies.[user, replies.[user, replies.[user, replies.[user]]]]]]
) into a prettier looking recursive expression (replies.^5
) with each reply having a user relation attached.
They suggested using objection's object notation with a function to call itself recursively to create a nested replies object. Which worked well.
function nestedReplies(depth) {
if (depth === 0) return {}
return {
user: true,
replies: nestedReplies(depth - 1),
}
}
var allowedEager = nestedReplies(5);
Now previously I was using a named eager filter in my ugly expression ([user, replies(replyFilter).[user, replies(replyFilter).[user, replies(replyFilter).[user, replies(replyFilter).[user, replies(replyFilter).[user]]]]]]
) which I'm having trouble porting to the objection nation.
Objection's parseRelationExpression() converted my ugly expression to this.
{
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true
}
}
}
}
}
}
So I added a $modify:["replyFilter"]
property to my nestedReplies()'s output so the object notation matched objection's parsed version of my ugly expresion.
Problem I'm having now is the $modify keys are getting stripped somewhere before feathers-objection calls q.eager(query.$eager, this.namedEagerFilters);
.
I use a feathers hook to set my default query.$eager property.
function before(context) {
context.params.query = context.params.query || {};
context.params.query["$eager"] = context.params.query["$eager"] || allowedEager;
console.log("$eager: " + JSON.stringify( context.params.query["$eager"], null, "\t"));
...
}
Which prints to console
$eager: {
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {}
}
}
}
}
}
}
I added two debug messages to feathers-objection's createQuery.
console.log("<createQuery> params.query: " + JSON.stringify( params.query, null, "\t" ));
console.log("<createQuery> _filterQuery: " + JSON.stringify( _filterQuery, null, "\t" ));
Which prints out
<createQuery> params.query: {
"$eager": {
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {
"$modify": [
"replyFilter"
],
"user": true,
"replies": {}
}
}
}
}
}
}
}
<createQuery> _filterQuery: {
"filters": {},
"query": {
"$eager": {
"user": true,
"replies": {
"user": true,
"replies": {
"user": true,
"replies": {
"user": true,
"replies": {
"user": true,
"replies": {
"user": true,
"replies": {}
}
}
}
}
}
}
}
}
I thought maybe it was @feathersjs/commons' filterQuery() function that was stripping the keys, but when I add debug messages to the function they never execute. So now I'm stuck. Any ideas on what might be removing the $modify keys from my eager object.
This isn't a huge priority. The string notation works fine for now. Just thought I'd bring it up in case it was fixable.