Reputation: 123
On NodeJS (NestJS) I used TypeORM to connect to Database (Oracle). I would like to pass the username and password during runtime of the application.
Due to security reasons, in my company, the security is configured in away that for each user, we create a database schema. He log into The application using his DB credential. I know I t is not very popular practice in the industry.
const connection = await createConnection({
type: "oracle",
host: "localhost",
port: 3306,
username: "test", // this need to pass during run time
password: "test", // same thing for the password
database: "test"
});
Could you please share with me any reference/ hints on how to achieve this on typeORM and nodeJs/NestJs?
Thanks
Upvotes: 4
Views: 2112
Reputation: 6006
You can use register TypeORM in the root level, and use a configuration service to provide the connection details:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
host: config.get('DB_HOST'),
username: config.get('DB_USER'),
password: config.get('DB_PASSWORD'),
database: config.get('DB_NAME'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
port: 3306,
type: 'oracle',
}),
inject: [ConfigService],
}),
ConfigService is just a simple service that implements a get
function that provides the specific requested configuration.
More on that here: https://docs.nestjs.com/techniques/database#async-configuration
Upvotes: 1