Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 带pg promise的嵌套查询对象映射_Node.js_Pg Promise - Fatal编程技术网

Node.js 带pg promise的嵌套查询对象映射

Node.js 带pg promise的嵌套查询对象映射,node.js,pg-promise,Node.js,Pg Promise,我将介绍for方法的一个示例: 假设事件实体与例如汽车有一对多关系,我想列出连接到每个事件的所有汽车,当我想要的对象深度超过一层时,我如何使用该函数 我想要的结果可能是这样的: [{ //This is a user id: 2, first_name: "John", last_name: "Doe", events: [{ id: 4, type: 'type', cars: [{

我将介绍for方法的一个示例:

假设
事件
实体与例如
汽车
有一对多关系,我想列出连接到每个
事件
的所有
汽车
,当我想要的对象深度超过一层时,我如何使用该函数

我想要的结果可能是这样的:

[{
    //This is a user
    id: 2,
    first_name: "John",
    last_name: "Doe",
    events: [{
        id: 4,
        type: 'type',
        cars: [{
            id: 4,
            brand: 'bmw'
        }]
    }]
}]
我是这本书的作者


然后使用它:

db.task(getUsers)
    .then(users => {
        // users = an object tree of users->events->cars
    })
    .catch(error => {
        // error
    });
方法简化了将检索到的行映射到其他行的过程,因为我们将它们映射到承诺中,所以需要解决这些问题,为此我们使用方法。我们为
汽车的每个内部请求数组
,然后在顶层处理
事件的请求数组

更新

如果将树逻辑倒置,可能更易于阅读和维护:

function getUsers(t) {
    const getCars = eventId => t.any('SELECT * FROM Cars WHERE eventId = $1', eventId);

    const getEvents = userId => t.map('SELECT * FROM Events WHERE userId = $1', userId, event => {
        return getCars(event.id)
            .then(cars => {
                event.cars = cars;
                return event;
            });
    }).then(t.batch);

    return t.map('SELECT * FROM Users WHERE status = $1', ['active'], user => {
        return getEvents(user.id)
            .then(events => {
                user.events = events;
                return user;
            });
    }).then(t.batch);
}

这里还有一种更快的单查询方法:

我是这本书的作者


然后使用它:

db.task(getUsers)
    .then(users => {
        // users = an object tree of users->events->cars
    })
    .catch(error => {
        // error
    });
方法简化了将检索到的行映射到其他行的过程,因为我们将它们映射到承诺中,所以需要解决这些问题,为此我们使用方法。我们为
汽车的每个内部请求数组
,然后在顶层处理
事件的请求数组

更新

如果将树逻辑倒置,可能更易于阅读和维护:

function getUsers(t) {
    const getCars = eventId => t.any('SELECT * FROM Cars WHERE eventId = $1', eventId);

    const getEvents = userId => t.map('SELECT * FROM Events WHERE userId = $1', userId, event => {
        return getCars(event.id)
            .then(cars => {
                event.cars = cars;
                return event;
            });
    }).then(t.batch);

    return t.map('SELECT * FROM Users WHERE status = $1', ['active'], user => {
        return getEvents(user.id)
            .then(events => {
                user.events = events;
                return user;
            });
    }).then(t.batch);
}

这里还有一种更快的单查询方法: