From f6189bf910bcaf8e3a7beda1a168c73f2f812a94 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Fri, 12 Apr 2019 21:20:06 +0200 Subject: [PATCH 1/2] Add pagination for .all methods I removed rsvp as a dependency as we only use Promise.all --- lib/contracts/contribution.js | 19 +++++---------- lib/contracts/contributor.js | 18 +++++--------- lib/contracts/proposal.js | 18 +++++--------- lib/kredits.js | 4 +-- lib/utils/pagination.js | 46 +++++++++++++++++++++++++++++++++++ package.json | 1 - 6 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 lib/utils/pagination.js diff --git a/lib/contracts/contribution.js b/lib/contracts/contribution.js index e49591a..9033d0c 100644 --- a/lib/contracts/contribution.js +++ b/lib/contracts/contribution.js @@ -1,20 +1,13 @@ -const ethers = require('ethers'); - -const ContributionSerializer = require('../serializers/contribution'); const Base = require('./base'); +const ContributionSerializer = require('../serializers/contribution'); +const paged = require('../utils/pagination'); class Contribution extends Base { - all() { + all(options = {}) { return this.functions.contributionsCount() - .then(async (count) => { - let contributions = []; - - for (let id = 1; id <= count; id++) { - const contribution = await this.getById(id) - contributions.push(contribution); - } - - return contributions; + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); }); } diff --git a/lib/contracts/contributor.js b/lib/contracts/contributor.js index d207773..75f2b2e 100644 --- a/lib/contracts/contributor.js +++ b/lib/contracts/contributor.js @@ -1,19 +1,13 @@ -const RSVP = require('rsvp'); - -const ContributorSerializer = require('../serializers/contributor'); const Base = require('./base'); +const ContributorSerializer = require('../serializers/contributor'); +const paged = require('../utils/pagination'); class Contributor extends Base { - all() { + all(options = {}) { return this.functions.contributorsCount() - .then(count => { - let contributors = []; - - for (let id = 1; id <= count; id++) { - contributors.push(this.getById(id)); - } - - return RSVP.all(contributors); + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); }); } diff --git a/lib/contracts/proposal.js b/lib/contracts/proposal.js index 3caccba..6252d84 100644 --- a/lib/contracts/proposal.js +++ b/lib/contracts/proposal.js @@ -1,19 +1,13 @@ -const RSVP = require('rsvp'); - -const ContributionSerializer = require('../serializers/contribution'); const Base = require('./base'); +const ContributionSerializer = require('../serializers/contribution'); +const paged = require('../utils/pagination'); class Proposal extends Base { - all() { + all(options = {}) { return this.functions.proposalsCount() - .then(count => { - let proposals = []; - - for (let id = 1; id <= count; id++) { - proposals.push(this.getById(id)); - } - - return RSVP.all(proposals); + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); }); } diff --git a/lib/kredits.js b/lib/kredits.js index ef47795..fc1c168 100644 --- a/lib/kredits.js +++ b/lib/kredits.js @@ -1,5 +1,4 @@ const ethers = require('ethers'); -const RSVP = require('rsvp'); const Preflight = require('./utils/preflight'); @@ -57,7 +56,8 @@ class Kredits { ); }); }); - return RSVP.all(addressPromises).then(() => { return this }); + + return Promise.all(addressPromises).then(() => { return this }); }); } diff --git a/lib/utils/pagination.js b/lib/utils/pagination.js new file mode 100644 index 0000000..02c2f97 --- /dev/null +++ b/lib/utils/pagination.js @@ -0,0 +1,46 @@ +function pageNumber(number, size, recordCount) { + let numberOfPages = Math.ceil(recordCount / size); + + number = parseInt(number) || 1; + + // Ensure page number is in range + number = number < 1 ? 1 : number; + number = number > numberOfPages ? numberOfPages : number; + + return number; +} + +function buildIds(order, number, size, recordCount) { + let offset = size * (number - 1); + + let start; + let mapFunction; + + if (order === 'asc') { + start = 1 + offset; + mapFunction = (_, i) => start + i; + } else { + start = recordCount - offset; + mapFunction = (_, i) => start - i; + } + + // Ensure size is in range + let end = offset + size; + if (end > recordCount) { + let diff = end - recordCount; + size = size - diff; + } + + return Array.from({ length: size }, mapFunction); +} + +module.exports = function paged(recordCount, options = {}) { + let { order, page } = options; + order = order || 'desc'; + page = page || {}; + + let size = parseInt(page.size) || 25; + let number = pageNumber(page.number, size, recordCount); + + return buildIds(order, number, size, recordCount); +}; diff --git a/package.json b/package.json index 50f086c..e0d9909 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,6 @@ "ethers": "^4.0.27", "ipfs-http-client": "^30.1.1", "kosmos-schemas": "github:67P/kosmos-schemas#feature/contribution_date_time", - "rsvp": "^4.8.2", "tv4": "^1.3.0" }, "keywords": [ -- 2.25.1 From 6f97c905d6eaf69e78868fe68e495bb33f961dd4 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Sat, 13 Apr 2019 13:05:47 +0200 Subject: [PATCH 2/2] Add count property and DRY --- lib/contracts/contribution.js | 13 ++++--------- lib/contracts/contributor.js | 13 ++++--------- lib/contracts/proposal.js | 13 ++++--------- lib/contracts/record.js | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 27 deletions(-) create mode 100644 lib/contracts/record.js diff --git a/lib/contracts/contribution.js b/lib/contracts/contribution.js index 9033d0c..373b983 100644 --- a/lib/contracts/contribution.js +++ b/lib/contracts/contribution.js @@ -1,14 +1,9 @@ -const Base = require('./base'); +const Record = require('./record'); const ContributionSerializer = require('../serializers/contribution'); -const paged = require('../utils/pagination'); -class Contribution extends Base { - all(options = {}) { - return this.functions.contributionsCount() - .then((count) => { - let records = paged(count, options).map((id) => this.getById(id)); - return Promise.all(records); - }); +class Contribution extends Record { + get count () { + return this.functions.contributionsCount(); } getById(id) { diff --git a/lib/contracts/contributor.js b/lib/contracts/contributor.js index 75f2b2e..8afb820 100644 --- a/lib/contracts/contributor.js +++ b/lib/contracts/contributor.js @@ -1,14 +1,9 @@ -const Base = require('./base'); +const Record = require('./record'); const ContributorSerializer = require('../serializers/contributor'); -const paged = require('../utils/pagination'); -class Contributor extends Base { - all(options = {}) { - return this.functions.contributorsCount() - .then((count) => { - let records = paged(count, options).map((id) => this.getById(id)); - return Promise.all(records); - }); +class Contributor extends Record { + get count () { + return this.functions.contributorsCount(); } getById(id) { diff --git a/lib/contracts/proposal.js b/lib/contracts/proposal.js index 6252d84..71bbacc 100644 --- a/lib/contracts/proposal.js +++ b/lib/contracts/proposal.js @@ -1,14 +1,9 @@ -const Base = require('./base'); +const Record = require('./record'); const ContributionSerializer = require('../serializers/contribution'); -const paged = require('../utils/pagination'); -class Proposal extends Base { - all(options = {}) { - return this.functions.proposalsCount() - .then((count) => { - let records = paged(count, options).map((id) => this.getById(id)); - return Promise.all(records); - }); +class Proposal extends Record { + get count () { + return this.functions.proposalsCount(); } getById(id) { diff --git a/lib/contracts/record.js b/lib/contracts/record.js new file mode 100644 index 0000000..a5d8e64 --- /dev/null +++ b/lib/contracts/record.js @@ -0,0 +1,14 @@ +const Base = require('./base'); +const paged = require('../utils/pagination'); + +class Record extends Base { + all(options = {}) { + return this.count + .then((count) => { + let records = paged(count, options).map((id) => this.getById(id)); + return Promise.all(records); + }); + } +} + +module.exports = Record -- 2.25.1