seeder
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import { Food } from '../modules/foods/entities/food.entity';
|
||||
import { Restaurant } from '../modules/restaurants/entities/restaurant.entity';
|
||||
import { Category } from '../modules/foods/entities/category.entity';
|
||||
import { foodsData } from './data/foods.data';
|
||||
|
||||
export class FoodsSeeder {
|
||||
async run(
|
||||
em: EntityManager,
|
||||
restaurantsMap: Map<string, Restaurant>,
|
||||
categoriesMap: Map<string, Category>,
|
||||
): Promise<void> {
|
||||
for (const foodData of foodsData) {
|
||||
const restaurant = restaurantsMap.get(foodData.restaurantSlug);
|
||||
const category = categoriesMap.get(`${foodData.restaurantSlug}-${foodData.categoryTitle}`);
|
||||
|
||||
if (!restaurant || !category) continue;
|
||||
|
||||
const existing = await em.findOne(Food, {
|
||||
title: foodData.title,
|
||||
restaurant,
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
const food = em.create(Food, {
|
||||
title: foodData.title,
|
||||
desc: foodData.desc,
|
||||
price: foodData.price,
|
||||
restaurant,
|
||||
category,
|
||||
isActive: foodData.isActive,
|
||||
stock: foodData.stock,
|
||||
stockDefault: foodData.stockDefault,
|
||||
sat: false,
|
||||
sun: false,
|
||||
mon: false,
|
||||
breakfast: false,
|
||||
noon: false,
|
||||
dinner: false,
|
||||
discount: 0,
|
||||
rate: 0,
|
||||
inPlaceServe: false,
|
||||
pickupServe: false,
|
||||
});
|
||||
|
||||
em.persist(food);
|
||||
}
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user