Skip to content

Commit

Permalink
Extract common query and scan methods into a base object
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhowie committed Apr 21, 2018
1 parent 78041c5 commit 44440f4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 159 deletions.
85 changes: 85 additions & 0 deletions lib/query-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

const _ = require('lodash');

module.exports = {
limit(num) {
if (num <= 0) {
throw new Error('Limit must be greater than 0');
}

this.request.Limit = num;

return this;
},

filterExpression(expression) {
this.request.FilterExpression = expression;

return this;
},

expressionAttributeValues(data) {
this.request.ExpressionAttributeValues = data;

return this;
},

expressionAttributeNames(data) {
this.request.ExpressionAttributeNames = data;

return this;
},

projectionExpression(data) {
this.request.ProjectionExpression = data;

return this;
},

startKey(hashKey, rangeKey) {
this.request.ExclusiveStartKey = this.serializer.buildKey(hashKey, rangeKey, this.table.schema);

return this;
},

attributes(attrs) {
if (!_.isArray(attrs)) {
attrs = [attrs];
}

const expressionAttributeNames = _.reduce(attrs, (result, attr) => {
const path = `#${attr}`;
result[path] = attr;

return result;
}, {});

this.request.ProjectionExpression = _.keys(expressionAttributeNames).join(',');
this.request.ExpressionAttributeNames = _.merge({}, expressionAttributeNames, this.request.ExpressionAttributeNames);

return this;
},

select(value) {
this.request.Select = value;

return this;
},

returnConsumedCapacity(value) {
if (_.isUndefined(value)) {
value = 'TOTAL';
}

this.request.ReturnConsumedCapacity = value;

return this;
},

loadAll() {
this.options.loadAll = true;

return this;
},
};
82 changes: 3 additions & 79 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const _ = require('lodash');
const expressions = require('./expressions');
const queryBase = require('./query-base');
const utils = require('./utils');

const internals = {};
Expand Down Expand Up @@ -65,39 +66,8 @@ const Query = module.exports = function (hashKey, table, serializer) {
this.request = {};
};

Query.prototype.limit = function (num) {
if (num <= 0) {
throw new Error('Limit must be greater than 0');
}

this.request.Limit = num;

return this;
};

Query.prototype.filterExpression = function (expression) {
this.request.FilterExpression = expression;

return this;
};

Query.prototype.expressionAttributeValues = function (data) {
this.request.ExpressionAttributeValues = data;

return this;
};

Query.prototype.expressionAttributeNames = function (data) {
this.request.ExpressionAttributeNames = data;

return this;
};

Query.prototype.projectionExpression = function (data) {
this.request.ProjectionExpression = data;

return this;
};
Query.prototype = Object.create(queryBase);
Query.prototype.constructor = Query;

Query.prototype.usingIndex = function (name) {
this.request.IndexName = name;
Expand Down Expand Up @@ -152,30 +122,6 @@ Query.prototype.addFilterCondition = function (condition) {
return this;
};

Query.prototype.startKey = function (hashKey, rangeKey) {
this.request.ExclusiveStartKey = this.serializer.buildKey(hashKey, rangeKey, this.table.schema);

return this;
};

Query.prototype.attributes = function (attrs) {
if (!_.isArray(attrs)) {
attrs = [attrs];
}

const expressionAttributeNames = _.reduce(attrs, (result, attr) => {
const path = `#${attr}`;
result[path] = attr;

return result;
}, {});

this.request.ProjectionExpression = _.keys(expressionAttributeNames).join(',');
this.request.ExpressionAttributeNames = _.merge({}, expressionAttributeNames, this.request.ExpressionAttributeNames);

return this;
};

Query.prototype.ascending = function () {
this.request.ScanIndexForward = true;

Expand All @@ -188,28 +134,6 @@ Query.prototype.descending = function () {
return this;
};

Query.prototype.select = function (value) {
this.request.Select = value;

return this;
};

Query.prototype.returnConsumedCapacity = function (value) {
if (_.isUndefined(value)) {
value = 'TOTAL';
}

this.request.ReturnConsumedCapacity = value;

return this;
};

Query.prototype.loadAll = function () {
this.options.loadAll = true;

return this;
};

Query.prototype.where = function (keyName) {
return internals.keyCondition(keyName, this.table.schema, this);
};
Expand Down
83 changes: 3 additions & 80 deletions lib/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const _ = require('lodash');
const expressions = require('./expressions');
const queryBase = require('./query-base');
const utils = require('./utils');

const internals = {};
Expand Down Expand Up @@ -41,15 +42,8 @@ const Scan = module.exports = function (table, serializer) {
this.request = {};
};

Scan.prototype.limit = function (num) {
if (num <= 0) {
throw new Error('Limit must be greater than 0');
}

this.request.Limit = num;

return this;
};
Scan.prototype = Object.create(queryBase);
Scan.prototype.constructor = Scan;

Scan.prototype.addFilterCondition = function (condition) {
const expressionAttributeNames = _.merge({}, condition.attributeNames, this.request.ExpressionAttributeNames);
Expand All @@ -72,46 +66,6 @@ Scan.prototype.addFilterCondition = function (condition) {
return this;
};

Scan.prototype.startKey = function (hashKey, rangeKey) {
this.request.ExclusiveStartKey = this.serializer.buildKey(hashKey, rangeKey, this.table.schema);

return this;
};

Scan.prototype.attributes = function (attrs) {
if (!_.isArray(attrs)) {
attrs = [attrs];
}

const expressionAttributeNames = _.reduce(attrs, (result, attr) => {
const path = `#${attr}`;
result[path] = attr;

return result;
}, {});

this.request.ProjectionExpression = _.keys(expressionAttributeNames).join(',');
this.request.ExpressionAttributeNames = _.merge({}, expressionAttributeNames, this.request.ExpressionAttributeNames);

return this;
};

Scan.prototype.select = function (value) {
this.request.Select = value;

return this;
};

Scan.prototype.returnConsumedCapacity = function (value) {
if (_.isUndefined(value)) {
value = 'TOTAL';
}

this.request.ReturnConsumedCapacity = value;

return this;
};

Scan.prototype.segments = function (segment, totalSegments) {
this.request.Segment = segment;
this.request.TotalSegments = totalSegments;
Expand All @@ -124,31 +78,6 @@ Scan.prototype.where = function (keyName) {
return internals.keyCondition(keyName, this.table.schema, this);
};


Scan.prototype.filterExpression = function (expression) {
this.request.FilterExpression = expression;

return this;
};

Scan.prototype.expressionAttributeValues = function (data) {
this.request.ExpressionAttributeValues = data;

return this;
};

Scan.prototype.expressionAttributeNames = function (data) {
this.request.ExpressionAttributeNames = data;

return this;
};

Scan.prototype.projectionExpression = function (data) {
this.request.ProjectionExpression = data;

return this;
};

Scan.prototype.exec = function (callback) {
const self = this;

Expand All @@ -159,12 +88,6 @@ Scan.prototype.exec = function (callback) {
return utils.paginatedRequest(self, runScan, callback);
};

Scan.prototype.loadAll = function () {
this.options.loadAll = true;

return this;
};

Scan.prototype.buildRequest = function () {
return _.merge({}, this.request, { TableName: this.table.tableName() });
};

0 comments on commit 44440f4

Please sign in to comment.