|
1 | 1 | module.exports = ({ |
2 | | - _, |
3 | | - commentIfDeactivated, |
4 | | - checkAllKeysDeactivated, |
5 | | - assignTemplates, |
6 | | - wrapInQuotes, |
7 | | - getColumnsList, |
8 | | -}) => { |
9 | | - const generateConstraintsString = (dividedConstraints, isParentActivated) => { |
10 | | - const deactivatedItemsAsString = commentIfDeactivated( |
11 | | - (dividedConstraints?.deactivatedItems || []).join(',\n\t'), |
12 | | - { |
13 | | - isActivated: !isParentActivated, |
14 | | - isPartOfLine: true, |
15 | | - }, |
16 | | - ); |
17 | | - const activatedConstraints = dividedConstraints?.activatedItems?.length |
18 | | - ? ',\n\t' + dividedConstraints.activatedItems.join(',\n\t') |
19 | | - : ''; |
| 2 | + _, |
| 3 | + commentIfDeactivated, |
| 4 | + checkAllKeysDeactivated, |
| 5 | + assignTemplates, |
| 6 | + wrapInQuotes, |
| 7 | + getColumnsList, |
| 8 | + }) => { |
| 9 | + const generateConstraintsString = (dividedConstraints, isParentActivated) => { |
| 10 | + const deactivatedItemsAsString = commentIfDeactivated( |
| 11 | + (dividedConstraints?.deactivatedItems || []).join(',\n\t'), |
| 12 | + { |
| 13 | + isActivated: !isParentActivated, |
| 14 | + isPartOfLine: true, |
| 15 | + }, |
| 16 | + ); |
| 17 | + const activatedConstraints = dividedConstraints?.activatedItems?.length |
| 18 | + ? ',\n\t' + dividedConstraints.activatedItems.join(',\n\t') |
| 19 | + : ''; |
20 | 20 |
|
21 | | - const deactivatedConstraints = dividedConstraints?.deactivatedItems?.length |
22 | | - ? '\n\t' + deactivatedItemsAsString |
23 | | - : ''; |
| 21 | + const deactivatedConstraints = dividedConstraints?.deactivatedItems?.length |
| 22 | + ? '\n\t' + deactivatedItemsAsString |
| 23 | + : ''; |
24 | 24 |
|
25 | | - return activatedConstraints + deactivatedConstraints; |
26 | | - }; |
| 25 | + return activatedConstraints + deactivatedConstraints; |
| 26 | + }; |
27 | 27 |
|
28 | | - const foreignKeysToString = keys => { |
29 | | - if (Array.isArray(keys)) { |
30 | | - const activatedKeys = keys |
31 | | - .filter(key => _.get(key, 'isActivated', true)) |
32 | | - .map(key => wrapInQuotes(_.trim(key.name))); |
33 | | - const deactivatedKeys = keys |
34 | | - .filter(key => !_.get(key, 'isActivated', true)) |
35 | | - .map(key => wrapInQuotes(_.trim(key.name))); |
36 | | - const deactivatedKeysAsString = deactivatedKeys.length |
37 | | - ? commentIfDeactivated(deactivatedKeys, { isActivated: false, isPartOfLine: true }) |
38 | | - : ''; |
| 28 | + const foreignKeysToString = keys => { |
| 29 | + if (Array.isArray(keys)) { |
| 30 | + const activatedKeys = keys |
| 31 | + .filter(key => _.get(key, 'isActivated', true)) |
| 32 | + .map(key => wrapInQuotes(_.trim(key.name))); |
| 33 | + const deactivatedKeys = keys |
| 34 | + .filter(key => !_.get(key, 'isActivated', true)) |
| 35 | + .map(key => wrapInQuotes(_.trim(key.name))); |
| 36 | + const deactivatedKeysAsString = deactivatedKeys.length |
| 37 | + ? commentIfDeactivated(deactivatedKeys, {isActivated: false, isPartOfLine: true}) |
| 38 | + : ''; |
39 | 39 |
|
40 | | - return activatedKeys.join(', ') + deactivatedKeysAsString; |
41 | | - } |
42 | | - return keys; |
43 | | - }; |
| 40 | + return activatedKeys.join(', ') + deactivatedKeysAsString; |
| 41 | + } |
| 42 | + return keys; |
| 43 | + }; |
44 | 44 |
|
45 | | - const foreignActiveKeysToString = keys => { |
46 | | - return keys.map(key => _.trim(key.name)).join(', '); |
47 | | - }; |
| 45 | + const foreignActiveKeysToString = keys => { |
| 46 | + return keys.map(key => _.trim(key.name)).join(', '); |
| 47 | + }; |
48 | 48 |
|
49 | | - const createKeyConstraint = (templates, isParentActivated) => keyData => { |
50 | | - const constraintName = wrapInQuotes(_.trim(keyData.name)); |
51 | | - const isAllColumnsDeactivated = checkAllKeysDeactivated(keyData.columns || []); |
52 | | - const columns = !_.isEmpty(keyData.columns) |
53 | | - ? getColumnsList(keyData.columns, isAllColumnsDeactivated, isParentActivated) |
54 | | - : ''; |
55 | | - const includeNonKey = keyData.include.length |
56 | | - ? ` INCLUDE${getColumnsList(keyData.include, isAllColumnsDeactivated, isParentActivated)}` |
57 | | - : ''; |
58 | | - const storageParameters = keyData.storageParameters ? ` WITH (${keyData.storageParameters})` : ''; |
59 | | - const tablespace = keyData.tablespace ? ` USING INDEX TABLESPACE ${wrapInQuotes(keyData.tablespace)}` : ''; |
| 49 | + /** |
| 50 | + * @param templates {Object} |
| 51 | + * @param isParentActivated {boolean} |
| 52 | + * @return {( |
| 53 | + * keyData: { |
| 54 | + * name: string, |
| 55 | + * columns: Array<{ |
| 56 | + * isActivated: boolean, |
| 57 | + * name: string, |
| 58 | + * }>, |
| 59 | + * include: Array<{ |
| 60 | + * isActivated: boolean, |
| 61 | + * name: string, |
| 62 | + * }>, |
| 63 | + * storageParameters: string, |
| 64 | + * tablespace: string, |
| 65 | + * } |
| 66 | + * ) => { |
| 67 | + * statement: string, |
| 68 | + * isActivated: boolean, |
| 69 | + * }} |
| 70 | + * */ |
| 71 | + const createKeyConstraint = (templates, isParentActivated) => keyData => { |
| 72 | + const constraintName = wrapInQuotes(_.trim(keyData.name)); |
| 73 | + const isAllColumnsDeactivated = checkAllKeysDeactivated(keyData.columns || []); |
| 74 | + const columns = !_.isEmpty(keyData.columns) |
| 75 | + ? getColumnsList(keyData.columns, isAllColumnsDeactivated, isParentActivated) |
| 76 | + : ''; |
| 77 | + const includeNonKey = keyData.include.length |
| 78 | + ? ` INCLUDE${getColumnsList(keyData.include, isAllColumnsDeactivated, isParentActivated)}` |
| 79 | + : ''; |
| 80 | + const storageParameters = keyData.storageParameters ? ` WITH (${keyData.storageParameters})` : ''; |
| 81 | + const tablespace = keyData.tablespace ? ` USING INDEX TABLESPACE ${wrapInQuotes(keyData.tablespace)}` : ''; |
60 | 82 |
|
61 | | - return { |
62 | | - statement: assignTemplates(templates.createKeyConstraint, { |
63 | | - constraintName: keyData.name ? `CONSTRAINT ${wrapInQuotes(constraintName)} ` : '', |
64 | | - keyType: keyData.keyType, |
65 | | - columns, |
66 | | - includeNonKey, |
67 | | - storageParameters, |
68 | | - tablespace, |
69 | | - }), |
70 | | - isActivated: !isAllColumnsDeactivated, |
71 | | - }; |
72 | | - }; |
| 83 | + return { |
| 84 | + statement: assignTemplates(templates.createKeyConstraint, { |
| 85 | + constraintName: keyData.name ? `CONSTRAINT ${wrapInQuotes(constraintName)} ` : '', |
| 86 | + keyType: keyData.keyType, |
| 87 | + columns, |
| 88 | + includeNonKey, |
| 89 | + storageParameters, |
| 90 | + tablespace, |
| 91 | + }), |
| 92 | + isActivated: !isAllColumnsDeactivated, |
| 93 | + }; |
| 94 | + }; |
73 | 95 |
|
74 | | - const getConstraintsWarnings = (invalidConstraints = []) => { |
75 | | - if (_.isEmpty(invalidConstraints)) { |
76 | | - return ''; |
77 | | - } |
| 96 | + const getConstraintsWarnings = (invalidConstraints = []) => { |
| 97 | + if (_.isEmpty(invalidConstraints)) { |
| 98 | + return ''; |
| 99 | + } |
78 | 100 |
|
79 | | - return ( |
80 | | - '\n\t' + |
81 | | - invalidConstraints |
82 | | - .map(keyData => { |
83 | | - const constraintName = keyData.name ? ` [constraint name: ${keyData.name}]` : ''; |
| 101 | + return ( |
| 102 | + '\n\t' + |
| 103 | + invalidConstraints |
| 104 | + .map(keyData => { |
| 105 | + const constraintName = keyData.name ? ` [constraint name: ${keyData.name}]` : ''; |
84 | 106 |
|
85 | | - return `-- ${keyData.errorMessage}${constraintName}`; |
86 | | - }) |
87 | | - .join('\n\t') |
88 | | - ); |
89 | | - }; |
| 107 | + return `-- ${keyData.errorMessage}${constraintName}`; |
| 108 | + }) |
| 109 | + .join('\n\t') |
| 110 | + ); |
| 111 | + }; |
90 | 112 |
|
91 | | - const additionalPropertiesForForeignKey = relationship => { |
92 | | - const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', ''); |
93 | | - const foreignOnUpdate = _.get(relationship, 'relationshipOnUpdate', ''); |
94 | | - const foreignMatch = _.get(relationship, 'relationshipMatch', ''); |
95 | | - return { foreignOnDelete, foreignOnUpdate, foreignMatch }; |
96 | | - }; |
| 113 | + const additionalPropertiesForForeignKey = relationship => { |
| 114 | + const foreignOnDelete = _.get(relationship, 'relationshipOnDelete', ''); |
| 115 | + const foreignOnUpdate = _.get(relationship, 'relationshipOnUpdate', ''); |
| 116 | + const foreignMatch = _.get(relationship, 'relationshipMatch', ''); |
| 117 | + return {foreignOnDelete, foreignOnUpdate, foreignMatch}; |
| 118 | + }; |
97 | 119 |
|
98 | | - return { |
99 | | - generateConstraintsString, |
100 | | - foreignKeysToString, |
101 | | - foreignActiveKeysToString, |
102 | | - createKeyConstraint, |
103 | | - getConstraintsWarnings, |
104 | | - additionalPropertiesForForeignKey, |
105 | | - }; |
| 120 | + return { |
| 121 | + generateConstraintsString, |
| 122 | + foreignKeysToString, |
| 123 | + foreignActiveKeysToString, |
| 124 | + createKeyConstraint, |
| 125 | + getConstraintsWarnings, |
| 126 | + additionalPropertiesForForeignKey, |
| 127 | + }; |
106 | 128 | }; |
0 commit comments