mirror of
https://github.com/docmost/docmost.git
synced 2025-11-14 11:21:11 +10:00
Setup TypeORM
This commit is contained in:
18
server/src/environment/environment.module.ts
Normal file
18
server/src/environment/environment.module.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { EnvironmentService } from './environment.service';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { validate } from './environment.validation';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
expandVariables: true,
|
||||
validate,
|
||||
}),
|
||||
],
|
||||
providers: [EnvironmentService],
|
||||
exports: [EnvironmentService],
|
||||
})
|
||||
export class EnvironmentModule {}
|
||||
18
server/src/environment/environment.service.spec.ts
Normal file
18
server/src/environment/environment.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { EnvironmentService } from './environment.service';
|
||||
|
||||
describe('EnvironmentService', () => {
|
||||
let service: EnvironmentService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [EnvironmentService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<EnvironmentService>(EnvironmentService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
26
server/src/environment/environment.service.ts
Normal file
26
server/src/environment/environment.service.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class EnvironmentService {
|
||||
constructor(private configService: ConfigService) {}
|
||||
|
||||
getEnv(): string {
|
||||
return this.configService.get<string>('NODE_ENV');
|
||||
}
|
||||
|
||||
getPort(): string {
|
||||
return this.configService.get<string>('PORT');
|
||||
}
|
||||
getDatabaseURL(): string {
|
||||
return this.configService.get<string>('DATABASE_URL');
|
||||
}
|
||||
|
||||
getJwtSecret(): string {
|
||||
return this.configService.get<string>('JWT_SECRET_KEY');
|
||||
}
|
||||
|
||||
getJwtTokenExpiresIn(): string {
|
||||
return this.configService.get<string>('JWT_TOKEN_EXPIRES_IN');
|
||||
}
|
||||
}
|
||||
20
server/src/environment/environment.validation.ts
Normal file
20
server/src/environment/environment.validation.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { IsString, IsUrl, validateSync } from 'class-validator';
|
||||
import { plainToClass } from 'class-transformer';
|
||||
|
||||
export class EnvironmentVariables {
|
||||
@IsString()
|
||||
NODE_ENV: string;
|
||||
|
||||
@IsUrl({ protocols: ['postgres', 'postgresql'], require_tld: false })
|
||||
DATABASE_URL: string;
|
||||
}
|
||||
|
||||
export function validate(config: Record<string, any>) {
|
||||
const validatedConfig = plainToClass(EnvironmentVariables, config);
|
||||
|
||||
const errors = validateSync(validatedConfig);
|
||||
if (errors.length > 0) {
|
||||
throw new Error(errors.toString());
|
||||
}
|
||||
return validatedConfig;
|
||||
}
|
||||
Reference in New Issue
Block a user