Look at https://github.com/vitaly-t/pg-promise/wiki/Data-Imports there is a very detailed document on how to use it for import.
However, while this works for a demo scenario, I do not know how to apply it in my case.
When I make my web call, I get the actual JSON data and a parameter in the header that gives me the value for the next page (maybe a date or a string or a numeric value).
This example 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, it sequence(indexwill use an index that seems to increase +1. But in my case
function getNextData(nextPage) {
.....
nextPage = response.next;
resolve(data);
}
My question is how can I replace indexwith nextPagein this example, since each new Promise should use nextPagefrom the previous one.
LATER EDIT: nextPageInfo?
:
db.any('Select value from table')
.then(function(value) {
var data = value;
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 => {
console.log('Total batches:', data.total, ', Duration:', data.duration);
})
.catch(error => {
console.log(error);
})
}