Created
February 1, 2017 16:31
-
-
Save kurtharriger/f1e5e600fef3515c466bcfc717cc18cb to your computer and use it in GitHub Desktop.
Shopify paging issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Shopify = require('shopify-api-node'); | |
const R = require('ramda'); | |
const shopify = new Shopify(...); | |
const list = (limit, since_id) => { | |
return ( | |
shopify.product.list({fields: 'id', since_id, limit}) | |
.then(products => { | |
if(products.length >= limit) { | |
return list(limit, R.last(products).id).then(moreProducts => ([...products, ...moreProducts])) | |
} | |
return Promise.resolve(products); | |
})); | |
} | |
const printList = (limit) => { | |
return list(limit).then(productList => { | |
console.log(`${productList.length} items returned using limit ${limit}`); | |
productList.forEach((p, i) => { | |
if(i > 0 && i % limit == 0) { console.log('---') }; | |
console.log(` ${i} - ${p.id}`); | |
}); | |
}); | |
} | |
printList(250) | |
.then(printList(5)) | |
.then(printList(8)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 items returned using limit 250 | |
0 - 266914221 | |
1 - 266914773 | |
2 - 266914649 | |
3 - 266914681 | |
4 - 266915241 | |
5 - 266915301 | |
6 - 266915337 | |
7 - 266914949 | |
8 - 266914985 | |
9 - 266915005 | |
10 - 266916257 | |
11 - 266915681 | |
12 - 266915717 | |
13 - 305105929 | |
17 items returned using limit 8 | |
0 - 266914221 | |
1 - 266914773 | |
2 - 266914649 | |
3 - 266914681 | |
4 - 266915241 | |
5 - 266915301 | |
6 - 266915337 | |
7 - 266914949 | |
--- | |
8 - 266914985 | |
9 - 266915005 | |
10 - 266915241 | |
11 - 266915301 | |
12 - 266915337 | |
13 - 266915681 | |
14 - 266915717 | |
15 - 266916257 | |
--- | |
16 - 305105929 | |
11 items returned using limit 5 | |
0 - 266914221 | |
1 - 266914773 | |
2 - 266914649 | |
3 - 266914681 | |
4 - 266915241 | |
--- | |
5 - 266915301 | |
6 - 266915337 | |
7 - 266915681 | |
8 - 266915717 | |
9 - 266916257 | |
--- | |
10 - 305105929 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment