Acapulco
Acapulco

Reputation: 3523

Enable nestRemoting for more than 2 levels (for nested queries)

My question is basically is, how can I enable nestRemoting (nested queries for related models) for more than 3 related models?

Currently I am working with 4 related models and would like to be able to do nested queries all the way up or down, and right now I am only able to do so for 3 models (i.e. 2 levels up and 2 levels down).

What works:

So for example, I have my Account model which is the root of everything, i.e. all other models eventually lead to an Account) and then I have let's say Projects, Folders then Photos as leaves.

I already configured my relations and it works perfectly fine, e.g.

and the other way around as well:

and finally in my code I added:

// Configure one way of the relation
Account.nestRemoting('projects');
Project.nestRemoting('folders');
Folder.nestRemoting('photos')

// Configure the other way of the relation
Photo.nestRemoting('folder')
Folder.nestRemoting('project')
Project.nestRemoting('account');

This works wonderful and lets me do things like

/api/Accounts/<account id>/projects/<project id>/folders

To get a list of Folders that belong to that Account.

And viceversa, I am able to do the other way around:

/api/Photos/folder/project/

However:

When I try to add one more nested level to the query in either direction I get an error.

I.e. if I try to do

/api/Accounts/<account id>/projects/<project id>/folders/<folder id>/photos

or if I do

/api/Photos/folder/project/account

I get the following error:

"name": "Error",
"status": 404,
"message": "Shared class \"Account\" has no method handling GET /<uuid>/projects/<uuid>/folders/<uuid>/photos?access_token=XXXXXXXXXXXXXXX",
"statusCode": 404,
"stack": "Error: Shared class \"Account\" has no method handling GET /<uuid>/projects/<uuid>/folders/<uuid>/photos?access_token=XXXXXXXXXXXXXXX\n    at restRemoteMethodNotFound (/myserver/node_modules/loopback/node_modules/strong-remoting/lib/rest-adapter.js:322:17)\n    at Layer.handle [as handle_request] (/myserver/node_modules/loopback/node_modules/express/lib/router/layer.js:95:5)\n 

(I replaced the actual id for for redability)

But this error is only triggered when I try to query more than 2 levels above the model (or 2 levels below).

So for example if I start one level below (e.g. at Project instead of Account):

/api/Projects/<project id>/folders/<folder id>/photos

This works fine.

Or the other way around:

/api/Folders/project/account

That tells me that the methods and everything is working as expected but there is a limit on the nesting levels.

So back to my original question, how can I add more levels to a nested query? is there anything I can config without modifying loopback's source code?

By the way, my datasource is a MongoDB instance (i.e. mongodb connector)

Thanks!

Upvotes: 2

Views: 1167

Answers (1)

Acapulco
Acapulco

Reputation: 3523

@Ivan Schwarz was right. It turns out that this is not a bug but an unsupported feature.

The details are here.

After poking around in the code, I found out that the root problem is that there is no relation defined between more than 2 levels of models so there is no way to traverse the dependency tree as it stands now.

The changes needed involve somehow traversing the relations of the related models and the relations of the latter, and so on, but right now that traversing is not coded.

The workaround is basically to define your own remote method by hand and call that instead.

Upvotes: 2

Related Questions