Нумерация страниц с вторичным индексом с помощью DynamoDB / Node.js - PullRequest
0 голосов
/ 30 января 2019

У меня проблема с разбиением на страницы глобального вторичного индекса в Dynamodb: /

Моя схема DynamoDB:

Resources:
  ImportsTable:
    Type: AWS::DynamoDB::Table
    Properties:
      # Generate a name based on the stage
      TableName: ${self:service}-${self:custom.stage}-imports
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
        - AttributeName: fixedKey
          AttributeType: S
        - AttributeName: timestamp
          AttributeType: N
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      StreamSpecification:
        StreamViewType: NEW_IMAGE
      ProvisionedThroughput:
        ReadCapacityUnits: ${self:custom.app.tableThroughput.imports}
        WriteCapacityUnits: ${self:custom.app.tableThroughput.imports}
      TimeToLiveSpecification:
        AttributeName: expirationTime
        Enabled: true
      GlobalSecondaryIndexes:
        - IndexName: time-index
          KeySchema:
            - AttributeName: fixedKey
              KeyType: HASH
            - AttributeName: timestamp
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
          ProvisionedThroughput:
            ReadCapacityUnits: ${self:custom.app.tableThroughput.imports}
            WriteCapacityUnits: ${self:custom.app.tableThroughput.imports}
      SSESpecification:
        SSEEnabled: true

Мои параметры запроса:

let params = {
            TableName: process.env.importsTableName,
            IndexName: 'time-index',
            KeyConditionExpression: 'fixedKey = :fk',
            Limit: 5,
            ProjectionExpression: "timeBasedId, importFileS3Key, meta, #st, #ch, success, errors, #ty, #id, email",
            ScanIndexForward: true,
            ExpressionAttributeNames: {
                "#ch": "Attributes",
                "#st": "status",
                "#ty": "type",
                "#id": "identity",
            },
            ExpressionAttributeValues: {
                ":fk" : "fixedKey",
            },
 };

Когда я запускаю это, я получаю ответ с этим:

   LastEvaluatedKey: {
    id: 88de14a0-2475-11e9-a0ee-d317558aa61b
    fixedKey: fixedKey
    timestamp: 1548842283754
}

Так что для следующего звонка я добавил это в свои параметры:

ExclusiveStartKey: {
                    id: event.queryStringParameters.id,
                }

event.queryStringParameters.id выглядит как хороший ключ

88de14a0-2475-11e9-a0ee-d317558aa61b

Но когда я его запускаю, я получаю ошибку 500 и это сообщение:

The provided starting key is invalid

Я нашел пример, который велел мне добавить в запрос весь LastEvaluatedKey, но когда я выполняю свой запрос с этим:

ExclusiveStartKey: {
                    id: event.queryStringParameters.id,
                    fixedKey: event.queryStringParameters.fixedKey,
                    timestamp: event.queryStringParameters.timestamp
                }

Когда event.queryStringParameters выглядит следующим образом:

{ fixedKey: 'fixedKey',
id: '88de14a0-2475-11e9-a0ee-d317558aa61b',
timestamp: '1548842283754' }

Я получаю эту ошибку:

The provided key element element does not math the schema

1 Ответ

0 голосов
/ 30 января 2019

Я, наконец, нашел решение, отметка времени - это число в моей схеме, поэтому мне нужно было parseInt () указать его в params:)

...