Nest: How to use Nest.js & TypeORM with Webpack HMR ?

Created on 18 May 2018  路  8Comments  路  Source: nestjs/nest

I'm submitting a...


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

Current behavior

if (module['hot']) {
  module['hot'].accept()
  module['hot'].dispose(() => {
    app.close()
  })
}

wx20180518-180518

Expected behavior

Minimal reproduction of the problem with instructions

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

Environment


Nest version: 5.0.0
TypeORM version: 0.2.5
MySQL version: 2.15.0


For Tooling issues:
- Node version: 8.11.2  
- Platform:  Mac

Others:

3rd-party 馃殌 type type

Most helpful comment

In latest @nestjs/[email protected] the keepConnectionAlive option is available. Set it to true in order to reuse the same connection between rebuilds. I'll update the docs soon

All 8 comments

Hi.

I think you should post (at least) your connection & configuration code (call to TypeOrmModule.forRoot, for instance).

Btw, if you're loading entities as TS files with a global path (e.g. src/**/*.entity.ts), the default webpack configuration won't allow it / the main script won't run.

There are several issues here:

  • When I was creating issue about Ability to reinitialize components of module I hoped for REAL hot swap! But instead we got changed file typescript recompilation, which does not give performance boost at all (with typescript settings transpileOnly: true). Application is still fully reinitialized.
  • So you must handle this reinitialization in typeorm and close connection:
import { getConnection } from 'typeorm';

    if (module.hot) {
        const connection = getConnection();
        if (connection.isConnected) {
            await connection.close();
        }
        module.hot.accept();
        module.hot.dispose(() => {
            app.close();
        });
    }

See full example here

  • About import entites (same issue with migration), yes glob pattern *.ts will not work, but you can import entites explicitly
import { Cat } from './cat/cat.entity';
import { User } from './user/user.entity';

@Module({
    imports: [
            entities: [
                Cat, User
            ],
        }),
    ],
})

Second option could be webpack require.context feature:

const entityContext = require.context('.', true, /\.entity\.ts$/);
entityContext.keys().forEach(entityContext);

But I did not test it.

Third option is to use ts-node instead of node (which will parse ts files as well) but it's major hit to performance and using webpack hmr aproach does not make sense at all)

@unlight you can't close connection like that, it will report an error when doing some database operation.
wx20180521-105129

Your errors says "connection is not established", that's why I'm checking flag connection.isConnected.
Anyway it works for me.

@unlight your code will close the connection at the application startup

I'll extend @nestjs/typeorm with keepConnection (or similar one) option in order to avoid reconnecting between successive compilations.

In latest @nestjs/[email protected] the keepConnectionAlive option is available. Set it to true in order to reuse the same connection between rebuilds. I'll update the docs soon

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artaommahe picture artaommahe  路  3Comments

marshall007 picture marshall007  路  3Comments

cojack picture cojack  路  3Comments

thohoh picture thohoh  路  3Comments

tronginc picture tronginc  路  3Comments