Skip to content

Commit bffd112

Browse files
author
unknown
committed
Added types for "alter relationships" functionality
1 parent 06f98b5 commit bffd112

File tree

4 files changed

+398
-45
lines changed

4 files changed

+398
-45
lines changed

forward_engineering/alterScript/alterScriptHelpers/alterRelationshipsHelper.js

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
11
const {AlterScriptDto} = require("../types/AlterScriptDto");
2+
const {
3+
AlterRelationshipDto
4+
} = require('../types/AlterRelationshipDto');
25

36

47
/**
5-
* @param relationship {Object}
8+
* @param relationship {AlterRelationshipDto}
69
* @return string
710
* */
811
const getRelationshipName = (relationship) => {
912
return relationship.role.name;
1013
}
1114

1215
/**
13-
* @return {(relationship: Object) => string}
16+
* @return {(relationship: AlterRelationshipDto) => string}
17+
* */
18+
const getFullChildTableName = (_) => (relationship) => {
19+
const {getNamePrefixedWithSchemaName} = require('../../utils/general')(_);
20+
const compMod = relationship.role.compMod;
21+
22+
const childBucketName = compMod.child.bucket.name;
23+
const childEntityName = compMod.child.collection.name;
24+
return getNamePrefixedWithSchemaName(childEntityName, childBucketName);
25+
}
26+
27+
/**
28+
* @return {(relationship: AlterRelationshipDto) => string}
1429
* */
1530
const getAddSingleForeignKeyScript = (ddlProvider, _) => (relationship) => {
1631
const compMod = relationship.role.compMod;
17-
18-
// const parentDBName = replaceSpaceWithUnderscore(compMod.parent.bucketName);
19-
// const parentEntityName = replaceSpaceWithUnderscore(compMod.parent.collectionName);
20-
// const parentTableName = getFullEntityName(parentDBName, parentEntityName);
21-
//
22-
// const childDBName = replaceSpaceWithUnderscore(compMod.child.bucketName);
23-
// const childEntityName = replaceSpaceWithUnderscore(compMod.child.collectionName);
24-
// const childTableName = getFullEntityName(childDBName, childEntityName);
25-
//
26-
// const relationshipName = compMod.name?.new || getRelationshipName(relationship) || '';
27-
//
28-
// const addFkConstraintDto = {
29-
// childTableName,
30-
// fkConstraintName: prepareName(relationshipName),
31-
// childColumns: compMod.child.fieldNames.map(name => prepareName(name)),
32-
// parentTableName,
33-
// parentColumns: compMod.parent.fieldNames.map(name => prepareName(name)),
34-
// };
35-
// return ddlProvider.addFkConstraint(addFkConstraintDto);
36-
37-
return '';
32+
33+
const relationshipName = compMod.name?.new || getRelationshipName(relationship) || '';
34+
35+
return ddlProvider.createForeignKey({
36+
name: relationshipName,
37+
foreignKey: compMod.child.collection.fkFields,
38+
primaryKey: compMod.parent.collection.fkFields,
39+
customProperties: compMod.customProperties?.new,
40+
foreignTable: compMod.child.collection.name,
41+
foreignSchemaName: compMod.child.bucket.name,
42+
foreignTableActivated: compMod.child.collection.isActivated,
43+
primaryTable: compMod.parent.collection.name,
44+
primarySchemaName: compMod.parent.bucket.name,
45+
primaryTableActivated: compMod.parent.collection.isActivated,
46+
}).statement;
3847
}
3948

4049
/**
41-
* @param relationship {Object}
50+
* @param relationship {AlterRelationshipDto}
4251
* @return boolean
4352
* */
4453
const canRelationshipBeAdded = (relationship) => {
@@ -48,17 +57,17 @@ const canRelationshipBeAdded = (relationship) => {
4857
}
4958
return [
5059
(compMod.name?.new || getRelationshipName(relationship)),
51-
compMod.parent?.bucketName,
52-
compMod.parent?.collectionName,
53-
compMod.parent?.fieldNames?.length,
54-
compMod.child?.bucketName,
55-
compMod.child?.collectionName,
56-
compMod.child?.fieldNames?.length,
60+
compMod.parent?.bucket,
61+
compMod.parent?.collection,
62+
compMod.parent?.collection?.fkFields?.length,
63+
compMod.child?.bucket,
64+
compMod.child?.collection,
65+
compMod.child?.collection?.fkFields?.length,
5766
].every(property => Boolean(property));
5867
}
5968

6069
/**
61-
* @return {(addedRelationships: Array<Object>) => Array<AlterScriptDto>}
70+
* @return {(addedRelationships: Array<AlterRelationshipDto>) => Array<AlterScriptDto>}
6271
* */
6372
const getAddForeignKeyScriptDtos = (ddlProvider, _) => (addedRelationships) => {
6473
return addedRelationships
@@ -73,35 +82,37 @@ const getAddForeignKeyScriptDtos = (ddlProvider, _) => (addedRelationships) => {
7382
}
7483

7584
/**
76-
* @return {(relationship: Object) => string}
85+
* @return {(relationship: AlterRelationshipDto) => string}
7786
* */
7887
const getDeleteSingleForeignKeyScript = (ddlProvider, _) => (relationship) => {
79-
const { getNamePrefixedWithSchemaName, wrapInQuotes } = require('../../utils/general')(_);
88+
const {wrapInQuotes} = require('../../utils/general')(_);
8089
const compMod = relationship.role.compMod;
8190

82-
const childBucketName = compMod.child.bucketName;
83-
const childEntityName = compMod.child.collectionName;
84-
const ddlChildEntityName = getNamePrefixedWithSchemaName(childEntityName, childBucketName);
91+
const ddlChildEntityName = getFullChildTableName(_)(relationship);
8592

8693
const relationshipName = compMod.name?.old || getRelationshipName(relationship) || '';
8794
const ddlRelationshipName = wrapInQuotes(relationshipName);
8895
return ddlProvider.dropForeignKey(ddlChildEntityName, ddlRelationshipName);
8996
}
9097

98+
/**
99+
* @param relationship {AlterRelationshipDto}
100+
* @return {boolean}
101+
* */
91102
const canRelationshipBeDeleted = (relationship) => {
92103
const compMod = relationship.role.compMod;
93104
if (!compMod) {
94105
return false;
95106
}
96107
return [
97108
(compMod.name?.old || getRelationshipName(relationship)),
98-
compMod.child?.bucketName,
99-
compMod.child?.collectionName,
109+
compMod.child?.bucket,
110+
compMod.child?.collection,
100111
].every(property => Boolean(property));
101112
}
102113

103114
/**
104-
* @return {(deletedRelationships: Array<Object>) => Array<AlterScriptDto>}
115+
* @return {(deletedRelationships: Array<AlterRelationshipDto>) => Array<AlterScriptDto>}
105116
* */
106117
const getDeleteForeignKeyScriptDtos = (ddlProvider, _) => (deletedRelationships) => {
107118
return deletedRelationships
@@ -116,7 +127,7 @@ const getDeleteForeignKeyScriptDtos = (ddlProvider, _) => (deletedRelationships)
116127
}
117128

118129
/**
119-
* @return {(modifiedRelationships: Array<Object>) => Array<AlterScriptDto>}
130+
* @return {(modifiedRelationships: Array<AlterRelationshipDto>) => Array<AlterScriptDto>}
120131
* */
121132
const getModifyForeignKeyScriptDtos = (ddlProvider, _) => (modifiedRelationships) => {
122133
return modifiedRelationships

0 commit comments

Comments
 (0)