Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to query anything from DynamoDB #348

Open
KasimAhmic opened this issue Jun 14, 2023 · 0 comments
Open

Unable to query anything from DynamoDB #348

KasimAhmic opened this issue Jun 14, 2023 · 0 comments

Comments

@KasimAhmic
Copy link

KasimAhmic commented Jun 14, 2023

I've set up TypedORM with NestJS and am able to create entities in DynamoDB but when I go to query them for any operation, (find, update, delete) I always get back a The provided key element does not match the schema error.

Stack trace:

[Nest] 63514  - 06/14/2023, 12:35:37 AM   ERROR [ExceptionsHandler] The provided key element does not match the schema
ValidationException: The provided key element does not match the schema
    at throwDefaultError (/Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:8:22)
    at /Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:18:39
    at de_UpdateItemCommandError (/Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:2511:20)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at /Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
    at /Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/lib-dynamodb/dist-cjs/baseCommand/DynamoDBDocumentClientCommand.js:26:34
    at /Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/middleware-signing/dist-cjs/awsAuthMiddleware.js:14:20
    at /Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/middleware-retry/dist-cjs/retryMiddleware.js:27:46
    at /Users/kasim/Documents/Xcira/app-services/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:7:26

TypedORM debug output for find:

  typedorm:transform:log cf65f482-28b2-4905-af1d-e69d26ef8a0d GET configuration Before: 
Primary key:  
{
  "id": "75eb00d2-f044-4a6d-b3f5-a58f46439f36"
}
 +5m
  typedorm:transform:log cf65f482-28b2-4905-af1d-e69d26ef8a0d GET configuration After: 
Body:  
{
  "TableName": "app-services",
  "Key": {
    "PK": "CONFIGURATION#75eb00d2-f044-4a6d-b3f5-a58f46439f36",
    "SK": "CONFIGURATION#75eb00d2-f044-4a6d-b3f5-a58f46439f36"
  }
}

TypedORM debug output for updates:

  typedorm:transform:log 74f5fdfe-8fc6-482e-bd11-c63869ca7b28 UPDATE configuration Before: 
Primary key:  
{
  "id": "75eb00d2-f044-4a6d-b3f5-a58f46439f36"
}
 
Body:  
{
  "host": "google.com",
  "configuration": {}
}
 
Options:  
{}
 +0ms
  typedorm:transform:log 74f5fdfe-8fc6-482e-bd11-c63869ca7b28 UPDATE configuration After: 
Body:  
{
  "TableName": "app-services",
  "Key": {
    "PK": "CONFIGURATION#75eb00d2-f044-4a6d-b3f5-a58f46439f36",
    "SK": "CONFIGURATION#75eb00d2-f044-4a6d-b3f5-a58f46439f36"
  },
  "ReturnValues": "ALL_NEW",
  "UpdateExpression": "SET #UE_host = :UE_host, #UE_configuration = :UE_configuration, #UE_updatedAt = :UE_updatedAt",
  "ExpressionAttributeNames": {
    "#UE_host": "host",
    "#UE_configuration": "configuration",
    "#UE_updatedAt": "updatedAt"
  },
  "ExpressionAttributeValues": {
    ":UE_host": "google.com",
    ":UE_configuration": {},
    ":UE_updatedAt": 1686717338
  }
}
 +2ms

Table:

       new Table({
          name: 'app-services',
          partitionKey: 'PK',
          sortKey: 'SK',
          indexes: {
            GSI1: {
              partitionKey: 'GSI1PK',
              sortKey: 'GSI1SK',
              type: INDEX_TYPE.GSI,
            },
          },
        }),

Entity:

@Entity({
  name: 'configuration',
  primaryKey: {
    partitionKey: 'CONFIGURATION#{{id}}',
    sortKey: 'CONFIGURATION#{{id}}',
  },
  indexes: {
    GSI1: {
      partitionKey: 'CONFIGURATION#{{id}}',
      sortKey: 'CONFIGURATION#{{id}}',
      type: INDEX_TYPE.GSI,
    },
  },
})
export class ConfigurationRecord {
  @AutoGenerateAttribute({ strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY.UUID4 })
  id: string;

  @Attribute()
  host: string;

  @Attribute()
  configuration: Record<string, unknown>;

  @AutoGenerateAttribute({ strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY.EPOCH_DATE, autoUpdate: true })
  updatedAt: number;
}

Find, Update, and Delete code:

  async findOne(id: string): Promise<ConfigurationRecord> {
    const result = await this.connection.entityManager.findOne(ConfigurationRecord, { id });

    return result;
  }

  async update(
    id: string,
    updateConfigurationDto: UpdateConfigurationRecordDto,
  ): Promise<ConfigurationRecord> {
    const record = new ConfigurationRecord();

    record.host = updateConfigurationDto.host;
    record.configuration = updateConfigurationDto.configuration;

    const configurationRecord = await this.connection.entityManager.update<ConfigurationRecord>(
      ConfigurationRecord,
      {
        id: id,
      },
      record,
    );

    return configurationRecord;
  }

  async remove(id: string): Promise<boolean> {
    const { success } = await this.connection.entityManager.delete<ConfigurationRecord>(ConfigurationRecord, {
      id,
    });

    return success;
  }

Curiously the delete operation also doesn't seem to be transforming the payload:

  typedorm:transform:log beb9a8ca-b19a-4494-9ed4-9d26e25b9db0 DELETE configuration Before: 
Primary key:  
{
  "id": "75eb00d2-f044-4a6d-b3f5-a58f46439f36"
}
 +2m
  typedorm:transform:log beb9a8ca-b19a-4494-9ed4-9d26e25b9db0 DELETE configuration After: 
Primary key:  
{
  "id": "75eb00d2-f044-4a6d-b3f5-a58f46439f36"
}```
@KasimAhmic KasimAhmic changed the title Unable to query anything from DyanamoDB Unable to query anything from DynamoDB Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant