@@ -47,6 +47,68 @@ interface ArrayStructure {
4747 elementType : FieldMapping | DocumentStructure | ArrayStructure ;
4848}
4949
50+ /**
51+ * Entry point method: Generate the final script
52+ */
53+ export function generateScript (
54+ schema : Record < string , FieldMapping > ,
55+ options : ScriptOptions
56+ ) : ScriptResult {
57+ try {
58+ const structure = buildDocumentStructure ( schema ) ;
59+
60+ const documentCode = generateDocumentCode (
61+ structure ,
62+ INDENT_SIZE * 2 , // 4 spaces: 2 for function body + 2 for inside return statement
63+ options . arrayLengthMap
64+ ) ;
65+
66+ // Escape ' and ` in database/collection names for template literals
67+ const escapedDbName = options . databaseName
68+ . replace ( / ' / g, "\\'" )
69+ . replace ( / ` / g, '\\`' ) ;
70+ const escapedCollectionName = options . collectionName
71+ . replace ( / ' / g, "\\'" )
72+ . replace ( / ` / g, '\\`' ) ;
73+
74+ const script = `// Mock Data Generator Script
75+ // Generated for collection: ${ escapedDbName } .${ escapedCollectionName }
76+ // Document count: ${ options . documentCount }
77+
78+ const { faker } = require('@faker-js/faker');
79+
80+ // Connect to database
81+ use('${ escapedDbName } ');
82+
83+ // Document generation function
84+ function generateDocument() {
85+ return ${ documentCode } ;
86+ }
87+
88+ // Generate and insert documents
89+ const documents = [];
90+ for (let i = 0; i < ${ options . documentCount } ; i++) {
91+ documents.push(generateDocument());
92+ }
93+
94+ // Insert documents into collection
95+ db.getCollection('${ escapedCollectionName } ').insertMany(documents);
96+
97+ console.log(\`Successfully inserted \${documents.length} documents into ${ escapedDbName } .${ escapedCollectionName } \`);` ;
98+
99+ return {
100+ script,
101+ success : true ,
102+ } ;
103+ } catch ( error ) {
104+ return {
105+ script : '' ,
106+ success : false ,
107+ error : error instanceof Error ? error . message : 'Unknown error' ,
108+ } ;
109+ }
110+ }
111+
50112/**
51113 * Parse a field path into simple parts
52114 *
@@ -113,6 +175,7 @@ function parseFieldPath(fieldPath: string): string[] {
113175
114176 return parts ;
115177}
178+
116179/**
117180 * Build the document structure from all field paths
118181 */
@@ -252,68 +315,6 @@ function insertIntoStructure(
252315 }
253316}
254317
255- /**
256- * Generate the final script
257- */
258- export function generateScript (
259- schema : Record < string , FieldMapping > ,
260- options : ScriptOptions
261- ) : ScriptResult {
262- try {
263- const structure = buildDocumentStructure ( schema ) ;
264-
265- const documentCode = generateDocumentCode (
266- structure ,
267- INDENT_SIZE * 2 , // 4 spaces: 2 for function body + 2 for inside return statement
268- options . arrayLengthMap || { }
269- ) ;
270-
271- // Escape ' and ` in database/collection names for template literals
272- const escapedDbName = options . databaseName
273- . replace ( / ' / g, "\\'" )
274- . replace ( / ` / g, '\\`' ) ;
275- const escapedCollectionName = options . collectionName
276- . replace ( / ' / g, "\\'" )
277- . replace ( / ` / g, '\\`' ) ;
278-
279- const script = `// Mock Data Generator Script
280- // Generated for collection: ${ escapedDbName } .${ escapedCollectionName }
281- // Document count: ${ options . documentCount }
282-
283- const { faker } = require('@faker-js/faker');
284-
285- // Connect to database
286- use('${ escapedDbName } ');
287-
288- // Document generation function
289- function generateDocument() {
290- return ${ documentCode } ;
291- }
292-
293- // Generate and insert documents
294- const documents = [];
295- for (let i = 0; i < ${ options . documentCount } ; i++) {
296- documents.push(generateDocument());
297- }
298-
299- // Insert documents into collection
300- db.getCollection('${ escapedCollectionName } ').insertMany(documents);
301-
302- console.log(\`Successfully inserted \${documents.length} documents into ${ escapedDbName } .${ escapedCollectionName } \`);` ;
303-
304- return {
305- script,
306- success : true ,
307- } ;
308- } catch ( error ) {
309- return {
310- script : '' ,
311- success : false ,
312- error : error instanceof Error ? error . message : 'Unknown error' ,
313- } ;
314- }
315- }
316-
317318/**
318319 * Generate JavaScript object code from document structure
319320 */
0 commit comments