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

Support gentle fallback for codebases that cannot use emitDecoratorMetadata #369

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

Comments

@eremzeit
Copy link

The decorators in TypeDORM are a nice sugar for defining the behavior of your entities, but ultimately there are going to be many codebases that are tied to ESBuild, which has no plans of supporting decorators. If you're using Vite, you can configure it to use SWC instead of ESBuild. In my case, I'm stuck with ESBuild when compiling lambda handlers in SST.

I'm currently hacking around this by manually calling functions on MetadataManager (eg. addRawAttribute), but it'd be nice if typedorm explicitly supported this. Maybe this just means mentioning this workaround in the readme. And maybe it means adding some helper functions to make this manual process a little prettier.

@eremzeit
Copy link
Author

eremzeit commented Oct 22, 2023

For example, for my project, I hacked together some sugar. Let me know if you want me to post the implementation (or want a PR).

For example, suppose you have an entity that is defined with decorators like this:

@Entity({
  name: "my_entity",
  primaryKey: {
    partitionKey: "my_entity#{{id}}",
    sortKey: "my_entity#{{id}}",
  },
})
class MyEntity {
  @AutoGenerateAttribute({
    strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY.KSUID,
  })
  id: string;

  @Attribute()
  fooAttribute: string;
}

If you didn't have decorators you could express it instead as...

class MyEntity {
  id: string;
  fooAttribute: string;
}

registerEntity({
  entity: {
    constructor: MyEntity,
    name: "my_entity",
    primaryKey: {
      partitionKey: "my_entity#{{id}}",
      sortKey: "my_entity#{{id}}",
    },
  },
  attributes: [
    Attribute("id")
      .string()
      .autoGenerate({ strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY.UUID4 }),
    Attribute("fooAttribute").string(),
  ],
});

Obviously, it's not as elegant being able to use decorators and you lose some type safety, but it's still worth it for me compared to the alternative, which is abandoning TypeDorm altogether and working directly with the AWS SDK. Also, I could probably modify this sugar to make it even more slick (I just hacked it together).

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