add TypeORM postgres config and migration runner.
This commit is contained in:
@@ -5,6 +5,7 @@ POSTGRES_DB=private_image
|
||||
POSTGRES_USER=postgres
|
||||
POSTGRES_PASSWORD=postgres
|
||||
POSTGRES_PORT=5432
|
||||
POSTGRES_MIGRATIONS_RUN=false
|
||||
|
||||
PGADMIN_DEFAULT_EMAIL=admin@example.com
|
||||
PGADMIN_DEFAULT_PASSWORD=admin
|
||||
|
||||
Generated
+1184
-21
File diff suppressed because it is too large
Load Diff
+13
-2
@@ -7,22 +7,33 @@
|
||||
"build": "nest build",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:prod": "node dist/main"
|
||||
"start:prod": "node dist/main",
|
||||
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli",
|
||||
"typeorm:run-migrations": "npm run typeorm migration:run -- -d ./src/database/DataSource.ts",
|
||||
"typeorm:generate-migration": "npm run typeorm -- -d ./src/database/DataSource.ts migration:generate ./src/database/migrations/$npm_config_name",
|
||||
"typeorm:revert-migration": "npm run typeorm -- -d ./src/database/DataSource.ts migration:revert",
|
||||
"typeorm:create-migration": "npm run typeorm -- migration:create ./src/database/migrations/$npm_config_name"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/common": "^11.0.1",
|
||||
"@nestjs/config": "^4.0.3",
|
||||
"@nestjs/core": "^11.0.1",
|
||||
"@nestjs/platform-express": "^11.0.1",
|
||||
"@nestjs/typeorm": "^12.0.2",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.15.1",
|
||||
"reflect-metadata": "^0.2.2"
|
||||
"pg": "^8.23.1",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"typeorm": "^0.3.28"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^11.0.24",
|
||||
"@nestjs/schematics": "^11.0.0",
|
||||
"@types/express": "^5.0.0",
|
||||
"@types/node": "^22.10.7",
|
||||
"@types/pg": "^8.23.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typescript": "~6.0.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import appConfig from './config';
|
||||
import { ConfigModule, ConfigService, registerAs } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { getAppConfig, getPostgresConfig } from './config';
|
||||
import { HealthCheckModule } from './modules/healthCheck/HealthCheckModule';
|
||||
import { Config } from './types/Config';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
load: [appConfig]
|
||||
load: [registerAs('postgres', getPostgresConfig), registerAs('app', getAppConfig)]
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
useFactory: (configService: ConfigService) => configService.get('postgres') as Config['postgres'],
|
||||
inject: [ConfigService]
|
||||
}),
|
||||
HealthCheckModule
|
||||
]
|
||||
|
||||
@@ -1,11 +1,28 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
import { AppConfig } from '../types/AppConfig';
|
||||
import { PostgresConfig } from '../types/Config';
|
||||
import { NodeEnv } from '../types/NodeEnv';
|
||||
|
||||
const env = (key: string): string => process.env[key] || '';
|
||||
|
||||
const envInt = (key: string): number => parseInt(env(key), 10);
|
||||
|
||||
const isEnabled = (key: string): boolean => env(key) === 'true';
|
||||
|
||||
export const getPostgresConfig = (): PostgresConfig => {
|
||||
return {
|
||||
type: 'postgres',
|
||||
url: `postgres://${env('POSTGRES_USER')}:${env('POSTGRES_PASSWORD')}@${env('POSTGRES_HOST')}:${env('POSTGRES_PORT')}/${env('POSTGRES_DB')}`,
|
||||
host: env('POSTGRES_HOST'),
|
||||
username: env('POSTGRES_USER'),
|
||||
password: env('POSTGRES_PASSWORD'),
|
||||
port: envInt('POSTGRES_PORT'),
|
||||
database: env('POSTGRES_DB'),
|
||||
entities: [__dirname + '/../modules/**/entities/*{.ts,.js}'],
|
||||
migrations: [__dirname + '/../database/migrations/*{.ts,.js}'],
|
||||
migrationsRun: isEnabled('POSTGRES_MIGRATIONS_RUN')
|
||||
};
|
||||
};
|
||||
|
||||
export const getAppConfig = (): AppConfig => {
|
||||
const rawOrigins = env('CORS_ORIGINS');
|
||||
|
||||
@@ -18,5 +35,3 @@ export const getAppConfig = (): AppConfig => {
|
||||
.filter(Boolean)
|
||||
};
|
||||
};
|
||||
|
||||
export default registerAs('app', getAppConfig);
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { getPostgresConfig } from '../config';
|
||||
|
||||
export default new DataSource(getPostgresConfig());
|
||||
+2
-2
@@ -2,13 +2,13 @@ import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './AppModule';
|
||||
import { AppConfig } from './types/AppConfig';
|
||||
import { Config } from './types/Config';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
const configService = app.get(ConfigService);
|
||||
const { port, corsOrigins } = configService.get('app') as AppConfig;
|
||||
const { port, corsOrigins } = configService.get('app') as Config['app'];
|
||||
|
||||
app.setGlobalPrefix('api');
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { AppConfig } from './AppConfig';
|
||||
|
||||
export interface PostgresConfig {
|
||||
type: 'postgres';
|
||||
url: string;
|
||||
host: string;
|
||||
username: string;
|
||||
password: string;
|
||||
port: number;
|
||||
database: string;
|
||||
entities: string[];
|
||||
migrations: string[];
|
||||
migrationsRun: boolean;
|
||||
}
|
||||
|
||||
export interface Config {
|
||||
app: AppConfig;
|
||||
postgres: PostgresConfig;
|
||||
}
|
||||
Reference in New Issue
Block a user