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

Fix: Cannot add multiple attribute decorators for primary key #31

Merged
merged 8 commits into from
Jul 21, 2024

Conversation

blazejkustra
Copy link
Owner

Summary:

Code sample:

Model

import attribute from '../dist/decorators';
import Entity from '../dist/entity';
import TableManager from '../dist/table';

export class TableUser extends Entity {
  @attribute.gsi.partitionKey.string({ indexName: 'index-1' })
  @attribute.partitionKey.string()
  domain: string;

  @attribute.sortKey.string()
  @attribute.gsi.sortKey.string({ indexName: 'index-1' })
  email: string;

  constructor(data: { domain: string; email: string }) {
    super(data);
    this.domain = data.domain;
    this.email = data.email;
  }
}

export const UserdataTableManager = new TableManager(TableUser, {
  tableName: 'USERS_TABLE_NAME',
  partitionKey: 'domain',
  sortKey: 'email',
  indexes: {
    'index-1': {
      partitionKey: 'domain',
      sortKey: 'email',
    },
  },
});

const EntityManager = UserdataTableManager.entityManager();

General

async function test() {
  const entity = await EntityManager.put(
    new TableUser({
      domain: 'test',
      email: 'not_empty',
    }),
  );
  console.log(entity);
}

async function createTable() {
  const table = await UserdataTableManager.createTable();
  console.log(table);
}

createTable();
test();

GitHub linked issue:

#30

Type of change:

  • Bug fix
  • Feature implementation
  • Documentation improvement
  • Testing improvement
  • Something not listed here

Is this a breaking change?

  • YES 🚨
  • No

Other:

  • I have searched through the GitHub pull requests to ensure this PR has not already been submitted
  • I have updated the Dynamode documentation (if required)
  • I have added/updated the Dynamode test cases (if required)
  • I agree that all changes made in this pull request may be distributed and are made available in accordance with the Dynamode License.

@blazejkustra blazejkustra self-assigned this Jul 6, 2024
@blazejkustra blazejkustra marked this pull request as ready for review July 6, 2024 19:48
@blazejkustra
Copy link
Owner Author

In case you have any questions fell free to leave them here @afronski 👍

I'll add some e2e tests next week.

Copy link

@afronski afronski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One remark: as in the proposed example, the original case from #30 looks good.

However, one of the allowed scenarios does not work:

export class TableUser extends Entity {
  @attribute.partitionKey.string()
  id: string;

  // --> This throws a "role mismatch" - and it's perfectly allowed as a reprojection for GSI. <--
  // "Attribute "type" is decorated with a wrong role in "TableUser" Entity."
  @attribute.sortKey.string()
  @attribute.gsi.partitionKey.string({ indexName: 'index-1' })
  type: string;

  @attribute.gsi.sortKey.string({ indexName: 'index-1' })
  email: string;
  
  // ...
}

@blazejkustra
Copy link
Owner Author

One remark: as in the proposed example, the original case from #30 looks good.

However, one of the allowed scenarios does not work:

export class TableUser extends Entity {
  @attribute.partitionKey.string()
  id: string;

  // --> This throws a "role mismatch" - and it's perfectly allowed as a reprojection for GSI. <--
  // "Attribute "type" is decorated with a wrong role in "TableUser" Entity."
  @attribute.sortKey.string()
  @attribute.gsi.partitionKey.string({ indexName: 'index-1' })
  type: string;

  @attribute.gsi.sortKey.string({ indexName: 'index-1' })
  email: string;
  
  // ...
}

Should be fixed with 3edd954, great find! (I haven't used DynamoDB for a while so I forgot about this use case which is 100% valid)

@afronski
Copy link

afronski commented Jul 7, 2024

@blazejkustra Thanks for the update - I rechecked the case presented above, and it works as expected now.

LGTM. 🙇

@blazejkustra
Copy link
Owner Author

I'll add some e2e tests and merge this week. Thank you!

@blazejkustra
Copy link
Owner Author

Update: I found one issue connected to entity metadata caused by this change 😢 I have to rethink how getEntityAttributes is implemented but I have little time this week.

@blazejkustra
Copy link
Owner Author

Turned out it is an edge case that I can fix in a different PR. Added remaining tests and merging now. Thanks @afronski for the review!

@blazejkustra blazejkustra merged commit d83e385 into main Jul 21, 2024
2 checks passed
@blazejkustra blazejkustra deleted the fix/indexes-on-primary-key branch July 21, 2024 18:43
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

Successfully merging this pull request may close these issues.

2 participants