我有三个数据库表,例如employee,department和leave_details。 员工和部门使用外键department_id有关系。也是员工和leave_details使用外键user_id有关系 我需要从leave_details获取数据,其中department_id:x
这里我的代码就是那个
leave_details.json
"relations": {
"employee": {
"type": "belongsTo",
"model": "employee",
"foreignKey": "user_id"
},
和employee.json
"properties": {"department_id": {
"type": "number"
}}
"relations": {
"department": {
"type": "belongsTo",
"model": "department",
"foreignKey": "department_id"
},}
和leave_details.js
filter = {
include: [{ relation: 'employee', scope: { where: { department_id: 5} } }],
fields: ['id', 'user_id', 'requested_time', 'leave_type', 'leave_start_time', 'leave_status', 'approved_by', 'approval_time', 'leave_end_time'],
order: 'requested_time DESC',
};
你能帮帮我吗
答案 0 :(得分:1)
到目前为止,使用环回过滤器是不可能的,你需要使用数据库级查询sql / mongodb聚合等。
https://github.com/strongloop/loopback/issues/517
在不破坏环回逻辑的情况下,最方便的复杂查询是mongodb聚合查询,如果你开发一个mixin,你会得到类似的东西:
let filter = {
where:{},
include:[],
...
aggregate: [{
$match: {
period: {
$lt: period || new Date()
}
}
},
{
$sort: {
period: -1
}
},
...]
};
或使用例如此 https://github.com/BoLaMN/loopback-mongo-aggregate-mixin