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

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

Closed
Jimmy89 opened this issue Feb 9, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@Jimmy89
Copy link

Jimmy89 commented Feb 9, 2024

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.

@Jimmy89 Jimmy89 changed the title Cannot insert an array into type set when using updateTransaction Document that type: "set" and convertClassInstanceToMap: true do not go well together Feb 9, 2024
@naorpeled
Copy link
Collaborator

Hey @Jimmy89
wanna open a PR for it?

I'd gladly get it merged

@naorpeled naorpeled added documentation Improvements or additions to documentation good first issue Good for newcomers labels Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants