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

Attribute auto generation fails if typescript flag useDefineForClassFields is true #368

Open
eremzeit opened this issue Oct 22, 2023 · 1 comment

Comments

@eremzeit
Copy link

eremzeit commented Oct 22, 2023

Summary

As of typescript version 3.7, there's a flag called useDefineForClassFields. If your build target is es2022 or above (including esnext), it defaults to true, but otherwise defaults to false.

If you are compiling typescript with the useDefineForClassFields as true and if you use the example code from the typedorm docs to define an entity that uses attribute auto generation, you'll get an error when you run entityManager.create(entity).

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier

Background Info:

Suppose you have this code.

export class FooClass {
  id: string;
}

console.log(new FooClass())
  • If useDefineForClassFields is false, you'll get FooClass {}
  • If useDefineForClassFields is true, you'll get FooClass { id: undefined }

So suppose you have typedorm code like this

@Entity({
  name: "foo_entity",
  primaryKey: {
    partitionKey: "foo#{{id}}",
    sortKey: "foo#{{id}}",
  }
})
export class FooEntity {
  @AutoGenerateAttribute({
    strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY.UUID4,
  })
  id: string;
}

entityManager.create(new FooEntity())

If useDefineForClassFields is true, you'll get an error complaining about how the id field is empty. The root issue is that the toDynamoEntity on BaseTransformer doesn't run auto-generation on any entity keys that already have a key (even if the value is undefined).

Suggested Changes

Seems like the code in the readme should would be compatible with the defaults of future build targets.

We could either:

  1. modify the logic in BaseTransformer.toDynamoEntity to auto generate values for keys that don't have undefined values, rather than only for object keys that aren't defined at all. The downside is that this would be a breaking change for anyone who is intentionally setting an attribute as undefined to prevent it from being auto-generated. I can't imagine someone doing this, but maybe someone has more context here.

  2. Modify the readme to show how to use the declare keyword to prevent the undefined value being set for that key.

For example, if you add the declare...

@Entity({
  name: "foo_entity",
  primaryKey: {
    partitionKey: "foo#{{id}}",
    sortKey: "foo#{{id}}",
  }
})
export class FooEntity {
  @AutoGenerateAttribute({
    strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY.UUID4,
  })
  declare id: string;
}

entityManager.create(new FooEntity())

Now you're telling typescript not to assign undefined to that key. So the attribute auto-generation in entityManager.create(new FooEntity()) call would work as expected.

See also

#338

@hllvd
Copy link

hllvd commented Nov 28, 2023

Ty, you just save my day

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

2 participants