{"library":"mongo-seeding","title":"MongoDB Database Seeding Library","description":"mongo-seeding is a Node.js library specifically designed to populate MongoDB databases with initial data, serving critical roles in development, testing, and creating demo environments. It offers flexibility in data definition, supporting various formats like JSON files and custom JavaScript/TypeScript scripts, and provides granular control over the data insertion process. The library is currently in its active development phase, with version 4.0.2 being the latest stable release as of recent updates. Its release cadence typically involves regular maintenance updates addressing dependencies and minor bug fixes, alongside significant major version increments that introduce breaking changes and new features. A key differentiator for `mongo-seeding` is its declarative approach to defining seed data, comprehensive support for both programmatic API usage and a command-line interface (CLI), and robust error handling mechanisms during data insertion, which collectively make it a reliable and efficient solution for managing and initializing MongoDB database states.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mongo-seeding"],"cli":{"name":"mongo-seeding","version":null}},"imports":["import { Seeder } from 'mongo-seeding';","import type { SeederConfig } from 'mongo-seeding';","import type { DatabaseConfig } from 'mongo-seeding';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Seeder } from 'mongo-seeding';\nimport path from 'path';\n\nconst MONGO_URI = process.env.MONGO_URI ?? 'mongodb://localhost:27017/my-database';\n\nconst config = {\n  database: {\n    uri: MONGO_URI,\n    options: {\n      useNewUrlParser: true,\n      useUnifiedTopology: true,\n    },\n  },\n  dropDatabase: true, // Clears the database before seeding\n  bulkWriteOptions: {\n    ordered: true,\n    // writeConcern: { w: 'majority' }\n  }\n};\n\nconst seeder = new Seeder(config);\n\nconst collections = seeder.readCollectionsFromPath(\n  path.resolve(__dirname, 'data'), // Assuming 'data' directory contains JSON/JS files\n  {\n    extensions: ['json', 'js', 'ts'],\n  },\n);\n\nasync function seedDatabase() {\n  try {\n    await seeder.import(collections);\n    console.log('Database seeded successfully!');\n  } catch (err) {\n    console.error('Error seeding database:', err);\n  } finally {\n    // The Seeder class doesn't expose a direct close method for the client.\n    // In a real application, ensure the underlying MongoClient is closed if managed externally.\n  }\n}\n\nseedDatabase();\n\n// Example content for 'data/users.json':\n// [\n//   { \"name\": \"Alice\", \"email\": \"alice@example.com\" },\n//   { \"name\": \"Bob\", \"email\": \"bob@example.com\" }\n// ]","lang":"typescript","description":"This quickstart demonstrates how to initialize `mongo-seeding`, configure the database connection, read seed data from a directory, and import it into MongoDB. It includes handling potential errors during the seeding process.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}