seeder
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import { Restaurant } from '../modules/restaurants/entities/restaurant.entity';
|
||||
import { restaurantsData } from './data/restaurants.data';
|
||||
|
||||
export class RestaurantsSeeder {
|
||||
async run(em: EntityManager): Promise<Map<string, Restaurant>> {
|
||||
const restaurantsMap = new Map<string, Restaurant>();
|
||||
|
||||
for (const restaurantData of restaurantsData) {
|
||||
let restaurant = await em.findOne(Restaurant, { slug: restaurantData.slug });
|
||||
if (!restaurant) {
|
||||
restaurant = em.create(Restaurant, restaurantData);
|
||||
em.persist(restaurant);
|
||||
}
|
||||
restaurantsMap.set(restaurantData.slug, restaurant);
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
return restaurantsMap;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user