Comments (4)
I used another approach in my project. I basically share the options through the AppModule:
main.ts
const settings = dotenv.parse(fs.readFileSync(filePath));
const app = await NestFactory.create(AppModule.forRoot(settings));
...
app.module.ts
export class AppModule {
static forRoot(settings: APISettings): DynamicModule {
return {
module: AppModule,
imports: [
// This module has a ConfigurationService, which
// does the same as described in the docs
// but it does not read the env file.
AppSettingsModule.forRoot(settings),
DatabaseModule.forRoot(settings),
...
]
};
}
}
database.module.ts
export function getOrmConfig(settings: IDatabaseSettings) {
return {
type: 'postgres',
host: settings.host,
port: settings.port,
username: settings.username,
password: settings.password,
database: settings.database,
entities,
synchronize: true,
logging: true,
};
}
export class DatabaseModule {
public static forRoot(settings: IDatabaseSettings): DynamicModule {
const ormConfig = getOrmConfig(settings);
return {
module: DatabaseModule,
imports: [
TypeOrmModule.forRoot(ormConfig)
],
components: [
DatabaseService,
],
exports: [
DatabaseService
]
};
}
}
I think it is a bad idea to read the configuration inside the service as suggested in the docs. For example; I have the use case, that my application should be useable and configureable by npm package (npm i -s my-api
). Lets assume my API is called MyAPI
. Then the following code would not be feasable, if the configuration is read by a nestjs service.
import { MyAPI } from 'my-api';
// Bootstraps the NestJS configuration and calls await NestFactory.create(AppModule.forRoot(settings));
new MyAPI({ databaseHost: 'postgres', ... }).run();
Thats why I prefer my solution; its more dynamic and modular.
I hope you get what I mean :)
from typeorm.
The project I described in my upper comment is now opensourced.
You can checkout the API to see how I solved this issue.
The starting point would be start-api.js. Then you can go down further the rabbit hole in main.ts and app.module.ts.
Good luck. Hit me up if you have any questions :)
from typeorm.
This is a really nice approach, I think I will try this for my use case.
Thank you very much!
I'm closing this issue..
from typeorm.
I'm facing the same problem.
@BrunnerLivio your approach is ok but I still prefer handle settings with a service.
For now I didn't found a great a solution, I'm using process.env
to retrieve settings in this particular case
from typeorm.
Related Issues (20)
- Result of query with comment is inconsistent and random HOT 20
- TypeOrmModule.forRoot() report errors: ConnectionNotFoundError: Connection "default" was not found. HOT 4
- '"typeorm"' has no exported member named 'InjectRepository'. Did you mean 'getRepository'? HOT 3
- setting synchronize to true gives me a typeorm error HOT 1
- When a custom converter is used on a column, migration fails.(nestjs+typeorm) HOT 1
- Testing mocker issues HOT 1
- Option to not initialize DataSource in createDataSourceFactory HOT 6
- Many to many relation is finding a column that doesn't exists HOT 1
- Polymorphic association #12750 HOT 1
- createTypeOrmProviders should inject EntityManager instead of Connection HOT 1
- Bring back keepConnectionAlive HOT 1
- auto db connection close problem When Execute Graceful shtudown with pm2 HOT 1
- Every migration:generate creates DROP/CREATES constraints and keys HOT 1
- Inject ConfigService into an Entity or Subscriber HOT 4
- Nestjs warns about "TypeOrmModule" taking a long time to serialize HOT 8
- TypeOrmModule makes @Request() unusable HOT 1
- When connectionstring param used, Hostname and database name are not correctly extracted HOT 2
- perhaps Typeorm module didn't passing request response to controller HOT 2
- Check whether the object passed is an entity HOT 2
- missing connect/error hook/event HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeorm.