-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgeneral.js
More file actions
246 lines (200 loc) · 6.13 KB
/
general.js
File metadata and controls
246 lines (200 loc) · 6.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'use strict';
module.exports = _ => {
const sqlFormatter = require('sql-formatter');
const { RESERVED_WORDS_AS_ARRAY } = require('../enums/reservedWords');
/**
* @typedef {((args: any) => string) | ((args: any) => ChainFunction)} ChainFunction
* */
/**
* @return {ChainFunction}
* */
const buildStatement = (mainStatement, isActivated) => {
let composeStatements = (...statements) => {
return statements.reduce((result, statement) => result + statement, mainStatement);
};
const chain = (...args) => {
if (args.length) {
composeStatements = composeStatements.bind(null, getStatement(...args));
return chain;
}
return commentDeactivatedStatements(composeStatements(), isActivated);
};
/**
* @param condition {boolean}
* @param statement {string}
* @return {string}
* */
const getStatement = (condition, statement) => {
if (condition && statement === ')') {
return '\n)';
}
if (statement === ';') {
return statement;
}
if (condition) {
return '\n' + indentString(statement);
}
return '';
};
return chain;
};
const isEscaped = name => /`[\s\S]*`/.test(name);
const prepareName = (name = '') => {
const containSpaces = /[\s-]/g;
if (containSpaces.test(name) && !isEscaped(name)) {
return `\`${name}\``;
} else if (RESERVED_WORDS_AS_ARRAY.includes(name.toUpperCase())) {
return `\`${name}\``;
} else if (name === '') {
return '';
} else if (!isNaN(name)) {
return `\`${name}\``;
}
return name;
};
const replaceSpaceWithUnderscore = (name = '') => {
return name.replace(/\s/g, '_');
};
const getFullTableName = collection => {
const collectionSchema = { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
const tableName = getEntityName(collectionSchema);
const schemaName = collectionSchema.compMod?.keyspaceName;
return getNamePrefixedWithSchemaName(tableName, schemaName);
};
const getFullCollectionName = collectionSchema => {
const collectionName = getEntityName(collectionSchema);
const bucketName = collectionSchema.compMod?.keyspaceName;
return getNamePrefixedWithSchemaName(collectionName, bucketName);
};
const getName = entity => entity.code || entity.collectionName || entity.name || '';
const getRelationshipName = relationship => relationship.name || '';
const getTab = (tabNum, configData) => (Array.isArray(configData) ? configData[tabNum] || {} : {});
const indentString = (str, tab = 4) =>
(str || '')
.split('\n')
.map(s => ' '.repeat(tab) + s)
.join('\n');
const descriptors = {};
const getTypeDescriptor = typeName => {
if (descriptors[typeName]) {
return descriptors[typeName];
}
try {
descriptors[typeName] = require(`../../types/${typeName}.json`);
return descriptors[typeName];
} catch (e) {
return {};
}
};
const getNamePrefixedWithSchemaName = (name, schemaName) => {
if (schemaName) {
return `${wrapInBrackets(schemaName)}.${wrapInBrackets(name)}`;
}
return wrapInBrackets(name);
};
/**
* @param statement {string}
* @param isActivated {boolean}
* @return {string}
* */
const commentDeactivatedStatements = (statement, isActivated = true) => {
if (isActivated) {
return statement;
}
const insertBeforeEachLine = (statement, insertValue) =>
statement
.split('\n')
.map(line => `${insertValue}${line}`)
.join('\n');
return insertBeforeEachLine(statement, '-- ');
};
const commentDeactivatedInlineKeys = (keys, deactivatedKeyNames) => {
const [activatedKeys, deactivatedKeys] = _.partition(
keys,
key => !(deactivatedKeyNames.has(key) || deactivatedKeyNames.has(key.slice(1, -1))),
);
if (activatedKeys.length === 0) {
return { isAllKeysDeactivated: true, keysString: deactivatedKeys.join(', ') };
}
if (deactivatedKeys.length === 0) {
return { isAllKeysDeactivated: false, keysString: activatedKeys.join(', ') };
}
return {
isAllKeysDeactivated: false,
keysString: `${activatedKeys.join(', ')} /*, ${deactivatedKeys.join(', ')} */`,
};
};
const wrapInBrackets = name => {
return `[${name}]`;
};
const wrapInBracketsIfNecessary = name => {
return name.replace(/^(?!\().*?(?<!\))$/, '($&)');
};
const escapeSpecialCharacters = (name = '') => {
return name.replace(/'/g, "''");
};
const skipSqlCommentsPattern = /^\s*(EXEC\b|.*\bMS_DESCRIPTION\b)/i;
const buildScript = statements => {
const formattedScripts = statements
.filter(Boolean)
.map(script =>
skipSqlCommentsPattern.test(script)
? script
: sqlFormatter.format(script, { indent: ' ' }).replace(/\{ \{ (.+?) } }/g, '{{$1}}'),
);
return formattedScripts.join('\n\n') + '\n\n';
};
const getContainerName = compMod => compMod.keyspaceName;
const getFullEntityName = (dbName, entityName) => (dbName ? `${dbName}.${entityName}` : entityName);
const getEntityName = entityData => {
return (entityData && (entityData.code || entityData.collectionName)) || '';
};
/**
* @return {Array<any>}
* */
const filterEmptyScripts = (...scripts) => scripts.filter(Boolean);
const compareProperties =
_ =>
({ new: newProperty, old: oldProperty }) => {
if (!newProperty && !oldProperty) {
return;
}
return !_.isEqual(newProperty, oldProperty);
};
const compareObjectsByProperties = (obj1, obj2, properties) => {
return properties.some(prop => obj1[prop] !== obj2[prop]);
};
const getSchemaOfAlterCollection = collection => {
return { ...collection, ...(_.omit(collection?.role, 'properties') || {}) };
};
const buildDefaultPKName = (tableName, columnName) => {
const PKName = `PK_${tableName}_${columnName}`;
return wrapInBrackets(PKName);
};
return {
buildStatement,
getName,
getTab,
indentString,
getTypeDescriptor,
getRelationshipName,
prepareName,
replaceSpaceWithUnderscore,
commentDeactivatedStatements,
commentDeactivatedInlineKeys,
buildScript,
wrapInBrackets,
wrapInBracketsIfNecessary,
escapeSpecialCharacters,
getFullEntityName,
getFullTableName,
getFullCollectionName,
getContainerName,
getEntityName,
filterEmptyScripts,
getSchemaOfAlterCollection,
getNamePrefixedWithSchemaName,
buildDefaultPKName,
compareObjectsByProperties,
};
};