nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TpyeORM module affects Fastify register sequence

wuiyang opened this issue · comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When TypeORM is imported with fastify-based NestJS, the plugin register is in race condition and are not in sequence. It leads to some plugin that depends on another plugin to throw error. The affected plugins are those depend on fastify-secure-session , such as fastify-flash, fastify-csrf, fastify-passport, and more.

Expected behavior

importing TypeORM module should not affect fastify register sequence. The plugin should load according to the code sequence.

Minimal reproduction of the problem with instructions

I have setup the minimal reproduction of this issue in here: https://github.com/wuiyang/nestjs-fastify-typeorm-error
After NestJS loaded, navigate to localhost:3001, would show error.
In console, it would show Error: Flash plugin requires a valid session., which is caused by fastify-flash unable to find session property.
Although the plugin loading sequence logged in console is according to the sequence, but the internal loading sequence is different from it. fastify-secure-session would be loaded last.
Adding await app.init() and await all register still have the same error
Removing TypeORM module import would resolve the issue.

What is the motivation / use case for changing the behavior?

Use TypeORM with Fastify-based NestJS.

Environment


Nest version: 7.6.15
@nestjs/platform-fastify: 7.6.18
@nestjs/typeorm: 7.1.5
fastify-passport: 0.4.2
fastify-secure-session: 2.3.1
 
For Tooling issues:
- Node version: 12.22.1
- Platform:  Windows

Related issue:
#655
nestjs/swagger#891 (comment)

https://github.com/wuiyang/nestjs-fastify-typeorm-error/blob/master/src/main.ts#L16-L39
You're missing await for all these register() calls.

Hi, I have added await for all the register() calls, and have included init().
The error is still there, and I have mentioned it in the minimal reproduction issue part.

Hope you take some time to re-confirm if this issue.

I'm pretty sure this issue isn't related to NestJS (+ doesn't really have anything to do with the @nestjs/typeorm either)

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

After several hours of investigation, I have found that using TypeORM's createConnection() in anywhere within NestJS would cause fastify register plugin in random order.

I have tried to create a project with only fastify and TypeORM, but there's no issue. It only occurs when NestJS combined with TypeORM would affect fastify.

Edit:
I have found the solution, not sure if it is the best solution.

I created fastify instance before creating NestJS application, and register those critical and affected plugins. Not sure if it would introduce any other issue.

Here's my main.ts code that resolved the issue:

async function bootstrap(): Promise<void> {
  console.info('Starting server...');
  const fastifyInstance = fastify(/* config goes here */);

  // creating my own config service to temporary get required data
  await setupCriticalServerPlugin(fastifyInstance, new ConfigService());

  // create NestJS server
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(fastifyInstance),
  );

  // get actual config service
  const configService = app.get(ConfigService);

  // setup other non-critical fastify plugins
  await Promise.all([
    setupViewEngine(app, configService),
    setupSubscription(app, configService),
    setupOthers(app, configService),
  ]);
}