【发布时间】:2017-03-09 11:17:59
【问题描述】:
我正在通过pg-promise 中的方法map 的示例:
// Build a list of active users, each with the list of user events:
db.task(t => {
return t.map('SELECT id FROM Users WHERE status = $1', ['active'], user => {
return t.any('SELECT * FROM Events WHERE userId = $1', user.id)
.then(events=> {
user.events = events;
return user;
});
}).then(t.batch);
})
.then(data => {
// success
})
.catch(error => {
// error
});
假设Event 实体与例如具有一对多关系。 Cars,我想列出所有连接到每个event 的cars,当我想要的对象超过一层深度时,如何使用map 函数?
我想要的结果可能是这样的:
[{
//This is a user
id: 2,
first_name: "John",
last_name: "Doe",
events: [{
id: 4,
type: 'type',
cars: [{
id: 4,
brand: 'bmw'
}]
}]
}]
【问题讨论】:
标签: node.js pg-promise