152 lines
4.8 KiB
TypeScript
152 lines
4.8 KiB
TypeScript
import { MongoClient, ObjectId } from 'mongodb';
|
|
|
|
const uri = 'mongodb://root:rootpassword@localhost:27017/icostpro?authSource=admin';
|
|
|
|
async function seed() {
|
|
const client = new MongoClient(uri);
|
|
await client.connect();
|
|
const db = client.db();
|
|
|
|
console.log('Connected to MongoDB. Seeding...');
|
|
|
|
// Clear existing data
|
|
await Promise.all([
|
|
db.collection('categories').deleteMany({}),
|
|
db.collection('subcategories').deleteMany({}),
|
|
db.collection('suppliers').deleteMany({}),
|
|
db.collection('ingredients').deleteMany({}),
|
|
]);
|
|
|
|
const now = new Date();
|
|
|
|
// Insert categories
|
|
const categoryIds = {
|
|
dairy: new ObjectId(),
|
|
dryGoods: new ObjectId(),
|
|
seafood: new ObjectId(),
|
|
produce: new ObjectId(),
|
|
};
|
|
|
|
await db.collection('categories').insertMany([
|
|
{ _id: categoryIds.dairy, name: 'Dairy', createdAt: now, updatedAt: now },
|
|
{ _id: categoryIds.dryGoods, name: 'Dry Goods', createdAt: now, updatedAt: now },
|
|
{ _id: categoryIds.seafood, name: 'Seafood', createdAt: now, updatedAt: now },
|
|
{ _id: categoryIds.produce, name: 'Produce', createdAt: now, updatedAt: now },
|
|
]);
|
|
console.log('Inserted 4 categories');
|
|
|
|
// Insert subcategories
|
|
const subcategoryIds = {
|
|
butterSpreads: new ObjectId(),
|
|
flours: new ObjectId(),
|
|
freshFish: new ObjectId(),
|
|
spicesExtracts: new ObjectId(),
|
|
fruits: new ObjectId(),
|
|
};
|
|
|
|
await db.collection('subcategories').insertMany([
|
|
{ _id: subcategoryIds.butterSpreads, name: 'Butter & Spreads', categoryId: categoryIds.dairy, createdAt: now, updatedAt: now },
|
|
{ _id: subcategoryIds.flours, name: 'Flours', categoryId: categoryIds.dryGoods, createdAt: now, updatedAt: now },
|
|
{ _id: subcategoryIds.freshFish, name: 'Fresh Fish', categoryId: categoryIds.seafood, createdAt: now, updatedAt: now },
|
|
{ _id: subcategoryIds.spicesExtracts, name: 'Spices & Extracts', categoryId: categoryIds.dryGoods, createdAt: now, updatedAt: now },
|
|
{ _id: subcategoryIds.fruits, name: 'Fruits', categoryId: categoryIds.produce, createdAt: now, updatedAt: now },
|
|
]);
|
|
console.log('Inserted 5 subcategories');
|
|
|
|
// Insert suppliers
|
|
const supplierIds = {
|
|
dairyFresh: new ObjectId(),
|
|
grainMasters: new ObjectId(),
|
|
oceanHarvest: new ObjectId(),
|
|
spiceWorld: new ObjectId(),
|
|
freshFarm: new ObjectId(),
|
|
};
|
|
|
|
await db.collection('suppliers').insertMany([
|
|
{ _id: supplierIds.dairyFresh, name: 'DairyFresh Co.', createdAt: now, updatedAt: now },
|
|
{ _id: supplierIds.grainMasters, name: 'GrainMasters', createdAt: now, updatedAt: now },
|
|
{ _id: supplierIds.oceanHarvest, name: 'OceanHarvest', createdAt: now, updatedAt: now },
|
|
{ _id: supplierIds.spiceWorld, name: 'SpiceWorld Ltd', createdAt: now, updatedAt: now },
|
|
{ _id: supplierIds.freshFarm, name: 'FreshFarm Direct', createdAt: now, updatedAt: now },
|
|
]);
|
|
console.log('Inserted 5 suppliers');
|
|
|
|
// Insert ingredients
|
|
await db.collection('ingredients').insertMany([
|
|
{
|
|
code: 'ING-001',
|
|
name: 'Organic Butter',
|
|
categoryId: categoryIds.dairy,
|
|
subcategoryId: subcategoryIds.butterSpreads,
|
|
quantity: 25,
|
|
unit: 'kg',
|
|
unitPrice: 4.50,
|
|
vat: 9,
|
|
supplierId: supplierIds.dairyFresh,
|
|
createdAt: new Date('2026-02-10'),
|
|
updatedAt: new Date('2026-02-10'),
|
|
},
|
|
{
|
|
code: 'ING-002',
|
|
name: 'All-Purpose Flour',
|
|
categoryId: categoryIds.dryGoods,
|
|
subcategoryId: subcategoryIds.flours,
|
|
quantity: 150,
|
|
unit: 'kg',
|
|
unitPrice: 0.85,
|
|
vat: 9,
|
|
supplierId: supplierIds.grainMasters,
|
|
createdAt: new Date('2026-02-09'),
|
|
updatedAt: new Date('2026-02-09'),
|
|
},
|
|
{
|
|
code: 'ING-003',
|
|
name: 'Atlantic Salmon Fillet',
|
|
categoryId: categoryIds.seafood,
|
|
subcategoryId: subcategoryIds.freshFish,
|
|
quantity: 12,
|
|
unit: 'kg',
|
|
unitPrice: 18.90,
|
|
vat: 9,
|
|
supplierId: supplierIds.oceanHarvest,
|
|
createdAt: new Date('2026-02-11'),
|
|
updatedAt: new Date('2026-02-11'),
|
|
},
|
|
{
|
|
code: 'ING-004',
|
|
name: 'Madagascar Vanilla Extract',
|
|
categoryId: categoryIds.dryGoods,
|
|
subcategoryId: subcategoryIds.spicesExtracts,
|
|
quantity: 8,
|
|
unit: 'L',
|
|
unitPrice: 45.00,
|
|
vat: 21,
|
|
supplierId: supplierIds.spiceWorld,
|
|
createdAt: new Date('2026-02-08'),
|
|
updatedAt: new Date('2026-02-08'),
|
|
},
|
|
{
|
|
code: 'ING-005',
|
|
name: 'Fresh Avocados',
|
|
categoryId: categoryIds.produce,
|
|
subcategoryId: subcategoryIds.fruits,
|
|
quantity: 40,
|
|
unit: 'pcs',
|
|
unitPrice: 1.20,
|
|
vat: 9,
|
|
supplierId: supplierIds.freshFarm,
|
|
createdAt: new Date('2026-02-11'),
|
|
updatedAt: new Date('2026-02-11'),
|
|
},
|
|
]);
|
|
console.log('Inserted 5 ingredients');
|
|
|
|
console.log('Seeding complete!');
|
|
await client.close();
|
|
}
|
|
|
|
seed().catch((err) => {
|
|
console.error('Seed failed:', err);
|
|
process.exit(1);
|
|
});
|