-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultValueHelper.js
More file actions
118 lines (110 loc) · 4.13 KB
/
defaultValueHelper.js
File metadata and controls
118 lines (110 loc) · 4.13 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const { toPairs } = require('lodash');
const { AlterScriptDto } = require('../../types/AlterScriptDto');
const {
getFullCollectionName,
wrapInQuotes,
isObjectInDeltaModelActivated,
isParentContainerActivated,
getSchemaOfAlterCollection,
} = require('../../../utils/general');
const { assignTemplates } = require('../../../utils/assignTemplates');
const templates = require('../../../ddlProvider/templates');
/**
* @param {Object} props
* @param {string} props.tableName
* @param {string} props.columnName
* @param {string} props.defaultValue
* @return string
*/
const updateColumnDefaultValue = ({ tableName, columnName, defaultValue }) => {
const templateConfig = {
tableName,
columnName,
defaultValue,
};
return assignTemplates({ template: templates.updateColumnDefaultValue, templateData: templateConfig });
};
/**
* @param {Object} props
* @param {Object} props.collection
* @returns { Array<AlterScriptDto> }
*/
const getUpdatedDefaultColumnValueScriptDtos = ({ collection }) => {
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);
const collectionSchema = getSchemaOfAlterCollection(collection);
return toPairs(collection.properties)
.filter(([_name, jsonSchema]) => {
const newDefault = jsonSchema.default;
const oldName = jsonSchema.compMod.oldField.name;
const oldDefault = collection.role.properties[oldName]?.default;
return newDefault !== undefined && (!oldDefault || newDefault !== oldDefault);
})
.map(([columnName, jsonSchema]) => {
const newDefaultValue = jsonSchema.default;
const scriptGenerationConfig = {
tableName: getFullCollectionName({ collectionSchema }),
columnName: wrapInQuotes(columnName),
defaultValue: newDefaultValue,
};
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: updateColumnDefaultValue(scriptGenerationConfig), isActivated };
})
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, false))
.filter(Boolean);
};
/**
* @param {Object} props
* @param {string} props.tableName
* @param {string} props.columnName
* @return string
*/
const dropColumnDefaultValue = ({ tableName, columnName }) => {
const templateConfig = {
tableName,
columnName,
};
return assignTemplates({ template: templates.dropColumnDefaultValue, templateData: templateConfig });
};
/**
* @param {Object} props
* @param {Object} props.collection
* @returns { Array<AlterScriptDto> }
*/
const getDeletedDefaultColumnValueScriptDtos = ({ collection }) => {
const isContainerActivated = isParentContainerActivated(collection);
const isCollectionActivated = isObjectInDeltaModelActivated(collection);
const collectionSchema = getSchemaOfAlterCollection(collection);
return toPairs(collection.properties)
.filter(([_name, jsonSchema]) => {
const newDefault = jsonSchema.default;
const oldName = jsonSchema.compMod.oldField.name;
const oldDefault = collection.role.properties[oldName]?.default;
const hasPrevValue = oldDefault !== undefined;
const hasNewValue = newDefault !== undefined;
return hasPrevValue && !hasNewValue;
})
.map(([columnName, jsonSchema]) => {
const scriptGenerationConfig = {
tableName: getFullCollectionName({ collectionSchema }),
columnName: wrapInQuotes(columnName),
};
const isActivated = isContainerActivated && isCollectionActivated && jsonSchema.isActivated;
return { script: dropColumnDefaultValue(scriptGenerationConfig), isActivated };
})
.map(({ script, isActivated }) => AlterScriptDto.getInstance([script], isActivated, true))
.filter(Boolean);
};
/**
* @param {Object} props
* @param {Object} props.collection
* @returns { Array<AlterScriptDto> }
*/
const getModifiedDefaultColumnValueScriptDtos = ({ collection }) => {
const updatedDefaultValuesScriptDtos = getUpdatedDefaultColumnValueScriptDtos({ collection });
const dropDefaultValuesScriptDtos = getDeletedDefaultColumnValueScriptDtos({ collection });
return [...updatedDefaultValuesScriptDtos, ...dropDefaultValuesScriptDtos];
};
module.exports = {
getModifiedDefaultColumnValueScriptDtos,
};