Skip to content

Commit

Permalink
Refactor _getMilestoneSnapshotByTimestamp
Browse files Browse the repository at this point in the history
This change refactors the `_getMilestoneSnapshotByTimestamp` method
according to code review comments.
  • Loading branch information
Alec Gibson committed Feb 7, 2019
1 parent 46ef109 commit cee561a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/mongo-milestone-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,27 @@ class MongoMilestoneDB extends MilestoneDB {
return Promise.reject(new InvalidTimestampError());
}

const nullSortOrder = isAfterTimestamp ? -1 : 1;
const limit = 1;
let query;
let sortOrder;

if (timestamp === null) {
query = {d: id};
sortOrder = isAfterTimestamp ? -1 : 1;
} else {
const comparator = isAfterTimestamp ? {$gte: timestamp} : {$lte: timestamp};
query = {
d: id,
'm.mtime': comparator,
};
sortOrder = isAfterTimestamp ? 1 : -1;
}

const query = {d: id};
const options = {
limit: 1,
sort: {'m.mtime': nullSortOrder},
limit: limit,
sort: {'m.mtime': sortOrder},
};

if (timestamp !== null) {
const comparator = isAfterTimestamp ? {$gte: timestamp} : {$lte: timestamp};
query['m.mtime'] = comparator;
options.sort['m.mtime'] = -nullSortOrder;
}

return this._getMilestoneSnapshotByQuery(collectionName, query, options);
}

Expand Down

0 comments on commit cee561a

Please sign in to comment.