Add TypeORM integration and seed method for TipoCuestionario
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TipoCuestionarioService } from './tipo_cuestionario.service';
|
||||
import { TipoCuestionarioController } from './tipo_cuestionario.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { TipoCuestionario } from './entities/tipo_cuestionario.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([TipoCuestionario])],
|
||||
controllers: [TipoCuestionarioController],
|
||||
providers: [TipoCuestionarioService],
|
||||
})
|
||||
|
||||
@@ -1,9 +1,40 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateTipoCuestionarioDto } from './dto/create-tipo_cuestionario.dto';
|
||||
import { UpdateTipoCuestionarioDto } from './dto/update-tipo_cuestionario.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import {
|
||||
TipoCuestionario,
|
||||
TipoCuestionarioEnum,
|
||||
} from './entities/tipo_cuestionario.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class TipoCuestionarioService {
|
||||
constructor(
|
||||
@InjectRepository(TipoCuestionario)
|
||||
private tipoCuestionarioRepository: Repository<TipoCuestionario>,
|
||||
) {}
|
||||
|
||||
async onModuleInit() {
|
||||
await this.seedTipoCuestionarios();
|
||||
}
|
||||
|
||||
private async seedTipoCuestionarios() {
|
||||
const enumValues = Object.values(TipoCuestionarioEnum);
|
||||
|
||||
for (const tipoCuestionario of enumValues) {
|
||||
const existingTipo = await this.tipoCuestionarioRepository.findOne({
|
||||
where: { tipo_cuestionario: tipoCuestionario },
|
||||
});
|
||||
|
||||
if (!existingTipo) {
|
||||
await this.tipoCuestionarioRepository.save({
|
||||
tipo_cuestionario: tipoCuestionario,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
create(createTipoCuestionarioDto: CreateTipoCuestionarioDto) {
|
||||
return 'This action adds a new tipoCuestionario';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user