ticket-booking-be/src/app.service.ts

101 lines
3.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { CommonService } from './common/common.service';
import * as fs from 'fs';
import * as path from 'path';
import { Sequelize } from 'sequelize-typescript';
import { AppConfigService } from './app-config/app-config.service';
import { Utility } from './common/Utility';
@Injectable()
export class AppService {
modelFilePaths: string[] = [] as string[];
constructor(private commonService: CommonService, private configService: AppConfigService) { }
getHello(): string {
return 'Hello World!';
}
async initializeSequelize() {
this.getModels();
const dbConfig = this.configService.getDbConfig();
if (this.modelFilePaths.length > 0) {
this.commonService.sequelize = new Sequelize({
database: "ticket_booking_db",
host: "192.168.0.113", // <-- IP or hostname of your DB
port: 5431,
dialect: 'postgres',
username: "postgres",
password: "FAdDoCwhmi3BvkK4pgEGDc7EspmDtHYV4y93748Xz6VysJKPdlsaU3L9N9k7OD9t",
models: this.modelFilePaths,
modelMatch: (filename, member) => {
return filename.substring(0, filename.indexOf('.entity')) === member.toLowerCase();
},
});
await this.commonService.sequelize.sync({ alter: true });
Utility.sequelize = this.commonService.sequelize;
}
const fileConfig = this.configService.initializeFileSystem();
this.commonService.fileConfig = fileConfig;
Utility.fileConfig = fileConfig;
const emailConfig = this.configService.getMailConfig();
this.commonService.mailConfig = emailConfig;
Utility.mailConfig = emailConfig;
const jwtConfig = this.configService.getJwtConfig();
this.commonService.jwtConfig = jwtConfig;
Utility.jwtConfig = jwtConfig;
const vapidConfig = this.configService.getVapidConfig();
this.commonService.vapidConfig = vapidConfig;
Utility.vapidConfig = vapidConfig;
const swaggerConfig = this.configService.getSwaggerConfig();
this.commonService.swaggerConfig = swaggerConfig;
Utility.swaggerConfig = swaggerConfig;
const redisConfig = this.configService.getRedisConfig();
this.commonService.redisConfig = redisConfig;
Utility.redisConfig = redisConfig;
const googleOauthConfig = this.configService.getGoogleOauthConfig();
this.commonService.googleOauthConfig = googleOauthConfig;
Utility.googleOauthConfig = googleOauthConfig;
}
getModels() {
this.fromDir(__dirname, '.entity', ['.js', '.ts']);
if (this.modelFilePaths.length > 0) {
}
}
fromDir(startPath, filter, extensions) {
if (!fs.existsSync(startPath) || !extensions || extensions.length === 0) {
console.log("no dir ", startPath);
return;
}
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
this.fromDir(filename, filter, extensions); //recurse
} else if (filename.includes(filter)) {
extensions.map((extension) => {
if (filename.endsWith(`${filter}${extension}`)) {
this.modelFilePaths.push(filename);
}
})
};
};
}
}