Skip to content

Commit 44440f4

Browse files
committed
Extract common query and scan methods into a base object
1 parent 78041c5 commit 44440f4

File tree

3 files changed

+91
-159
lines changed

3 files changed

+91
-159
lines changed

lib/query-base.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
5+
module.exports = {
6+
limit(num) {
7+
if (num <= 0) {
8+
throw new Error('Limit must be greater than 0');
9+
}
10+
11+
this.request.Limit = num;
12+
13+
return this;
14+
},
15+
16+
filterExpression(expression) {
17+
this.request.FilterExpression = expression;
18+
19+
return this;
20+
},
21+
22+
expressionAttributeValues(data) {
23+
this.request.ExpressionAttributeValues = data;
24+
25+
return this;
26+
},
27+
28+
expressionAttributeNames(data) {
29+
this.request.ExpressionAttributeNames = data;
30+
31+
return this;
32+
},
33+
34+
projectionExpression(data) {
35+
this.request.ProjectionExpression = data;
36+
37+
return this;
38+
},
39+
40+
startKey(hashKey, rangeKey) {
41+
this.request.ExclusiveStartKey = this.serializer.buildKey(hashKey, rangeKey, this.table.schema);
42+
43+
return this;
44+
},
45+
46+
attributes(attrs) {
47+
if (!_.isArray(attrs)) {
48+
attrs = [attrs];
49+
}
50+
51+
const expressionAttributeNames = _.reduce(attrs, (result, attr) => {
52+
const path = `#${attr}`;
53+
result[path] = attr;
54+
55+
return result;
56+
}, {});
57+
58+
this.request.ProjectionExpression = _.keys(expressionAttributeNames).join(',');
59+
this.request.ExpressionAttributeNames = _.merge({}, expressionAttributeNames, this.request.ExpressionAttributeNames);
60+
61+
return this;
62+
},
63+
64+
select(value) {
65+
this.request.Select = value;
66+
67+
return this;
68+
},
69+
70+
returnConsumedCapacity(value) {
71+
if (_.isUndefined(value)) {
72+
value = 'TOTAL';
73+
}
74+
75+
this.request.ReturnConsumedCapacity = value;
76+
77+
return this;
78+
},
79+
80+
loadAll() {
81+
this.options.loadAll = true;
82+
83+
return this;
84+
},
85+
};

lib/query.js

+3-79
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const _ = require('lodash');
44
const expressions = require('./expressions');
5+
const queryBase = require('./query-base');
56
const utils = require('./utils');
67

78
const internals = {};
@@ -65,39 +66,8 @@ const Query = module.exports = function (hashKey, table, serializer) {
6566
this.request = {};
6667
};
6768

68-
Query.prototype.limit = function (num) {
69-
if (num <= 0) {
70-
throw new Error('Limit must be greater than 0');
71-
}
72-
73-
this.request.Limit = num;
74-
75-
return this;
76-
};
77-
78-
Query.prototype.filterExpression = function (expression) {
79-
this.request.FilterExpression = expression;
80-
81-
return this;
82-
};
83-
84-
Query.prototype.expressionAttributeValues = function (data) {
85-
this.request.ExpressionAttributeValues = data;
86-
87-
return this;
88-
};
89-
90-
Query.prototype.expressionAttributeNames = function (data) {
91-
this.request.ExpressionAttributeNames = data;
92-
93-
return this;
94-
};
95-
96-
Query.prototype.projectionExpression = function (data) {
97-
this.request.ProjectionExpression = data;
98-
99-
return this;
100-
};
69+
Query.prototype = Object.create(queryBase);
70+
Query.prototype.constructor = Query;
10171

10272
Query.prototype.usingIndex = function (name) {
10373
this.request.IndexName = name;
@@ -152,30 +122,6 @@ Query.prototype.addFilterCondition = function (condition) {
152122
return this;
153123
};
154124

155-
Query.prototype.startKey = function (hashKey, rangeKey) {
156-
this.request.ExclusiveStartKey = this.serializer.buildKey(hashKey, rangeKey, this.table.schema);
157-
158-
return this;
159-
};
160-
161-
Query.prototype.attributes = function (attrs) {
162-
if (!_.isArray(attrs)) {
163-
attrs = [attrs];
164-
}
165-
166-
const expressionAttributeNames = _.reduce(attrs, (result, attr) => {
167-
const path = `#${attr}`;
168-
result[path] = attr;
169-
170-
return result;
171-
}, {});
172-
173-
this.request.ProjectionExpression = _.keys(expressionAttributeNames).join(',');
174-
this.request.ExpressionAttributeNames = _.merge({}, expressionAttributeNames, this.request.ExpressionAttributeNames);
175-
176-
return this;
177-
};
178-
179125
Query.prototype.ascending = function () {
180126
this.request.ScanIndexForward = true;
181127

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

191-
Query.prototype.select = function (value) {
192-
this.request.Select = value;
193-
194-
return this;
195-
};
196-
197-
Query.prototype.returnConsumedCapacity = function (value) {
198-
if (_.isUndefined(value)) {
199-
value = 'TOTAL';
200-
}
201-
202-
this.request.ReturnConsumedCapacity = value;
203-
204-
return this;
205-
};
206-
207-
Query.prototype.loadAll = function () {
208-
this.options.loadAll = true;
209-
210-
return this;
211-
};
212-
213137
Query.prototype.where = function (keyName) {
214138
return internals.keyCondition(keyName, this.table.schema, this);
215139
};

lib/scan.js

+3-80
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const _ = require('lodash');
44
const expressions = require('./expressions');
5+
const queryBase = require('./query-base');
56
const utils = require('./utils');
67

78
const internals = {};
@@ -41,15 +42,8 @@ const Scan = module.exports = function (table, serializer) {
4142
this.request = {};
4243
};
4344

44-
Scan.prototype.limit = function (num) {
45-
if (num <= 0) {
46-
throw new Error('Limit must be greater than 0');
47-
}
48-
49-
this.request.Limit = num;
50-
51-
return this;
52-
};
45+
Scan.prototype = Object.create(queryBase);
46+
Scan.prototype.constructor = Scan;
5347

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

75-
Scan.prototype.startKey = function (hashKey, rangeKey) {
76-
this.request.ExclusiveStartKey = this.serializer.buildKey(hashKey, rangeKey, this.table.schema);
77-
78-
return this;
79-
};
80-
81-
Scan.prototype.attributes = function (attrs) {
82-
if (!_.isArray(attrs)) {
83-
attrs = [attrs];
84-
}
85-
86-
const expressionAttributeNames = _.reduce(attrs, (result, attr) => {
87-
const path = `#${attr}`;
88-
result[path] = attr;
89-
90-
return result;
91-
}, {});
92-
93-
this.request.ProjectionExpression = _.keys(expressionAttributeNames).join(',');
94-
this.request.ExpressionAttributeNames = _.merge({}, expressionAttributeNames, this.request.ExpressionAttributeNames);
95-
96-
return this;
97-
};
98-
99-
Scan.prototype.select = function (value) {
100-
this.request.Select = value;
101-
102-
return this;
103-
};
104-
105-
Scan.prototype.returnConsumedCapacity = function (value) {
106-
if (_.isUndefined(value)) {
107-
value = 'TOTAL';
108-
}
109-
110-
this.request.ReturnConsumedCapacity = value;
111-
112-
return this;
113-
};
114-
11569
Scan.prototype.segments = function (segment, totalSegments) {
11670
this.request.Segment = segment;
11771
this.request.TotalSegments = totalSegments;
@@ -124,31 +78,6 @@ Scan.prototype.where = function (keyName) {
12478
return internals.keyCondition(keyName, this.table.schema, this);
12579
};
12680

127-
128-
Scan.prototype.filterExpression = function (expression) {
129-
this.request.FilterExpression = expression;
130-
131-
return this;
132-
};
133-
134-
Scan.prototype.expressionAttributeValues = function (data) {
135-
this.request.ExpressionAttributeValues = data;
136-
137-
return this;
138-
};
139-
140-
Scan.prototype.expressionAttributeNames = function (data) {
141-
this.request.ExpressionAttributeNames = data;
142-
143-
return this;
144-
};
145-
146-
Scan.prototype.projectionExpression = function (data) {
147-
this.request.ProjectionExpression = data;
148-
149-
return this;
150-
};
151-
15281
Scan.prototype.exec = function (callback) {
15382
const self = this;
15483

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

162-
Scan.prototype.loadAll = function () {
163-
this.options.loadAll = true;
164-
165-
return this;
166-
};
167-
16891
Scan.prototype.buildRequest = function () {
16992
return _.merge({}, this.request, { TableName: this.table.tableName() });
17093
};

0 commit comments

Comments
 (0)