Commit 168b1918 authored by Diego Iván's avatar Diego Iván
Browse files

agregando scripts que agregan datos por defecto

parent a847ed1d
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
import { Module } from '@nestjs/common';
import { DatabaseSeederService } from './database-seeder.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { State } from 'src/state/entities/state.entity';
import { StateService } from 'src/state/state.service';

@Module({
  providers: [DatabaseSeederService, StateService],
  imports: [TypeOrmModule.forFeature([State])],
})
export class DatabaseSeederModule {}
+25 −0
Original line number Diff line number Diff line
import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { State } from 'src/state/entities/state.entity';
import { StateService } from 'src/state/state.service';
import { Repository } from 'typeorm';
import * as data from './states.json';

@Injectable()
export class DatabaseSeederService implements OnModuleInit {
  constructor(
    @InjectRepository(State) private stateRepo: Repository<State>,
    private readonly stateService: StateService,
  ) {}

  async insertStates() {
    const states = data.states;
    for (const state of states) {
      await this.stateService.create(state);
    }
  }

  async onModuleInit() {
    await this.insertStates();
  }
}