-
Notifications
You must be signed in to change notification settings - Fork 0
/
RdbmsFkStitcher.ts
60 lines (56 loc) · 2.62 KB
/
RdbmsFkStitcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { AbstractSingularBuilder } from '../../../runtime/AbstractSingularBuilder'
import { Artifact } from '../../../system/Artifact'
import { System } from '../../../system/System'
import { SystemComponent } from '../../../system/SystemComponent'
import { ValueType } from '../../../system/ValueType'
import { ForeignKey } from '../ForeignKey'
import { RelationalDatabase } from '../RelationalDatabase'
import { Schema } from '../Schema'
import { Table } from '../Table'
export class RdbmsFkStitcher extends AbstractSingularBuilder {
constructor(configurationValues?: { [key: string]: any }) {
const nonNullConfigurationValues = configurationValues ? configurationValues : []
nonNullConfigurationValues['repeatFor'] = JSON.stringify({
objectTypeName: 'ForeignKey',
})
super(
'A transformer that collects foreign keys and ensures their relationships and names are rationalized with their related tables',
{
repeatFor: {
name: 'Repeat for',
required: true,
description:
'A parsable string of JSON that represents the properties values of the component that should be iterated on, review the documentation for SystemDescendantComponent and derivations for most available for use',
valueType: ValueType.string,
defaultValue: ``,
},
},
nonNullConfigurationValues
)
}
async buildInternal(system: System, component: SystemComponent): Promise<Artifact[]> {
const fk = component as ForeignKey
const schema = fk.parent as Schema
const sourceTableName = SystemComponent.fullConstantCase(schema.fullConstantCaseName, fk.sourceTableName)
const sourceTable = system.descendants[sourceTableName] as Table
if (sourceTable != null) {
const fkColumn = sourceTable.columns[System.constantCase(fk.sourceColumnName)]
sourceTable.addChild(fk)
if (fkColumn != null) {
fkColumn.foreignKey = fk
fk.sourceColumn = fkColumn
fk.sourceTable = sourceTable
} else this.logger.warn(`FK ${fk.constantCaseFullName} has no matching column ${fk.sourceColumnName}`)
} else this.logger.warn(`FK ${fk.constantCaseFullName} has no matching table ${sourceTableName}`)
// target could be in a different schema
const db = schema.parent as RelationalDatabase
const fullTargetSchemaName = SystemComponent.fullConstantCase(db.fullConstantCaseName, fk.targetSchemaName)
const targetSchema = system.descendants[fullTargetSchemaName] as Table
const fullTargetTableName = SystemComponent.fullConstantCase(targetSchema.fullConstantCaseName, fk.targetTableName)
const targetTable = system.descendants[fullTargetTableName] as Table
if (targetTable != null) {
fk.targetTable = targetTable
}
return []
}
}