MCPcopy
hub / github.com/chartdb/chartdb / getFieldExtraAttributes

Function getFieldExtraAttributes

src/lib/dbml/dbml-import/dbml-import.ts:438–516  ·  view source on GitHub ↗
(
            field: Field,
            enums: DBMLEnum[]
        )

Source from the content-addressed store, hash-verified

436 const allEnums: DBMLEnum[] = [];
437
438 const getFieldExtraAttributes = (
439 field: Field,
440 enums: DBMLEnum[]
441 ): Partial<DBMLField> => {
442 // First check if the type name itself contains the length (e.g., "character varying(50)")
443 const typeName = field.type.type_name;
444 let extractedArgs: string[] | undefined;
445
446 // Check for types with embedded length like "character varying(50)" or varchar(255)
447 const typeWithLengthMatch = typeName.match(/^(.+?)\(([^)]+)\)$/);
448 if (typeWithLengthMatch) {
449 // Extract the args from the type name itself
450 extractedArgs = typeWithLengthMatch[2]
451 .split(',')
452 .map((arg: string) => arg.trim());
453 }
454
455 // Use extracted args or fall back to field.type.args
456 const args =
457 extractedArgs ||
458 (field.type.args ? field.type.args.split(',') : undefined);
459
460 if (!args || args.length === 0) {
461 return {};
462 }
463
464 const dataType = mapDBMLTypeToDataType(field.type.type_name, {
465 ...options,
466 enums,
467 });
468
469 // Also check the preferred synonym for field attributes (e.g., decimal → numeric)
470 const preferredType = options.databaseType
471 ? getPreferredSynonym(dataType.name, options.databaseType)
472 : null;
473 const effectiveType = preferredType ?? dataType;
474
475 // Check if this is a character type that should have a max length
476 const baseTypeName = typeName
477 .replace(/\(.*\)/, '')
478 .toLowerCase()
479 .replace(/['"]/g, '');
480 const isCharType =
481 baseTypeName.includes('char') ||
482 baseTypeName.includes('varchar') ||
483 baseTypeName === 'text' ||
484 baseTypeName === 'string';
485
486 if (isCharType && args[0]) {
487 return {
488 characterMaximumLength: args[0],
489 };
490 } else if (
491 effectiveType.fieldAttributes?.precision &&
492 effectiveType.fieldAttributes?.scale
493 ) {
494 const precisionNum = args?.[0] ? parseInt(args[0]) : undefined;
495 const scaleNum = args?.[1] ? parseInt(args[1]) : undefined;

Callers 1

importDBMLToDiagramFunction · 0.85

Calls 2

getPreferredSynonymFunction · 0.90
mapDBMLTypeToDataTypeFunction · 0.85

Tested by

no test coverage detected