MCPcopy
hub / github.com/drizzle-team/drizzle-orm / extractRelations

Function extractRelations

drizzle-kit/src/serializer/studio.ts:515–584  ·  view source on GitHub ↗
(
	tablesConfig: {
		tables: TablesRelationalConfig;
		tableNamesMap: Record<string, string>;
	},
	casing?: CasingType,
)

Source from the content-addressed store, hash-verified

513};
514
515export const extractRelations = (
516 tablesConfig: {
517 tables: TablesRelationalConfig;
518 tableNamesMap: Record<string, string>;
519 },
520 casing?: CasingType,
521): Relation[] => {
522 const relations = Object.values(tablesConfig.tables)
523 .map((it) =>
524 Object.entries(it.relations).map(([name, relation]) => {
525 try {
526 const normalized = normalizeRelation(
527 tablesConfig.tables,
528 tablesConfig.tableNamesMap,
529 relation,
530 );
531 const rel = relation;
532 const refTableName = rel.referencedTableName;
533 const refTable = rel.referencedTable;
534 const fields = normalized.fields
535 .map((it) => getColumnCasing(it, casing))
536 .flat();
537 const refColumns = normalized.references
538 .map((it) => getColumnCasing(it, casing))
539 .flat();
540
541 let refSchema: string | undefined;
542 if (is(refTable, PgTable)) {
543 refSchema = pgTableConfig(refTable).schema;
544 } else if (is(refTable, MySqlTable)) {
545 refSchema = mysqlTableConfig(refTable).schema;
546 } else if (is(refTable, SQLiteTable)) {
547 refSchema = undefined;
548 } else if (is(refTable, SingleStoreTable)) {
549 refSchema = singlestoreTableConfig(refTable).schema;
550 } else {
551 throw new Error('unsupported dialect');
552 }
553
554 let type: 'one' | 'many';
555 if (is(rel, One)) {
556 type = 'one';
557 } else if (is(rel, Many)) {
558 type = 'many';
559 } else {
560 throw new Error('unsupported relation type');
561 }
562
563 return {
564 name,
565 type,
566 table: it.dbName,
567 schema: it.schema || 'public',
568 columns: fields,
569 refTable: refTableName,
570 refSchema: refSchema || 'public',
571 refColumns: refColumns,
572 };

Callers 1

prepareServerFunction · 0.85

Calls 4

normalizeRelationFunction · 0.90
getColumnCasingFunction · 0.90
isFunction · 0.90
valuesMethod · 0.45

Tested by

no test coverage detected