| 370 | }; |
| 371 | |
| 372 | export const importDBMLToDiagram = async ( |
| 373 | dbmlContent: string, |
| 374 | options: { |
| 375 | databaseType: DatabaseType; |
| 376 | } |
| 377 | ): Promise<Diagram> => { |
| 378 | try { |
| 379 | // Handle empty content |
| 380 | if (!dbmlContent.trim()) { |
| 381 | return { |
| 382 | id: generateDiagramId(), |
| 383 | name: defaultDBMLDiagramName, |
| 384 | databaseType: options?.databaseType ?? DatabaseType.GENERIC, |
| 385 | tables: [], |
| 386 | relationships: [], |
| 387 | createdAt: new Date(), |
| 388 | updatedAt: new Date(), |
| 389 | }; |
| 390 | } |
| 391 | |
| 392 | // Validate array types BEFORE preprocessing (preprocessing removes []) |
| 393 | validateArrayTypesForDatabase(dbmlContent, options.databaseType); |
| 394 | |
| 395 | const parser = new Parser(); |
| 396 | // Preprocess and sanitize DBML content |
| 397 | const { |
| 398 | content: preprocessedContent, |
| 399 | arrayFields, |
| 400 | fieldChecks, |
| 401 | tableChecks, |
| 402 | } = preprocessDBML(dbmlContent); |
| 403 | const sanitizedContent = sanitizeDBML(preprocessedContent); |
| 404 | |
| 405 | // Handle content that becomes empty after preprocessing |
| 406 | if (!sanitizedContent.trim()) { |
| 407 | return { |
| 408 | id: generateDiagramId(), |
| 409 | name: defaultDBMLDiagramName, |
| 410 | databaseType: options?.databaseType ?? DatabaseType.GENERIC, |
| 411 | tables: [], |
| 412 | relationships: [], |
| 413 | createdAt: new Date(), |
| 414 | updatedAt: new Date(), |
| 415 | }; |
| 416 | } |
| 417 | |
| 418 | const parsedData = parser.parse(sanitizedContent, 'dbmlv2'); |
| 419 | |
| 420 | // Handle case where no schemas are found |
| 421 | if (!parsedData.schemas || parsedData.schemas.length === 0) { |
| 422 | return { |
| 423 | id: generateDiagramId(), |
| 424 | name: defaultDBMLDiagramName, |
| 425 | databaseType: options?.databaseType ?? DatabaseType.GENERIC, |
| 426 | tables: [], |
| 427 | relationships: [], |
| 428 | createdAt: new Date(), |
| 429 | updatedAt: new Date(), |