Files
negareh-api/src/seeders/inventory.seeder.ts
T
2026-01-07 12:24:55 +03:30

30 lines
783 B
TypeScript

import type { EntityManager } from '@mikro-orm/core';
import { product } from '../modules/products/entities/product.entity';
import { Inventory } from '../modules/inventory/entities/inventory.entity';
export class InventorySeeder {
async run(em: EntityManager): Promise<void> {
const products = await em.find(product, {});
for (const product of products) {
// Check if inventory already exists for this product
const existingInventory = await em.findOne(Inventory, {
product: { id: product.id },
});
if (!existingInventory) {
const inventory = em.create(Inventory, {
product,
totalStock: 50,
availableStock: 50,
});
em.persist(inventory);
}
}
await em.flush();
}
}