@@ -277,37 +277,53 @@ console.log(\`Successfully inserted \${documents.length} documents into ${escape
277277/**
278278 * Generate JavaScript object code from document structure
279279 */
280- function generateDocumentCode ( structure : DocumentStructure ) : string {
280+ function generateDocumentCode (
281+ structure : DocumentStructure ,
282+ indent : number = 2
283+ ) : string {
281284 // For each field in structure:
282285 // - If FieldMapping: generate faker call
283286 // - If DocumentStructure: generate nested object
284287 // - If ArrayStructure: generate array
285288
286- const parts : string [ ] = [ ] ;
289+ const fieldIndent = ' ' . repeat ( indent ) ;
290+ const closingBraceIndent = ' ' . repeat ( indent - 2 ) ;
291+ const rootLevelFields : string [ ] = [ ] ;
287292
288293 for ( const [ fieldName , value ] of Object . entries ( structure ) ) {
289294 if ( 'mongoType' in value ) {
290295 // It's a field mapping
291296 const fakerCall = generateFakerCall ( value as FieldMapping ) ;
292- parts . push ( ` ${ fieldName } : ${ fakerCall } ` ) ;
297+ rootLevelFields . push ( `${ fieldIndent } ${ fieldName } : ${ fakerCall } ` ) ;
293298 } else if ( 'type' in value && value . type === 'array' ) {
294299 // It's an array
295- const arrayCode = generateArrayCode ( value as ArrayStructure ) ;
296- parts . push ( ` ${ fieldName } : ${ arrayCode } ` ) ;
300+ const arrayCode = generateArrayCode ( value as ArrayStructure , indent + 2 ) ;
301+ rootLevelFields . push ( `${ fieldIndent } ${ fieldName } : ${ arrayCode } ` ) ;
297302 } else {
298303 // It's a nested object: recursive call
299- const nestedCode = generateDocumentCode ( value as DocumentStructure ) ;
300- parts . push ( ` ${ fieldName } : ${ nestedCode } ` ) ;
304+ const nestedCode = generateDocumentCode (
305+ value as DocumentStructure ,
306+ indent + 2
307+ ) ;
308+ rootLevelFields . push ( `${ fieldIndent } ${ fieldName } : ${ nestedCode } ` ) ;
301309 }
302310 }
303311
304- return `{\n${ parts . join ( ',\n' ) } \n}` ;
312+ // Handle empty objects
313+ if ( rootLevelFields . length === 0 ) {
314+ return '{}' ;
315+ }
316+
317+ return `{\n${ rootLevelFields . join ( ',\n' ) } \n${ closingBraceIndent } }` ;
305318}
306319
307320/**
308321 * Generate array code
309322 */
310- function generateArrayCode ( arrayStructure : ArrayStructure ) : string {
323+ function generateArrayCode (
324+ arrayStructure : ArrayStructure ,
325+ indent : number = 2
326+ ) : string {
311327 const elementType = arrayStructure . elementType ;
312328
313329 // Fixed length for now - TODO: make configurable
@@ -319,11 +335,17 @@ function generateArrayCode(arrayStructure: ArrayStructure): string {
319335 return `Array.from({length: ${ arrayLength } }, () => ${ fakerCall } )` ;
320336 } else if ( 'type' in elementType && elementType . type === 'array' ) {
321337 // Nested array (e.g., matrix[][])
322- const nestedArrayCode = generateArrayCode ( elementType as ArrayStructure ) ;
338+ const nestedArrayCode = generateArrayCode (
339+ elementType as ArrayStructure ,
340+ indent
341+ ) ;
323342 return `Array.from({length: ${ arrayLength } }, () => ${ nestedArrayCode } )` ;
324343 } else {
325344 // Array of objects
326- const objectCode = generateDocumentCode ( elementType as DocumentStructure ) ;
345+ const objectCode = generateDocumentCode (
346+ elementType as DocumentStructure ,
347+ indent
348+ ) ;
327349 return `Array.from({length: ${ arrayLength } }, () => ${ objectCode } )` ;
328350 }
329351}
0 commit comments