Closed jocafi closed 1 year ago
dotenv
(what @nestjs/config
uses to parse the env file) possessed everything to strings and the !!'false'
value is true
so synchronize will always be ran. The <boolean>
is just a type hint to Typescript, not an actual runtime conversion. This is not a bug, but the expected behavior of JavaScript based on the libraries being used.
@jmcdo29 Thx Jay. I changed the line below and it worked !
synchronize: configService.get('DB_SYNCRONIZE') === 'true',
Is there an existing issue for this?
Current behavior
I want customize the behaviour of the TypeOrm property "synchronize" using a defined environment variable in the '.env' file.
However using the ConfigService to get the boolean value does not work, although I could read and use all the other environment variables to configure TypeOrm.
If I set "synchronize" explicitly to true or false, it works well.
Minimum reproduction code
I put the code in the field "Steps to reproduce"
Steps to reproduce
1) Create a Postgresl database using docker 1.5) Double Check that you dropped the table user in the database. 2) Create .env in your root directory and set the following variables:
3) Change your AppModule to the following code:
4) Create the user.entity.ts using the code below:
5) Add your entity class to the module user.module.ts:
6) Run the program. 7) Check the logs to see the message: "(AppModule) Syncronize: false" 8) Check that the table user was created, although "DB_SYNCRONIZE = false" is defined in the .env
If you drop the table again and set explicitly "synchronize: false" and run it again, you will see that the table user was NOT created and this is the expected behaviour for the case above.
Expected behavior
The table user should not be created.
1) Check that "DB_SYNCRONIZE = false" is defined in the .env 2) Check that you dropped the table user before starting the program.
Package
Other package
@nestjs/config or @nestjs/typeorm
NestJS version
10.0.0
Packages versions
Node.js version
v18.16.0
In which operating systems have you tested?
Other
No response