我正在通过pg-promise的方法映射示例:
// 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
});
Run Code Online (Sandbox Code Playgroud)
假设该Event实体与,例如Cars,具有一对多的关系,并且我想列出所有cars与之连接的实体event,那么当我想要的对象深于一层以上时,如何使用map函数?
我想要的结果可能看起来像这样:
[{ …Run Code Online (Sandbox Code Playgroud)