-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcolumnDefinitionHelper.js
More file actions
79 lines (69 loc) · 2.57 KB
/
columnDefinitionHelper.js
File metadata and controls
79 lines (69 loc) · 2.57 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
const _ = require('lodash');
const { DbVersion } = require('../../enums/DbVersion');
const { getColumnDefault } = require('../ddlHelpers/columnDefinitionHelpers/getColumnDefault');
/**
* @param dbVersion {string} DB version in "21&i" format
* @return {boolean}
* */
const shouldUseClobForJsonColumns = dbVersion => {
const dbVersionAsNumber = Number.parseInt(dbVersion, 10);
return dbVersionAsNumber < DbVersion.JSON_TYPE_SINCE;
};
module.exports = ({ assignTemplates, templates, commentIfDeactivated, wrapComment, prepareName }) => {
const { getOptionsString } = require('./constraintHelper')({ prepareName });
const getColumnComments = (tableName, columnDefinitions) => {
return _.chain(columnDefinitions)
.filter('comment')
.map(columnData => {
const comment = assignTemplates(templates.comment, {
object: 'COLUMN',
objectName: `${tableName}.${prepareName(columnData.name)}`,
comment: wrapComment(columnData.comment),
});
return commentIfDeactivated(comment, columnData);
})
.join('\n')
.value();
};
const getColumnConstraints = ({ nullable, unique, primaryKey, primaryKeyOptions, uniqueKeyOptions }) => {
const { constraintString, statement } = getOptionsString(
getOptions({ primaryKey, unique, primaryKeyOptions, uniqueKeyOptions }),
);
const primaryKeyString = primaryKey ? ` PRIMARY KEY` : '';
const uniqueKeyString = unique ? ` UNIQUE` : '';
const nullableString = nullable || primaryKey ? '' : ' NOT NULL';
return `${nullableString}${constraintString}${primaryKeyString}${uniqueKeyString}${statement}`;
};
const getOptions = ({ primaryKey, unique, primaryKeyOptions, uniqueKeyOptions }) => {
if (primaryKey) {
return primaryKeyOptions || {};
} else if (unique) {
return uniqueKeyOptions || {};
} else {
return {};
}
};
const replaceTypeByVersion = (type, version) => {
if (type === 'JSON' && shouldUseClobForJsonColumns(version)) {
return 'CLOB';
}
return type;
};
const getColumnEncrypt = ({ encryption }) => {
if (_.isPlainObject(encryption) && !_.isEmpty(_.omit(encryption, 'id'))) {
const { ENCRYPTION_ALGORITHM, INTEGRITY_ALGORITHM, noSalt } = encryption;
const encryptionAlgorithm = ENCRYPTION_ALGORITHM ? ` USING '${ENCRYPTION_ALGORITHM}'` : '';
const integrityAlgorithm = INTEGRITY_ALGORITHM ? ` '${INTEGRITY_ALGORITHM}'` : '';
const salt = noSalt ? ' NO SALT' : '';
return ` ENCRYPT${encryptionAlgorithm}${integrityAlgorithm}${salt}`;
}
return '';
};
return {
getColumnComments,
getColumnConstraints,
replaceTypeByVersion,
getColumnDefault,
getColumnEncrypt,
};
};