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(prisma): resolve several issues with prisma generate for ESM #2741

Merged
merged 3 commits into from
Jul 8, 2024

Conversation

vangie
Copy link
Contributor

@vangie vangie commented Jul 2, 2024

  1. Correct the generated 'lib/esm/index.js' file's path.
  2. Workaround for 'ReferenceError: Cannot access 'xxx' before initialization' caused by esm circular ref.
../tsed/packages/orm/prisma/lib/esm/.schema/models/MessageModel.js:129
    __metadata("design:type", ConversationModel)
                              ^

ReferenceError: Cannot access 'ConversationModel' before initialization
    at ../tsed/packages/orm/prisma/lib/esm/.schema/models/MessageModel.js:129:31
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.11.1

Information

Type Breaking change
Feature/Fix/Doc/Chore Yes/No

Todos

  • Tests
  • Coverage
  • Example
  • Documentation

@@ -15,7 +16,9 @@ function createDecorator(name: string, args: string[]): DecoratorStructure {

export function transformFieldToDecorators(field: DmmfField, ctx: TransformContext): DecoratorStructure[] {
const hasCircularRef = isCircularRef(field.model.name, field.type, ctx);

if (isEsm() && hasCircularRef) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

but in this case, a lot of decorators won't be generated...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know howto fix following issue in esm , any sugguest?

../tsed/packages/orm/prisma/lib/esm/.schema/models/MessageModel.js:129
    __metadata("design:type", ConversationModel)
                              ^

ReferenceError: Cannot access 'ConversationModel' before initialization
    at ../tsed/packages/orm/prisma/lib/esm/.schema/models/MessageModel.js:129:31
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.11.1

Copy link
Collaborator

Choose a reason for hiding this comment

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

we should wrap the ConversationModel into an arrow function instead adding it directly in the concerned decorator

Copy link
Collaborator

Choose a reason for hiding this comment

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

Eg: Property(() => ConversationModel)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The generate MessageModel.js is already in the form of a function.

...
__decorate([
    Property(() => ConversationModel),
    Required(),
    __metadata("design:type", ConversationModel)
], MessageModel.prototype, "conversation", void 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not very familiar with the details here, so I wonder if you could help implement it. I can help you verify it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

sure =)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fixed :) @vangie

Copy link
Contributor Author

@vangie vangie Jul 8, 2024

Choose a reason for hiding this comment

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

I have tried in my project,everything is OK。@Romakita

Copy link
Collaborator

Choose a reason for hiding this comment

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

perfect go merge :D

@vangie vangie force-pushed the vangie/fix-prisma-generate-esm branch from 0320aec to cc7dc01 Compare July 2, 2024 16:40
1. Correct the generated 'lib/esm/index.js' file's path.
2. Workaround for 'ReferenceError: Cannot access 'xxx' before initialization' caused by esm circular ref.
@Romakita Romakita force-pushed the vangie/fix-prisma-generate-esm branch from cc7dc01 to 9ac425d Compare July 6, 2024 06:57
@Romakita Romakita force-pushed the vangie/fix-prisma-generate-esm branch from 9ac425d to 22e5e0d Compare July 6, 2024 07:02
@Romakita Romakita merged commit 3302883 into tsedio:production Jul 8, 2024
15 checks passed
@Romakita
Copy link
Collaborator

Romakita commented Jul 8, 2024

🎉 This PR is included in version 7.75.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants