@@ -222,23 +222,44 @@ export function generateScript(
222222
223223 const documentCode = generateDocumentCode ( structure ) ;
224224
225- const script = `
226- // Generated Mock Data Script
225+ // Escape ' and ` in database/collection names for template literals
226+ const escapedDbName = options . databaseName
227+ . replace ( / ' / g, "\\'" )
228+ . replace ( / ` / g, '\\`' ) ;
229+ const escapedCollectionName = options . collectionName
230+ . replace ( / ' / g, "\\'" )
231+ . replace ( / ` / g, '\\`' ) ;
232+
233+ // Validate document count
234+ const documentCount = Math . max (
235+ 1 ,
236+ Math . min ( 10000 , Math . floor ( options . documentCount ) )
237+ ) ;
238+
239+ const script = `// Mock Data Generator Script
240+ // Generated for collection: ${ escapedDbName } .${ escapedCollectionName }
241+ // Document count: ${ documentCount }
242+
227243const { faker } = require('@faker-js/faker');
228244
245+ // Connect to database
246+ use('${ escapedDbName } ');
247+
248+ // Document generation function
229249function generateDocument() {
230250 return ${ documentCode } ;
231251}
232252
233- // Generate documents
253+ // Generate and insert documents
234254const documents = [];
235- for (let i = 0; i < ${ options . documentCount } ; i++) {
255+ for (let i = 0; i < ${ documentCount } ; i++) {
236256 documents.push(generateDocument());
237257}
238258
239- // TODO: Add database connection and insertion
240- console.log('Generated', documents.length, 'documents');
241- ` ;
259+ // Insert documents into collection
260+ db.getCollection('${ escapedCollectionName } ').insertMany(documents);
261+
262+ console.log(\`Successfully inserted \${documents.length} documents into ${ escapedDbName } .${ escapedCollectionName } \`);` ;
242263
243264 return {
244265 script,
0 commit comments