Skip to content

Commit

Permalink
Pass through other keys (eg ConditionExpression) from PutItemInput
Browse files Browse the repository at this point in the history
  • Loading branch information
robhogan committed Apr 12, 2018
1 parent 6c281b2 commit 9ffc89f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ myGeoTableManager.putPoint({
Item: { // The primary key, geohash and geojson data is filled in for you
country: { S: 'UK' }, // Specify attribute values using { type: value } objects, like the DynamoDB API.
capital: { S: 'London' }
}
},
// ... Anything else to pass through to `putItem`, eg ConditionExpression
}
}).promise()
.then(function() { console.log('Done!') });
Expand Down
3 changes: 2 additions & 1 deletion src/dynamodb/DynamoDBManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ export class DynamoDBManager {
const geohash = S2Manager.generateGeohash(putPointInput.GeoPoint);
const hashKey = S2Manager.generateHashKey(geohash, this.config.hashKeyLength);
const putItemInput: PutItemInput = {
...putPointInput.PutItemInput,
TableName: this.config.tableName,
Item: putPointInput.PutItemInput.Item || {}
}
};

putItemInput.Item[this.config.hashKeyAttributeName] = { N: hashKey.toString(10) };
putItemInput.Item[this.config.rangeKeyAttributeName] = putPointInput.RangeKeyValue;
Expand Down
47 changes: 45 additions & 2 deletions test/dynamodb/DynamoDBManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('DynamoDBManager.deletePoint', () => {
TableName: 'MyTable',
Key: {
hashKey: { N: '44' },
rangeKey: 'myRangeKey'
rangeKey: { S: '1234' }
}
}
);
Expand All @@ -22,7 +22,7 @@ describe('DynamoDBManager.deletePoint', () => {
const ddb = new DynamoDBManager(config);

ddb.deletePoint({
RangeKeyValue: 'myRangeKey',
RangeKeyValue: { S: '1234' },
GeoPoint: {
longitude: 50,
latitude: 1
Expand All @@ -32,3 +32,46 @@ describe('DynamoDBManager.deletePoint', () => {
expect(called).to.be.true;
});
});

describe('DynamoDBManager.putPoint', () => {
it('calls putItem with the correct arguments ', () => {
let called = false;
const config = new GeoDataManagerConfiguration({
putItem: (args: any) => {
called = true;
expect(args).to.deep.equal({
TableName: 'MyTable',
Item: {
geoJson: { S: "{\"type\":\"POINT\",\"coordinates\":[-0.13,51.51]}" },
geohash: { N: "5221366118452580119" },
hashKey: { N: "52" },
rangeKey: { S: "1234" },
country: { S: 'UK' },
capital: { S: 'London' }
},
ConditionExpression: "attribute_not_exists(capital)"
}
);
}
}, 'MyTable');

const ddb: any = new DynamoDBManager(config);

ddb.putPoint({
RangeKeyValue: { S: '1234' }, // Use this to ensure uniqueness of the hash/range pairs.
GeoPoint: { // An object specifying latitutde and longitude as plain numbers. Used to build the geohash, the hashkey and geojson data
latitude: 51.51,
longitude: -0.13
},
PutItemInput: { // Passed through to the underlying DynamoDB.putItem request. TableName is filled in for you.
Item: { // The primary key, geohash and geojson data is filled in for you
country: { S: 'UK' }, // Specify attribute values using { type: value } objects, like the DynamoDB API.
capital: { S: 'London' }
},
ConditionExpression: "attribute_not_exists(capital)"
}
});

expect(called).to.be.true;
});
});

0 comments on commit 9ffc89f

Please sign in to comment.