nestjs-build icon indicating copy to clipboard operation
nestjs-build copied to clipboard

Add UsersModule with TypeORM

Open peterfication opened this issue 3 years ago • 8 comments

Hey @techvlad,

Thanks a lot for your swc/ESBuild setup. I tried it out but couldn't get it to work with the dependency injection of TypeORM. Do you have any ideas whether this could be resolved? I checked out ESBuild and swc but had no luck finding a solution for it :/

Any hints are appreciated :)

peterfication avatar May 14 '21 08:05 peterfication

@peterfication - I think your module definition(users.module.ts) might be wrong, shouldn't it be?

@Module({ imports: [TypeOrmModule.forFeature([User])], providers: [UsersService], exports: [UsersService], })

gozmanyoni avatar Jul 06 '21 02:07 gozmanyoni

Yeah, right. But it will still fail on another level: QueryFailedError: SQLITE_ERROR: no such table: user1. (Before it had issues with UserRepository1)

peterfication avatar Jul 08 '21 12:07 peterfication

@peterfication I think this boils down to configuration issues.

the table wont exist unless you create it or make nest generate it

gozmanyoni avatar Jul 09 '21 04:07 gozmanyoni

Yes, the table needs to be created, but it shouldn't be called user1 but user. That's the problem here.

peterfication avatar Jul 09 '21 05:07 peterfication

@peterfication - yep, I haven't worked with typeORM so I don't know what the default behaviour is. doesn't seem right indeed :) you could pass in a table name to the entity attribute to go around that though. so there is a workaround at least.

gozmanyoni avatar Jul 09 '21 08:07 gozmanyoni

@peterfication - you should be able to retain names and get the table named correctly by modifying the esbuild.transpiler file:

await build({ platform: 'node', target: 'node14', keepNames: true, bundle: false, sourcemap: false, plugins: [await esbuildDecorators({ tsconfig })], entryPoints: [sourceFilePath], outfile: buildFilePath, format: 'cjs', tsconfig, })

See - https://esbuild.github.io/api/#keep-names for more info.

gozmanyoni avatar Jul 12 '21 08:07 gozmanyoni

Thanks a lot @gozmanyoni, this fixed it for ESBuild.

Also, SWC added an option called keepClassNames which will fix it for SWC.

🎉

peterfication avatar Jul 12 '21 12:07 peterfication

This resolved it for me 💪

peterfication avatar Jul 12 '21 12:07 peterfication