forked from loopbackio/loopback-connector
-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Diogo Doreto edited this page Oct 16, 2015
·
3 revisions
The purpose of this fork is to enable the filtering model's relations.
Atention! This is a work-in-progress! The feature is available at query-relations
branch.
Here's a example. Suppose that a model customer
have many orders
, then you can create a query like this:
var sql = connector.buildSelect('customer', {
where: {
vip: true,
orders: {
where: {
id: 42
}
}
}
});
// which results in
ParameterizedSQL {
sql: 'SELECT DISTINCT `CUSTOMER`.`NAME`,`CUSTOMER`.`VIP`,`CUSTOMER`.`ADDRESS` FROM `CUSTOMER` INNER JOIN ( SELECT `ORDER`.`CUSTOMERID` FROM `ORDER` WHERE `ORDER`.`ID`=$1 ORDER BY `ORDER`.`ID` ) AS `ORDER` ON `CUSTOMER`.`NAME`=`ORDER`.`CUSTOMERID` WHERE `CUSTOMER`.`VIP`=$1 ORDER BY `CUSTOMER`.`NAME`',
params: [ 42, true ]
}
with some line breaks for readability:
SELECT DISTINCT `CUSTOMER`.`NAME`,
`CUSTOMER`.`VIP`,
`CUSTOMER`.`ADDRESS`
FROM `CUSTOMER`
INNER JOIN (SELECT `ORDER`.`CUSTOMERID`
FROM `ORDER`
WHERE `ORDER`.`ID` = $1
ORDER BY `ORDER`.`ID`) AS `ORDER`
ON `CUSTOMER`.`NAME` = `ORDER`.`CUSTOMERID`
WHERE `CUSTOMER`.`VIP` = $1
ORDER BY `CUSTOMER`.`NAME`
You can already query multiples relations together and even nest then.
Regarding tests, my modifications didn't break any available tests, with the exception of one single line in the function columnEscaped
, where I needed to add the table name before the column name to resolve some ambiguity problems. This broke a lot of tests, but the SQL output is still valid.