diff --git a/README.md b/README.md index 19f3531..590eaeb 100644 --- a/README.md +++ b/README.md @@ -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!') }); diff --git a/src/dynamodb/DynamoDBManager.ts b/src/dynamodb/DynamoDBManager.ts index 57903a1..8d1bf06 100644 --- a/src/dynamodb/DynamoDBManager.ts +++ b/src/dynamodb/DynamoDBManager.ts @@ -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; diff --git a/test/dynamodb/DynamoDBManager.ts b/test/dynamodb/DynamoDBManager.ts index 2c31fe6..a8443ce 100644 --- a/test/dynamodb/DynamoDBManager.ts +++ b/test/dynamodb/DynamoDBManager.ts @@ -12,7 +12,7 @@ describe('DynamoDBManager.deletePoint', () => { TableName: 'MyTable', Key: { hashKey: { N: '44' }, - rangeKey: 'myRangeKey' + rangeKey: { S: '1234' } } } ); @@ -22,7 +22,7 @@ describe('DynamoDBManager.deletePoint', () => { const ddb = new DynamoDBManager(config); ddb.deletePoint({ - RangeKeyValue: 'myRangeKey', + RangeKeyValue: { S: '1234' }, GeoPoint: { longitude: 50, latitude: 1 @@ -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; + }); +});