mohamedsamara / mern-ecommerce

:balloon: Fullstack MERN Ecommerce Application
https://mern-store-gold.vercel.app
MIT License
1.63k stars 770 forks source link

Update DB Seeding code to use faker to populate Categories, Brands, Products. #242

Closed petecheslock closed 3 weeks ago

petecheslock commented 2 months ago

Thank you for this great project. We use this application as an interactive demo for AppMap Navie. The following code and pull request was generated by the Navie AI Architect. Let us know if you would like any other changes.

This fixes #137

Changes

  1. package.json & package-lock.json:

    • Added new dependency: @faker-js/faker.
  2. Seed Script (server/utils/seed.js):

    • Integrated Faker.js for generating sample data.
    • Implemented checks for existing data to avoid redundant seeding.
    • Included seeding logic for categories, brands, and products.
    • Established associations between products and categories.
    • Ensured proper closure of the MongoDB connection after seeding completion.

Running the seed for the first time:

$ npm run seed:db -- admin@example.com strongpassword

> server@1.0.0 seed:db
> node utils/seed.js admin@example.com strongpassword

✓ Seed database started
✓ MongoDB Connected!
! Seeding admin user...
✓ Admin user seeded.
✓ Categories seeded.
✓ Brands seeded.
✓ Products seeded and associated with categories.
✓ Database connection closed!

Running the seed again later:

$ npm run seed:db -- admin@example.com strongpassword

> server@1.0.0 seed:db
> node utils/seed.js admin@example.com strongpassword

✓ Seed database started
✓ MongoDB Connected!
! Admin user already exists, skipping seeding for admin user.
! Sufficient number of categories already exist, skipping seeding for categories.
! Sufficient number of brands already exist, skipping seeding for brands.
! Sufficient number of products already exist, skipping seeding for products.
✓ Database connection closed!
vercel[bot] commented 2 months ago

@petecheslock is attempting to deploy a commit to the mohamedsamara's projects Team on Vercel.

A member of the Team first needs to authorize it.

mohamedsamara commented 3 weeks ago

@petecheslock Thank you for the Pull Request. Just merged 👍