From what I understand I am able to use a Global Secondary Index in order to sort results of an attribute, although I'm having some trouble implementing this.
Essentially, the table uses the imdb of films as the Hash key, and the User ID as a Sort key. I'm attempting to get a collection of all the films a user has rated, sorted by how recently they created the rating. My belief was that by using a GSI I could bypass the requirement of needing to specify the imdb ID.
I have a table:
RatingsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: RatingsTable
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: imdb_title_id
AttributeType: N
- AttributeName: UID
AttributeType: S
- AttributeName: createdAt
AttributeType: N
KeySchema:
- AttributeName: imdb_title_id
KeyType: HASH
- AttributeName: UID
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: createdAt
KeySchema:
- AttributeName: UID
KeyType: HASH
- AttributeName: createdAt
KeyType: RANGE
Projection:
ProjectionType: ALL
I wish to be able to sort on an additional field "createdAt", I have logged the parameters for the Dynamo query and it is a follows:
{
TableName: 'RatingsTable',
ProjectionExpression: 'imdb_title_id, createdAt, rating, ratingPercentile',
KeyConditionExpression: 'UID = :UID',
ExpressionAttributeValues: { ':UID': { S: 'h4XJj3YRxZiF7TDcGkxAhc' } },
IndexName: 'createdAt',
ScanIndexForward: false
}
Yet I get this error:
Error: Unsupported type passed: createdAt
at convertToNative (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:35:31)
at eval (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:79:143)
at Array.reduce ()
at convertMap (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:76:32)
at convertToNative (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:27:32)
at unmarshall (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/unmarshall.js:8:77)
at getRecentRatings (webpack-internal:///./aws/handlers/getFilms.ts:60:151)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async getFilms (webpack-internal:///./aws/handlers/getFilms.ts:35:13)
at async runRequest (webpack-internal:///./node_modules/@middy/core/index.js:86:26)
2022-03-18T19:38:20.150Z 1e34517b-5fc5-4a98-9bab-dc6094734aac ERROR Error: Unsupported type passed: createdAt at convertToNative (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:35:31) at eval (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:79:143) at Array.reduce () at convertMap (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:76:32) at convertToNative (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/convertToNative.js:27:32) at unmarshall (webpack-internal:///./node_modules/@aws-sdk/util-dynamodb/dist-es/unmarshall.js:8:77) at getRecentRatings (webpack-internal:///./aws/handlers/getFilms.ts:60:151) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async getFilms (webpack-internal:///./aws/handlers/getFilms.ts:35:13) at async runRequest (webpack-internal:///./node_modules/@middy/core/index.js:86:26)
I'm not sure I understand what's causing this, I have used other indexes similarly without any issues. I'm still getting to grips with Dynamo so any help would be greatly appreciated
:
Here's a sample of the data
