chore: add typeorm config

This commit is contained in:
mahyargdz
2025-01-19 10:23:18 +03:30
parent 3fb52961c7
commit 2fda19e537
7 changed files with 780 additions and 461 deletions
+10 -1
View File
@@ -1,7 +1,16 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { TypeOrmModule } from "@nestjs/typeorm";
import { DatabaseConfigs } from "./configs/typeorm.config";
@Module({
imports: [],
imports: [
ConfigModule.forRoot({
cache: true,
isGlobal: true,
}),
TypeOrmModule.forRootAsync(DatabaseConfigs()),
],
controllers: [],
providers: [],
})
+13
View File
@@ -0,0 +1,13 @@
import { NestFastifyApplication } from "@nestjs/platform-fastify";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
export function getSwaggerDocument(app: NestFastifyApplication) {
const swaggerConfig = new DocumentBuilder()
.setTitle("The Danak dsc api document")
.setDescription("The Danak dsc API description")
.setVersion("1.0.0")
.build();
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup("api-docs", app, swaggerDocument);
}
+23
View File
@@ -0,0 +1,23 @@
import { ConfigService } from "@nestjs/config";
import { TypeOrmModuleAsyncOptions } from "@nestjs/typeorm";
export function DatabaseConfigs(): TypeOrmModuleAsyncOptions {
return {
inject: [ConfigService],
useFactory(configService: ConfigService) {
return {
type: "postgres",
host: configService.getOrThrow<string>("DB_HOST"),
port: configService.getOrThrow<number>("DB_PORT"),
database: configService.getOrThrow<string>("DB_NAME"),
username: configService.getOrThrow<string>("DB_USER"),
password: configService.getOrThrow<string>("DB_PASS"),
synchronize: true,
logging: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true,
migrationsTableName: "typeorm_migrations",
migrationsRun: false,
autoLoadEntities: true,
};
},
};
}
+18 -4
View File
@@ -1,8 +1,22 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { FastifyAdapter, NestFastifyApplication } from "@nestjs/platform-fastify";
import { AppModule } from "./app.module";
import { getSwaggerDocument } from "./configs/swagger.config";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
const logger = new Logger("APP");
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
const configService = app.get<ConfigService>(ConfigService);
getSwaggerDocument(app);
const PORT = configService.getOrThrow<number>("PORT");
await app.listen(PORT, () => {
logger.log(`Server running on http://localhost:${PORT}`);
logger.log(`Swagger running on http://localhost:${PORT}/api-docs`);
});
}
bootstrap();