Appearance
Database Config
The DbConfig
provides a way to configure and manage the connection to your PostgreSQL database. It utilizes environment variables to define connection details and offers customization using an optional PostgresConnectionOptions
object.
Environment variables used in the DbConfig
- POSTGRES_HOST: The hostname of the PostgreSQL server.
- POSTGRES_PORT: The port number for the PostgreSQL server.
- POSTGRES_USER: The username used to connect to the PostgreSQL server.
- POSTGRES_PASS: The password used to authenticate with the PostgreSQL server.
- POSTGRES_DB: The name of the PostgreSQL database.
- POSTGRES_IS_LOGGING_ENABLED: An optional flag to enable logging for the PostgreSQL connection.
Here's a YAML formatted example of the environment variables used in the DbConfig
:
yaml
POSTGRES_HOST: 'localhost'
POSTGRES_PORT: 5432
POSTGRES_USER: 'app'
POSTGRES_PASS: 'gj3#$859lgpek'
POSTGRES_DB: 'jetstream'
POSTGRES_IS_LOGGING_ENABLED: 'true'
You can adjust the example values according to your application's needs.
How to create and customize DbConfig
Here's an example of how to use the createDbConfig
to create DbConfig
:
ts
import { createDbConfig } from '@deeepvision/nest-kit/dist/configs';
export const DbConfig = createDbConfig();
The DbConfig
can be tailored to your needs by providing an optional PostgresConnectionOptions
object as an argument.
Here's an example of how to use the createDbConfig
function with these options:
ts
// config/index.ts
import { createDbConfig } from './config/db.config';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
const postgresCustomOptions: Partial<PostgresConnectionOptions> = {
ssl: {
rejectUnauthorized: false,
},
synchronize: true,
}
export const DbConfig = createDbConfig(postgresCustomOptions);
In this example, we're modifying the ssl and synchronize
options of the PostgresConnectionOptions
object to better suit our needs.
How to use DbConfig for TypeOrmModule initialization
In your
app.module.ts
, import the required modules and configurations:tsimport { TypeOrmModule } from '@nestjs/typeorm'; import * as configs from './config';
Inside the
@Module
decorator, use theTypeOrmModule.forRootAsync()
method to initialize theTypeOrmModule
with theDbConfig
. Pass a configuration object with the following properties:useFactory
: An async factory function that takes injectedopt
s (aConfigType
ofWinstonConfig
) as an argument.inject
: An array containing the injection tokenconfigs.DbConfig.KEY
to properly inject theDbConfig
.
Here's the complete code:
ts// app.module.ts @Module({ imports: [ TypeOrmModule.forRootAsync({ useFactory: async (dbConfig: ConfigType<typeof configs.DbConfig>) => dbConfig, inject: [configs.DbConfig.KEY], }), // ...other imports ], // ...other module properties }) export class AppModule {}
By following these steps, you'll be able to utilize the DbConfig
for initializing the TypeOrmModule
with the appropriate database connection settings.
Configuring Entity Paths for TypeORM in NestKit
When configuring NestKit, you need to set up paths to NestKit entities for TypeORM. This allows TypeORM to understand which entities will be used for database operations and which ones will be used for migration generation.
To configure the entity paths, you can pass an array of file paths to the createDbConfig function, as shown in the example below:
ts
export const DbConfig = createDbConfig({
entities: [
'dist/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/auth-n/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/user-to-roles/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/organization-groups/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/organizations/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/images/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/roles/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/users/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/binary-files/**/*.entity.js',
],
});
There are full list of all entities paths:
ts
'node_modules/@deeepvision/nest-kit/dist/modules/auth-n/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/binary-files/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/brand-assignments/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/brands/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/favorite-brands/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/favorite-organizations/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/images/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/organization-groups/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/organizations/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/roles/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/shared-brands-to-organization-groups/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/shared-brands-to-organizations/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/studio-assignments/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/studios/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/timezones/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/user-manager/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/user-to-roles/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/users/**/*.entity.js',
'node_modules/@deeepvision/nest-kit/dist/modules/service-accounts/**/*.entity.js',