简体   繁体   English

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

[英]node.js pg-promise and pagination from API

Looking at https://github.com/vitaly-t/pg-promise/wiki/Data-Imports there's a very detailed doc on how to use it for importing. 看看https://github.com/vitaly-t/pg-promise/wiki/Data-Imports,有一个关于如何使用它进行导入的非常详细的文档。

However while that works for the demoed scenario I don't know how to apply it on my case. 然而,虽然这适用于演示场景,但我不知道如何将其应用于我的案例。

When I do my web call, I get the actual JSON data and a paramter in the header which gives me a value for the next page (could be a date or String or a number value). 当我进行网络调用时,我会在标题中获得实际的JSON数据和参数,它为下一页提供了一个值(可以是日期或字符串或数字值)。

In the example, it says: 在示例中,它说:

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);
    });

In this case, sequence(index will use index which seems to increment +1. But in my case, 在这种情况下, sequence(index将使用似乎增加+1的索引。但在我的情况下,

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

   resolve(data);
}

My question is, how can I replace index with nextPage in this example, as each new Promise needs to use the nextPage from previous one. 我的问题是,如何在这个例子中用nextPage替换index ,因为每个新的Promise需要使用前一个的nextPage

LATER EDIT: And if I want to fetch info from a certain value of nextPageInfo? LATER EDIT:如果我想从nextPageInfo的某个值获取信息?

For instance: 例如:

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);
    })

}

Following this question, I have extended article Data Imports with the new extras section, which gives you exactly the example that you need. 在这个问题之后,我已经使用新的附加部分扩展了文章Data Imports ,它为您提供了所需的示例。 The example copied from the article: 从文章中复制的示例:

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);
    });

Please note that since we are chaining the result from getNextData to the value of nextPageInfo , then if its value is undefined , it will do the next insert, but then will end the sequence (as success). 请注意,因为我们将getNextData的结果链接到nextPageInfo的值,所以如果它的值undefined ,它将执行下一次插入,但随后将结束序列(成功)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM