node.js - Getting joined data from strongloop/loopback -
how can data 2 joined tables? suppose, there 2 models called category (categoryid, categoryname) , product(productid, productname, categoryid), there way result like:(productid, productname, categoryid, categoryname)
there should relation between category model , product model. category hasmany products , each product belongsto category. model json files should like
category:
{ "name":"category", "properties":{ "categoryid": { "type":"number", "id":1 }, "categoryname":{ "type":"string" } }, "relations": { "products": { "type":"hasmany", "model":"product", "foreignkey":"categoryid" } } }
product
{ "name":"product", "properties":{ "productid: { "type":"number", "id":1 }, "productname":{ "type":"string" }, "categoryid": { "type":"number" } }, "relations": { "category": { "type":"belongsto", "model":"category", "foreignkey":"categoryid" } } }
of course these json definitions must completed corresponding options , datasource properties, know.
the relation between 2 models add endpoints loopback explorer can query products in certan category:
get /api/categorys/:id_category/products
of category product belongs
get /api/products/:id_product/category
please note that, unless specify plural option category, plural categorys. not typo.
finally, if want query product , category, use include filter
get /api/products/:id_product?filter[include]=category
hope helps.
Comments
Post a Comment