Skip to content

Instantly share code, notes, and snippets.

@ChristianRich
Last active August 30, 2018 05:58
Show Gist options
  • Save ChristianRich/3f20b38331645fa68dd246c222619ee8 to your computer and use it in GitHub Desktop.
Save ChristianRich/3f20b38331645fa68dd246c222619ee8 to your computer and use it in GitHub Desktop.
Query DynamoDB for items within a given date time range using a global secondary index and a range key
#!/bin/bash
# You may want to remove the `--endpoint-url http://localhost:8000` line for actual AWS deployment
aws dynamodb create-table \
--endpoint-url http://localhost:8000 \
--table-name exampleTableName \
--attribute-definitions \
AttributeName=id,AttributeType=S \
AttributeName=status,AttributeType=S \
AttributeName=createdDateTime,AttributeType=S \
--key-schema \
AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 \
--global-secondary-indexes \
IndexName=createdDateTime-index,KeySchema=["{AttributeName=status,KeyType=HASH},{AttributeName=createdDateTime,KeyType=RANGE}"],Projection="{ProjectionType=ALL}",ProvisionedThroughput="{ReadCapacityUnits=10,WriteCapacityUnits=10}"
const { Items } = await this.dynamoDb.query({
TableName: 'exampleTableName',
IndexName: 'createdDateTime-index',
KeyConditionExpression: '#createdDateTime BETWEEN :fromDateTime AND :toDateTime AND #status = :status',
ExpressionAttributeNames: {
'#status': 'status',
'#createdDateTime': 'createdDateTime',
},
ExpressionAttributeValues: {
':fromDateTime': '2017-02-20T01:58:49.710Z',
':toDateTime': new Date().toISOString(),
':status': 'SUCCESS',
},
}).promise();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment