Skip to content

Commit dd7c467

Browse files
authored
Merge pull request #13 from jgilbert01/feat/add-sk-to-get
add sk to get method of dynamo connector
2 parents c58b836 + aaa8b66 commit dd7c467

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambda-api-utils",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "This library contains utilities for use with the Lambda-Api to create rest endpoints in BFF microservices.",
55
"keywords": [
66
"aws",

src/connectors/dynamodb.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,18 @@ class Connector {
107107
);
108108
}
109109

110-
get(id, IndexName, pk) {
110+
get(id, IndexName, pk, sk, skName) {
111111
const params = {
112112
TableName: this.tableName,
113113
IndexName,
114-
KeyConditionExpression: '#pk = :pk',
114+
KeyConditionExpression: sk ? '#pk = :pk and #sk = :sk' : '#pk = :pk',
115115
ExpressionAttributeNames: {
116116
'#pk': pk || 'pk',
117+
...(sk ? { '#sk': skName || 'sk' } : {}),
117118
},
118119
ExpressionAttributeValues: {
119120
':pk': id,
121+
...(sk ? { ':sk': sk } : {}),
120122
},
121123
ConsistentRead: !IndexName,
122124
};

test/unit/connectors/dynamodb.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,37 @@ describe('connectors/dynamodb.js', () => {
135135
}]);
136136
});
137137

138+
it('should get by id - with range', async () => {
139+
const spy = sinon.spy((_) => ({
140+
Items: [{
141+
pk: '00000000-0000-0000-0000-000000000000',
142+
sk: 'thing',
143+
name: 'thing0',
144+
timestamp: 1600051691001,
145+
}],
146+
}));
147+
mockDdb.on(QueryCommand).callsFake(spy);
148+
149+
const data = await new Connector({ debug: debug('db'), tableName: 't1' })
150+
.get('00000000-0000-0000-0000-000000000000', undefined, undefined, 'thing');
151+
152+
expect(spy).to.have.been.calledOnce;
153+
expect(spy).to.have.been.calledWith({
154+
TableName: 't1',
155+
IndexName: undefined,
156+
KeyConditionExpression: '#pk = :pk and #sk = :sk',
157+
ExpressionAttributeNames: { '#pk': 'pk', '#sk': 'sk' },
158+
ExpressionAttributeValues: { ':pk': '00000000-0000-0000-0000-000000000000', ':sk': 'thing' },
159+
ConsistentRead: true,
160+
});
161+
expect(data).to.deep.equal([{
162+
pk: '00000000-0000-0000-0000-000000000000',
163+
sk: 'thing',
164+
name: 'thing0',
165+
timestamp: 1600051691001,
166+
}]);
167+
});
168+
138169
it('should query - page 1', async () => {
139170
const spy = sinon.spy((_) => ({
140171
LastEvaluatedKey: { pk: '1', sk: 'thing' },

0 commit comments

Comments
 (0)