来自API的node.js pg-promise和分页

时间:2017-07-05 12:16:12

标签: javascript node.js promise pg-promise

查看https://github.com/vitaly-t/pg-promise/wiki/Data-Imports,有关于如何使用它进行导入的非常详细的文档。

然而,虽然这适用于演示场景,但我不知道如何将其应用于我的案例。

当我进行网络通话时,我会在标题中获得实际的JSON数据和参数,它为下一页提供了一个值(可以是日期或字符串或数字值)。

在示例中,它说:

db.tx('massive-insert', t => {
    return t.sequence(index => {
        return getNextData(index)
            .then(data => {
                if (data) {
                    const insert = pgp.helpers.insert(data, cs);
                    return t.none(insert);
                }
            });
    });
})
    .then(data => {
        console.log('Total batches:', data.total, ', Duration:', data.duration);
    })
    .catch(error => {
        console.log(error);
    });

在这种情况下,sequence(index将使用似乎增加+1的索引。 但就我而言,

function getNextData(nextPage) {
    //get the data for nextPage
    .....
   //get the nextPage if exists for future use
   nextPage = response.next;

   resolve(data);
}

我的问题是,如何在此示例中将index替换为nextPage,因为每个新Promise都需要使用之前的nextPage

LATER EDIT:如果我想从nextPageInfo的某个值获取信息?

例如:

db.any('Select value from table')
      .then(function(value) {

var data = value; //not working

db.tx('massive-insert', t => {
    return t.sequence((index, data) => {
        return getNextData(index, data)
            .then(a => {
                if (a) {
                    const insert = pgp.helpers.insert(a.data, cs);
                    return t.none(insert).then(() => a.nextPageInfo);
                }
            })
    });
})
    .then(data => {
        // COMMIT has been executed
        console.log('Total batches:', data.total, ', Duration:', data.duration);
    })
    .catch(error => {
        // ROLLBACK has been executed
        console.log(error);
    })

}

1 个答案:

答案 0 :(得分:2)

根据这个问题,我已将文章Data Imports扩展为新的extras部分,该部分为您提供了所需的示例。从文章中复制的示例:

function getNextData(t, index, nextPageInfo) {
    // t = database transaction protocol

    // NOTE: nextPageInfo = undefined when index = 0

    return new Promise((resolve, reject) {

        /* pull the next data, according to nextPageInfo */            

        /* do reject(error) on an error, to ROLLBACK changes */

        if(/* there is still data left*/) {
            // if whateverNextDetails = undefined, the data will be inserted,
            // but the sequence will end there (as success).
            resolve({data, nextPageInfo: whateverNextDetails});
        } else {
            resolve(null);
        }   
    });
}

db.tx('massive-insert', t => {
    return t.sequence((index, data) => {
        return getNextData(t, index, data)
            .then(a => {
                if (a) {
                    const insert = pgp.helpers.insert(a.data, cs);
                    return t.none(insert).then(() => a.nextPageInfo);
                }
            })
    });
})
    .then(data => {
        // COMMIT has been executed
        console.log('Total batches:', data.total, ', Duration:', data.duration);
    })
    .catch(error => {
        // ROLLBACK has been executed
        console.log(error);
    });

请注意,由于我们将结果从getNextData链接到nextPageInfo的值,如果其值为undefined,则会执行下一次插入,但随后将结束顺序(成功)。