{"library":"mongo-seeding-cli","title":"Mongo Seeding CLI and Library","description":"Mongo Seeding is a versatile tool designed for populating MongoDB databases, offering flexibility through its command-line interface (CLI), a programmatic JavaScript/TypeScript library, and a Docker image. It enables developers to define import data using JavaScript, TypeScript, or JSON files, allowing for dynamic data generation and logic, a key differentiator from simpler tools like `mongoimport` which primarily handle static JSON. The current stable version is 4.0.2, and the project demonstrates an active release cadence with regular maintenance updates and significant version bumps, such as v4.0.0. It is frequently used for setting up development environments, testing database queries, and establishing initial application states.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mongo-seeding-cli"],"cli":{"name":"mongo-seed","version":null}},"imports":["import { Seeder } from 'mongo-seeding';","import type { SeederConfig } from 'mongo-seeding';","import { Seeder } from 'mongo-seeding'; const { replaceDocumentIdWithUnderscoreId, setTimestamps } = Seeder.Transformers;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Seeder } from 'mongo-seeding';\nimport path from 'path';\n\nconst config = {\n  database: 'mongodb://localhost:27017/my-database-test',\n  dropDatabase: true, // Be cautious: this will delete the entire database\n  dropCollections: true,\n  // As of v4.0.0, use bulkWriteOptions instead of collectionInsertManyOptions\n  bulkWriteOptions: {\n    ordered: false,\n  },\n};\n\nconst seeder = new Seeder(config);\nconst collections = seeder.readCollectionsFromPath(\n  path.resolve(__dirname, './data'),\n  {\n    transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],\n  },\n);\n\nasync function seedDatabase() {\n  try {\n    console.log('Starting database seeding...');\n    await seeder.import(collections);\n    console.log('Database seeding completed successfully.');\n  } catch (err) {\n    console.error('Database seeding failed:', err);\n    process.exit(1);\n  }\n}\n\n// Example data structure in a './data/users/users.js' file:\n// module.exports = [\n//   { id: 'user1', name: 'Alice', email: 'alice@example.com' },\n//   { id: 'user2', name: 'Bob', email: 'bob@example.com' },\n// ];\n\nseedDatabase();","lang":"typescript","description":"This quickstart demonstrates how to programmatically seed a MongoDB database using the `mongo-seeding` library in TypeScript, including configuring the seeder, reading data from files, and applying transformers. It also highlights the usage of `bulkWriteOptions` introduced in v4.0.0.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}