dynamodb-toolbox/dynamodb-toolbox

Document that `type: "set"` and `convertClassInstanceToMap: true` do not go well together

Closed this issue · 1 comments

UPDATE: I found out it is the marshallOptions set to convertClassInstanceToMap: true that created this bug. It might be good to add this to the documentation (that you must not set this option).

----------Original report -------------------------
I have an attribute with type set and setType: "string". When inserting a record with putTransaction and the attribute set to a value [SHA_HASH], the dynamodb record becomes {}.

I managed to reproduce the bug with the following code (for clarity I use v0.9.2):

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';

const region = process.env.DEFAULT_REGION || "eu-central-1";
const isLambda = !!process.env.LAMBDA_TASK_ROOT;

const marshallOptions = {
  convertEmptyValues: false,
  convertClassInstanceToMap: true,
};

const translateConfig = { marshallOptions }

const dynamoDBConfig = {
  region,
};

// Put here your own config
if (!isLambda) {
  Object.assign(dynamoDBConfig, {
    endpoint: "http://localhost:8000",
    credentials: {
      accessKeyId: "localhost",
      secretAccessKey: "localhost",
    }
  });
}

const DocumentClient = DynamoDBDocumentClient.from(new DynamoDBClient(dynamoDBConfig), translateConfig);

import { Table } from "dynamodb-toolbox";
const ProviderTable = new Table({
  name: 'foobar',
  partitionKey: 'PK',
  sortKey: 'SK',
  attributes: {
    PK: "string",
    SK: "string",
    Type: "string",
    Id: "string"
  },
  entityField: "Type",

  DocumentClient
});

import { Entity } from "dynamodb-toolbox";
import ProviderTable from "../table/provider.mjs";

const TestEntity = new Entity({
    table: ProviderTable,
    timestamps: false,
    typeAlias: "type",
    // Specify entity name
    name: "test",
    attributes: {
        PK: { partitionKey: true, default: () => `someValue`},
        SK: { sortKey: true, default: () => `#`},
        collection: { type: "set", setType: "string", map: "F1" } // <-- attribute that is going wrong
    },
});

async function executeAction() {
    return ProviderTable.transactWrite([
        TestEntity.putTransaction({
            collection: ["testing"] // <-- this is what I expect in the database, but instead I get `{}`
        })
    ]);
}

await executeAction();

After executing executeAction you find in the dynamodb the PK key someValue with F1 set to {} instead of the expected array.
Setting the type to list does correctly store the array, but I want set for its uniqueness.

Hey @Jimmy89
wanna open a PR for it?

I'd gladly get it merged