|
| 1 | +const reorganizeConstraints = (attributes, entityLevel) => { |
| 2 | + return [reorganizePrimaryKeys, reorganizeUniqueKeys].reduce( |
| 3 | + ({ attributes, entityLevel }, reorganize) => reorganize(attributes, entityLevel), |
| 4 | + { attributes, entityLevel }, |
| 5 | + ); |
| 6 | +}; |
| 7 | + |
| 8 | +const reorganizePrimaryKeys = (attributes, entityLevel) => { |
| 9 | + const isSinglePrimaryKey = entityLevel.primaryKey?.[0]?.compositePrimaryKey?.length !== 1; |
| 10 | + |
| 11 | + if (isSinglePrimaryKey) { |
| 12 | + return { attributes, entityLevel }; |
| 13 | + } |
| 14 | + |
| 15 | + const primaryKey = entityLevel.primaryKey[0]; |
| 16 | + const attributeName = primaryKey.compositePrimaryKey[0]; |
| 17 | + |
| 18 | + return { |
| 19 | + attributes: setAttributesPrimaryKeyData(attributes, attributeName, primaryKey), |
| 20 | + entityLevel: clearPrimaryKeys(entityLevel), |
| 21 | + }; |
| 22 | +}; |
| 23 | + |
| 24 | +const reorganizeUniqueKeys = (attributes, entityLevel) => { |
| 25 | + return (entityLevel.uniqueKey || []).reduce( |
| 26 | + ({ attributes, entityLevel }, uniqueKey) => { |
| 27 | + const hasSingleUniqueKey = uniqueKey.compositeUniqueKey.length === 1; |
| 28 | + if (!hasSingleUniqueKey) { |
| 29 | + return { attributes, entityLevel }; |
| 30 | + } |
| 31 | + |
| 32 | + const attributeName = uniqueKey.compositeUniqueKey[0]; |
| 33 | + |
| 34 | + return { |
| 35 | + attributes: setAttributesUniqueKeyData(attributes, attributeName, uniqueKey), |
| 36 | + entityLevel: filterUniqueKeys(entityLevel, uniqueKey), |
| 37 | + }; |
| 38 | + }, |
| 39 | + { attributes, entityLevel }, |
| 40 | + ); |
| 41 | +}; |
| 42 | + |
| 43 | +const setAttributesPrimaryKeyData = (attributes, attributeName, primaryKey) => { |
| 44 | + return attributes.map(attribute => { |
| 45 | + if (attribute.name !== attributeName) { |
| 46 | + return attribute; |
| 47 | + } |
| 48 | + |
| 49 | + return setPrimaryKeyData(attribute, primaryKey); |
| 50 | + }); |
| 51 | +}; |
| 52 | + |
| 53 | +const setPrimaryKeyData = (attribute, primaryKey) => { |
| 54 | + return { |
| 55 | + ...attribute, |
| 56 | + primaryKey: true, |
| 57 | + primaryKeyOptions: getOptions(primaryKey), |
| 58 | + }; |
| 59 | +}; |
| 60 | + |
| 61 | +const setAttributesUniqueKeyData = (attributes, attributeName, uniqueKey) => { |
| 62 | + return attributes.map(attribute => { |
| 63 | + if (attribute.name !== attributeName) { |
| 64 | + return attribute; |
| 65 | + } |
| 66 | + |
| 67 | + return setUniqueKeyData(attribute, uniqueKey); |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +const setUniqueKeyData = (attribute, uniqueKey) => { |
| 72 | + return { |
| 73 | + ...attribute, |
| 74 | + unique: true, |
| 75 | + uniqueKeyOptions: getOptions(uniqueKey), |
| 76 | + }; |
| 77 | +}; |
| 78 | + |
| 79 | +const getOptions = key => { |
| 80 | + return [ |
| 81 | + { |
| 82 | + constraintName: key.constraintName, |
| 83 | + indexInclude: key.indexInclude, |
| 84 | + indexStorageParameters: key.indexStorageParameters, |
| 85 | + indexTablespace: key.indexTablespace, |
| 86 | + indexComment: key.indexComment, |
| 87 | + }, |
| 88 | + ]; |
| 89 | +}; |
| 90 | + |
| 91 | +const clearPrimaryKeys = entityLevel => { |
| 92 | + return { ...entityLevel, primaryKey: [] }; |
| 93 | +}; |
| 94 | + |
| 95 | +const filterUniqueKeys = (entityLevel, uniqueKey) => { |
| 96 | + const filteredKeys = entityLevel.uniqueKey.filter(key => key.constraintName !== uniqueKey.constraintName); |
| 97 | + |
| 98 | + return { ...entityLevel, uniqueKey: filteredKeys }; |
| 99 | +}; |
| 100 | + |
| 101 | +module.exports = { reorganizeConstraints }; |
0 commit comments