|
1 | | -module.exports = app => { |
2 | | - const _ = app.require('lodash'); |
| 1 | +const _ = require('lodash'); |
3 | 2 |
|
4 | | - const checkFieldPropertiesChanged = (compMod, propertiesToCheck) => { |
5 | | - return propertiesToCheck.some(prop => compMod?.oldField[prop] !== compMod?.newField[prop]); |
6 | | - }; |
| 3 | +const checkFieldPropertiesChanged = (compMod, propertiesToCheck) => { |
| 4 | + return propertiesToCheck.some(prop => compMod?.oldField[prop] !== compMod?.newField[prop]); |
| 5 | +}; |
7 | 6 |
|
8 | | - const modifyGroupItems = ({ data, key, hydrate, drop, create, filter = Boolean }) => { |
9 | | - const compMod = getCompMod(data); |
10 | | - const parentName = data.code || data.name || data.collectionName; |
| 7 | +const modifyGroupItems = ({ data, key, hydrate, drop, create, filter = Boolean }) => { |
| 8 | + const compMod = getCompMod(data); |
| 9 | + const parentName = data.code || data.name || data.collectionName; |
11 | 10 |
|
12 | | - const { removed, added, modified } = getModifiedGroupItems(compMod[key] || {}, hydrate, filter); |
| 11 | + const { removed, added, modified } = getModifiedGroupItems(compMod[key] || {}, hydrate, filter); |
13 | 12 |
|
14 | | - const removedScripts = removed.map(item => drop(item, parentName)); |
15 | | - const addedScripts = added.map(item => create(item, parentName)); |
16 | | - const modifiedScripts = modified.map(item => create({ ...item, orReplace: true }, parentName)); |
| 13 | + const removedScripts = removed.map(item => drop(item, parentName)); |
| 14 | + const addedScripts = added.map(item => create(item, parentName)); |
| 15 | + const modifiedScripts = modified.map(item => create({ ...item, orReplace: true }, parentName)); |
17 | 16 |
|
18 | | - return [].concat(modifiedScripts).concat(removedScripts).concat(addedScripts).filter(Boolean).join('\n\n'); |
19 | | - }; |
| 17 | + return [].concat(modifiedScripts).concat(removedScripts).concat(addedScripts).filter(Boolean).join('\n\n'); |
| 18 | +}; |
20 | 19 |
|
21 | | - const getModifiedGroupItems = ({ new: newItems = [], old: oldItems = [] }, hydrate, filter) => { |
22 | | - const oldHydrated = oldItems.map(hydrate).filter(filter); |
23 | | - const newHydrated = newItems.map(hydrate).filter(filter); |
24 | | - |
25 | | - const { removed, added, modified } = oldHydrated.reduce( |
26 | | - (accumulator, oldItem) => { |
27 | | - const newItem = newHydrated.find(item => item.name === oldItem.name); |
28 | | - const itemsAreNotEqual = !isGroupItemsEqual(newItem, oldItem); |
29 | | - |
30 | | - if (!newItem) { |
31 | | - return { |
32 | | - removed: [...accumulator.removed, oldItem], |
33 | | - modified: accumulator.modified, |
34 | | - added: accumulator.added, |
35 | | - }; |
36 | | - } |
37 | | - |
38 | | - if (itemsAreNotEqual) { |
39 | | - return { |
40 | | - removed: accumulator.removed, |
41 | | - modified: [...accumulator.modified, newItem], |
42 | | - added: accumulator.added, |
43 | | - }; |
44 | | - } |
45 | | - |
46 | | - return accumulator; |
47 | | - }, |
48 | | - { |
49 | | - removed: [], |
50 | | - modified: [], |
51 | | - added: newHydrated.filter(newItem => !oldHydrated.some(item => item.name === newItem.name)), |
52 | | - }, |
53 | | - ); |
| 20 | +const getModifiedGroupItems = ({ new: newItems = [], old: oldItems = [] }, hydrate, filter) => { |
| 21 | + const oldHydrated = oldItems.map(hydrate).filter(filter); |
| 22 | + const newHydrated = newItems.map(hydrate).filter(filter); |
| 23 | + |
| 24 | + const { removed, added, modified } = oldHydrated.reduce( |
| 25 | + (accumulator, oldItem) => { |
| 26 | + const newItem = newHydrated.find(item => item.name === oldItem.name); |
| 27 | + const itemsAreNotEqual = !isGroupItemsEqual(newItem, oldItem); |
| 28 | + |
| 29 | + if (!newItem) { |
| 30 | + return { |
| 31 | + removed: [...accumulator.removed, oldItem], |
| 32 | + modified: accumulator.modified, |
| 33 | + added: accumulator.added, |
| 34 | + }; |
| 35 | + } |
54 | 36 |
|
55 | | - return { removed, added, modified }; |
56 | | - }; |
| 37 | + if (itemsAreNotEqual) { |
| 38 | + return { |
| 39 | + removed: accumulator.removed, |
| 40 | + modified: [...accumulator.modified, newItem], |
| 41 | + added: accumulator.added, |
| 42 | + }; |
| 43 | + } |
57 | 44 |
|
58 | | - const isGroupItemsEqual = (leftItem, rightItem) => _.isEqual(leftItem, rightItem); |
59 | | - |
60 | | - const getCompMod = containerData => containerData.role?.compMod ?? {}; |
61 | | - |
62 | | - const checkCompModEqual = ({ new: newItem, old: oldItem } = {}) => _.isEqual(newItem, oldItem); |
63 | | - |
64 | | - const setTableKeys = (idToNameHashTable, idToActivatedHashTable, table) => { |
65 | | - return { |
66 | | - ...table, |
67 | | - distKey: [ |
68 | | - { |
69 | | - ...(table.distKey?.[0] ?? {}), |
70 | | - compositeDistKey: |
71 | | - table.distKey?.[0]?.compositeDistKey?.map(key => ({ |
72 | | - ...key, |
73 | | - name: idToNameHashTable[key.keyId], |
74 | | - isActivated: idToActivatedHashTable[key.keyId], |
75 | | - })) || [], |
76 | | - }, |
77 | | - ], |
78 | | - sortKey: [ |
79 | | - { |
80 | | - ...(table.sortKey?.[0] ?? {}), |
81 | | - compositeSortKey: |
82 | | - table.sortKey?.[0]?.compositeSortKey?.map(key => ({ |
83 | | - ...key, |
84 | | - name: idToNameHashTable[key.keyId], |
85 | | - isActivated: idToActivatedHashTable[key.keyId], |
86 | | - })) || [], |
87 | | - }, |
88 | | - ], |
89 | | - primaryKey: [ |
90 | | - { |
91 | | - ...(table.primaryKey?.[0] ?? {}), |
92 | | - compositePrimaryKey: |
93 | | - table.primaryKey?.[0]?.compositePrimaryKey?.map(key => ({ |
94 | | - ...key, |
95 | | - name: idToNameHashTable[key.keyId], |
96 | | - isActivated: idToActivatedHashTable[key.keyId], |
97 | | - })) || [], |
98 | | - }, |
99 | | - ], |
100 | | - uniqueKey: [ |
101 | | - { |
102 | | - ...(table.uniqueKey?.[0] ?? {}), |
103 | | - compositeUniqueKey: |
104 | | - table.uniqueKey?.[0]?.compositeUniqueKey?.map(key => ({ |
105 | | - ...key, |
106 | | - name: idToNameHashTable[key.keyId], |
107 | | - isActivated: idToActivatedHashTable[key.keyId], |
108 | | - })) || [], |
109 | | - }, |
110 | | - ], |
111 | | - }; |
112 | | - }; |
| 45 | + return accumulator; |
| 46 | + }, |
| 47 | + { |
| 48 | + removed: [], |
| 49 | + modified: [], |
| 50 | + added: newHydrated.filter(newItem => !oldHydrated.some(item => item.name === newItem.name)), |
| 51 | + }, |
| 52 | + ); |
113 | 53 |
|
114 | | - const getKeys = keys => _.map(keys, 'name'); |
| 54 | + return { removed, added, modified }; |
| 55 | +}; |
115 | 56 |
|
116 | | - const setNewProperties = jsonSchema => { |
117 | | - const compMod = getCompMod(jsonSchema); |
| 57 | +const isGroupItemsEqual = (leftItem, rightItem) => _.isEqual(leftItem, rightItem); |
118 | 58 |
|
119 | | - return _.toPairs(compMod).reduce((jsonSchema, [key, compMod]) => { |
120 | | - if (compMod.new) { |
121 | | - return { ...jsonSchema, [key]: compMod.new }; |
122 | | - } |
| 59 | +const getCompMod = containerData => containerData.role?.compMod ?? {}; |
123 | 60 |
|
124 | | - return jsonSchema; |
125 | | - }, jsonSchema); |
126 | | - }; |
| 61 | +const checkCompModEqual = ({ new: newItem, old: oldItem } = {}) => _.isEqual(newItem, oldItem); |
127 | 62 |
|
| 63 | +const setTableKeys = (idToNameHashTable, idToActivatedHashTable, table) => { |
128 | 64 | return { |
129 | | - checkFieldPropertiesChanged, |
130 | | - getCompMod, |
131 | | - modifyGroupItems, |
132 | | - checkCompModEqual, |
133 | | - setTableKeys, |
134 | | - getKeys, |
135 | | - setNewProperties, |
| 65 | + ...table, |
| 66 | + distKey: [ |
| 67 | + { |
| 68 | + ...(table.distKey?.[0] ?? {}), |
| 69 | + compositeDistKey: |
| 70 | + table.distKey?.[0]?.compositeDistKey?.map(key => ({ |
| 71 | + ...key, |
| 72 | + name: idToNameHashTable[key.keyId], |
| 73 | + isActivated: idToActivatedHashTable[key.keyId], |
| 74 | + })) || [], |
| 75 | + }, |
| 76 | + ], |
| 77 | + sortKey: [ |
| 78 | + { |
| 79 | + ...(table.sortKey?.[0] ?? {}), |
| 80 | + compositeSortKey: |
| 81 | + table.sortKey?.[0]?.compositeSortKey?.map(key => ({ |
| 82 | + ...key, |
| 83 | + name: idToNameHashTable[key.keyId], |
| 84 | + isActivated: idToActivatedHashTable[key.keyId], |
| 85 | + })) || [], |
| 86 | + }, |
| 87 | + ], |
| 88 | + primaryKey: [ |
| 89 | + { |
| 90 | + ...(table.primaryKey?.[0] ?? {}), |
| 91 | + compositePrimaryKey: |
| 92 | + table.primaryKey?.[0]?.compositePrimaryKey?.map(key => ({ |
| 93 | + ...key, |
| 94 | + name: idToNameHashTable[key.keyId], |
| 95 | + isActivated: idToActivatedHashTable[key.keyId], |
| 96 | + })) || [], |
| 97 | + }, |
| 98 | + ], |
| 99 | + uniqueKey: [ |
| 100 | + { |
| 101 | + ...(table.uniqueKey?.[0] ?? {}), |
| 102 | + compositeUniqueKey: |
| 103 | + table.uniqueKey?.[0]?.compositeUniqueKey?.map(key => ({ |
| 104 | + ...key, |
| 105 | + name: idToNameHashTable[key.keyId], |
| 106 | + isActivated: idToActivatedHashTable[key.keyId], |
| 107 | + })) || [], |
| 108 | + }, |
| 109 | + ], |
136 | 110 | }; |
137 | 111 | }; |
| 112 | + |
| 113 | +const getKeys = keys => _.map(keys, 'name'); |
| 114 | + |
| 115 | +const setNewProperties = jsonSchema => { |
| 116 | + const compMod = getCompMod(jsonSchema); |
| 117 | + |
| 118 | + return _.toPairs(compMod).reduce((jsonSchema, [key, compMod]) => { |
| 119 | + if (compMod.new) { |
| 120 | + return { ...jsonSchema, [key]: compMod.new }; |
| 121 | + } |
| 122 | + |
| 123 | + return jsonSchema; |
| 124 | + }, jsonSchema); |
| 125 | +}; |
| 126 | + |
| 127 | +module.exports = { |
| 128 | + checkFieldPropertiesChanged, |
| 129 | + getCompMod, |
| 130 | + modifyGroupItems, |
| 131 | + checkCompModEqual, |
| 132 | + setTableKeys, |
| 133 | + getKeys, |
| 134 | + setNewProperties, |
| 135 | +}; |
0 commit comments