Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model include doesn't handle 'hasMany through' correctly #85

Closed
raymondfeng opened this issue Mar 13, 2014 · 8 comments
Closed

Model include doesn't handle 'hasMany through' correctly #85

raymondfeng opened this issue Mar 13, 2014 · 8 comments
Assignees
Labels

Comments

@raymondfeng
Copy link
Contributor

https://groups.google.com/forum/#!topic/loopbackjs/PWB4naSgYZg

@raymondfeng raymondfeng self-assigned this Mar 13, 2014
@raymondfeng
Copy link
Contributor Author

This should have been fixed by #86
and #89.

@bajtos
Copy link
Member

bajtos commented Mar 25, 2014

We should add an integration test to loopback for this: setup models with "hasMany through", perform a REST request with "include" field and verify that the related objects were included.

@bajtos
Copy link
Member

bajtos commented Mar 25, 2014

I started to write the unit-test and it seems those two pull requests did not fix the issue fully.

Consider the following setup:

 var product = app.model(
   'product',
   { properties: { id: 'string', name: 'string' }, dataSource: 'db' }
);
var category = app.model(
  'category',
  { properties: { id: 'string', name: 'string' }, dataSource: 'db' }
);
product.hasAndBelongsToMany(category);
category.hasAndBelongsToMany(product);
category.create({ name: 'CAT-NAME' }, function(err, cat) {
  // assume cat has id "CAT-ID"
  cat.products.create({ name: 'PROD-NAME' }, function(err, prod) {
    // assume prod has id "PROD-ID"
    // done/next
  });
});

This request works as expected:

GET /api/products?where[categoryId]=CAT-ID
{
  "id": "PROD-ID" 
  "name": "PROD-NAME"
}

These two do not work though:

GET /api/categories/CAT-ID?include=products
{
  "id": "CAT-ID"
}

GET /api/categories/findOne?where[id]=CAT-ID&include=products
{
  "id": "CAT-ID"
}

GET /api/categories?where[id]=CAT-ID&include=products
[
 { "id": "CAT-ID" }
]

@raymondfeng is that expected?

@raymondfeng
Copy link
Contributor Author

The syntax for the include option is wrong. It should be something like:

?filter[where][id]=CAT-ID&filter[include]=products

@bajtos
Copy link
Member

bajtos commented Mar 25, 2014

The syntax for the include option is wrong. It should be something like:
?filter[where][id]=CAT-ID&filter[include]=products

Thanks, find/findOne work now.

What's the correct URL for findById with include? Neither of these work:

GET /api/categories/CAT-ID?include=products
GET /api/categories/CAT-ID?filter[include]=products

@bajtos
Copy link
Member

bajtos commented Mar 25, 2014

I fixed my test for find by categoryId and realised it does not work.

GET /api/products?filter[where][categoryId]=CAT-ID
[]

@bajtos
Copy link
Member

bajtos commented Mar 25, 2014

Summary:

GET /api/categories/findOne?filter[where][id]=CAT-ID&filter[include]=products

GET /api/products?filter[where][categoryId]=CAT-ID

GET /api/categories/CAT-ID?include=products

@bajtos
Copy link
Member

bajtos commented Mar 25, 2014

At the moment, LoopBack supports these two use-cases:

  • find related models via URL scope:
    GET /api/categories/CAT-ID/products
  • include related models in the result of find/findOne:
    GET /api/categories/findOne?filter[where][id]=CAT-ID&filter[include]=products

I have opened two follow-up issues #93 and #94 to implement the other two uses cases mentioned above.

@raymondfeng I suppose we can close the issue once the new version of loopback-datasource-juggler is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants