-
Notifications
You must be signed in to change notification settings - Fork 36
/
DataExtensionField.js
338 lines (313 loc) · 13.8 KB
/
DataExtensionField.js
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
'use strict';
const TYPE = require('../../types/mcdev.d');
const MetadataType = require('./MetadataType');
const Util = require('../util/util');
/**
* DataExtensionField MetadataType
*
* @augments MetadataType
*/
class DataExtensionField extends MetadataType {
/**
* Retrieves all records and saves it to disk
*
* @param {string} retrieveDir Directory where retrieved metadata directory will be saved
* @param {string[]} [additionalFields] Returns specified fields even if their retrieve definition is not set to true
* @returns {Promise.<{metadata: TYPE.DataExtensionFieldMap, type: string}>} Promise of items
*/
static async retrieve(retrieveDir, additionalFields) {
return super.retrieveSOAP(retrieveDir, null, null, additionalFields);
}
/**
* Retrieves all records for caching
*
* @param {TYPE.SoapRequestParams} [requestParams] required for the specific request (filter for example)
* @param {string[]} [additionalFields] Returns specified fields even if their retrieve definition is not set to true
* @returns {Promise.<{metadata: TYPE.DataExtensionFieldMap, type: string}>} Promise of items
*/
static async retrieveForCache(requestParams, additionalFields) {
return super.retrieveSOAP(null, requestParams, null, additionalFields);
}
/**
* helper for {@link DataExtension._retrieveFieldsForSingleDe} that sorts the fields into an array
*
* @param {TYPE.DataExtensionFieldMap} fieldsObj customerKey-based list of fields for one dataExtension
* @returns {TYPE.DataExtensionFieldItem[]} sorted array of field objects
*/
static convertToSortedArray(fieldsObj) {
return (
Object.keys(fieldsObj)
.map((field) => fieldsObj[field])
// the API returns the fields not sorted
.sort(this.sortDeFields)
);
}
/**
* sorting method to ensure `Ordinal` is respected
*
* @param {TYPE.DataExtensionFieldItem} a -
* @param {TYPE.DataExtensionFieldItem} b -
* @returns {boolean} sorting based on Ordinal
*/
static sortDeFields(a, b) {
return a.Ordinal - b.Ordinal;
}
/**
* manages post retrieve steps
*
* @param {TYPE.DataExtensionFieldItem} metadata a single item
* @param {boolean} forDataExtension when used by DataExtension class we remove more fields
* @returns {TYPE.DataExtensionFieldItem} metadata
*/
static postRetrieveTasks(metadata, forDataExtension) {
return this._parseMetadata(metadata, forDataExtension);
}
/**
* parses retrieved Metadata before saving
*
* @private
* @param {TYPE.DataExtensionFieldItem} metadata a single record
* @param {boolean} forDataExtension when used by DataExtension class we remove more fields
* @returns {TYPE.DataExtensionFieldItem} parsed metadata definition
*/
static _parseMetadata(metadata, forDataExtension) {
if (forDataExtension) {
// remove fields according to definition
this.keepRetrieveFields(metadata);
// remove fields that we do not need after association to a DE
delete metadata.CustomerKey;
delete metadata.DataExtension;
delete metadata.Ordinal;
if (metadata.FieldType !== 'Decimal') {
// remove scale - it's only used for "Decimal" to define the digits behind the decimal
delete metadata.Scale;
}
}
return metadata;
}
/**
* Mofifies passed deployColumns for update by mapping ObjectID to their target column's values.
* Removes FieldType field if its the same in deploy and target column, because it results in an error even if its of the same type
*
* @param {TYPE.DataExtensionFieldItem[]} deployColumns Columns of data extension that will be deployed
* @param {string} deKey external/customer key of Data Extension
* @returns {Promise.<Object.<string, TYPE.DataExtensionFieldItem>>} existing fields by their original name to allow re-adding FieldType after update
*/
static async prepareDeployColumnsOnUpdate(deployColumns, deKey) {
// get row count to know which field restrictions apply
let hasData = false;
try {
const rowset = await this.client.rest.get(
`/data/v1/customobjectdata/key/${deKey}/rowset?$page=1&$pagesize=1`
);
const rowCount = rowset.count;
hasData = rowCount > 0;
Util.logger.debug(`dataExtension ${deKey} row count: ${rowCount}`);
} catch (ex) {
Util.logger.debug(`Could not retrieve rowcount for ${deKey}: ${ex.message}`);
}
// retrieve existing fields to enable updating them
const response = await this.retrieveForCache(
{
filter: {
leftOperand: 'DataExtension.CustomerKey',
operator: 'equals',
rightOperand: deKey,
},
},
['Name', 'ObjectID']
);
const fieldsObj = response.metadata;
// ensure fields can be updated properly by their adding ObjectId based on Name-matching
/** @type {Object.<string, TYPE.DataExtensionFieldItem>} */
const existingFieldByName = {};
for (const key of Object.keys(fieldsObj)) {
existingFieldByName[fieldsObj[key].Name] = fieldsObj[key];
}
for (let i = deployColumns.length - 1; i >= 0; i--) {
const item = deployColumns[i];
const itemOld = existingFieldByName[item.Name];
if (itemOld) {
// field is getting updated ---
// Updating to a new FieldType will result in an error; warn & afterwards remove it
if (itemOld.FieldType !== item.FieldType) {
// applicable: with or without data but simply ignored by API
Util.logger.warn(
` - The Field Type of an existing field cannot be changed. Keeping the original value for [${deKey}].[${item.Name}]: '${itemOld.FieldType}'`
);
item.FieldType = itemOld.FieldType;
}
if (item.FieldType !== 'Decimal') {
// remove scale - it's only used for "Decimal" to define the digits behind the decimal
delete item.Scale;
}
delete item.FieldType;
if (itemOld.MaxLength > item.MaxLength) {
// applicable: with or without data (Code 310007)
Util.logger.warn(
` - The length of an existing field cannot be decreased. Keeping the original value for [${deKey}].[${item.Name}]: '${itemOld.MaxLength}'`
);
item.MaxLength = itemOld.MaxLength;
}
if (Util.isFalse(itemOld.IsRequired) && Util.isTrue(item.IsRequired)) {
// applicable: with or without data (Code 310007)
Util.logger.warn(
` - A field cannot be changed to be required on update after it was created to allow nulls. Resetting to not equired: [${deKey}].[${item.Name}]`
);
item.IsRequired = itemOld.IsRequired;
}
// enable renaming
if (item.Name_new) {
Util.logger.info(
` - Found Name_new='${item.Name_new}' for field ${deKey}.${item.Name} - trying to rename.`
);
item.Name = item.Name_new;
delete item.Name_new;
}
// check if any changes were found
let changeFound = false;
for (const key of Object.keys(item)) {
if (item[key] !== itemOld[key]) {
changeFound = true;
}
}
if (!changeFound) {
deployColumns.splice(i, 1);
Util.logger.verbose(`no change - removed field [${deKey}].[${item.Name}]`);
continue;
}
// set the ObjectId for clear identification during update
item.ObjectID = itemOld.ObjectID;
} else {
// field is getting added ---
if (hasData && Util.isTrue(item.IsRequired) && item.DefaultValue === '') {
// applicable: with data only
if (Util.isFalse(item.IsPrimaryKey)) {
Util.logger.warn(
` - Adding new fields to an existing table requires that these fields are either not-required (nullable) or have a default value set. Changing [${deKey}].[${item.Name}] to be not-required`
);
item.IsRequired = false;
} else {
Util.logger.error(
`- You cannot add a new primary key field to an existing table that has data. Removing [${deKey}].[${item.Name}] from deployment`
);
deployColumns.splice(i, 1);
}
}
if (item.Name_new) {
Util.logger.info(
` - Found Name_new='${item.Name_new}' for field ${deKey}.${item.Name} but could not find a corresponding DE field on the server - adding new field instead of updating.`
);
delete item.Name_new;
}
// Field doesn't exist in target, therefore Remove ObjectID if present
delete item.ObjectID;
}
if (Util.isTrue(item.IsPrimaryKey) && Util.isFalse(item.IsRequired)) {
// applicable: with or without data
Util.logger.warn(
`- Primary Key field [${deKey}].[${item.Name}] cannot be not-required (nullable). Changing field to be required!`
);
item.IsRequired = true;
}
// filter bad manual changes to the json
if (!Util.isTrue(item.IsRequired) && !Util.isFalse(item.IsRequired)) {
Util.logger.error(
`- Invalid value for 'IsRequired' of [${deKey}].[${item.Name}]. Found '${item.IsRequired}' instead of 'true'/'false'. Removing field from deploy!`
);
deployColumns.splice(i, 1);
}
if (!Util.isTrue(item.IsPrimaryKey) && !Util.isFalse(item.IsPrimaryKey)) {
Util.logger.error(
`- Invalid value for 'IsPrimaryKey' of [${deKey}].[${item.Name}]. Found '${item.IsPrimaryKey}' instead of 'true'/'false'. Removing field from deploy!`
);
deployColumns.splice(i, 1);
}
}
Util.logger.info(
Util.getGrayMsg(
` - Found ${deployColumns.length} added/updated Fields for ${deKey}${
deployColumns.length ? ': ' : ''
}` + deployColumns.map((item) => item.Name).join(', ')
)
);
return existingFieldByName;
}
/**
* Delete a metadata item from the specified business unit
*
* @param {string} customerKey Identifier of data extension
* @returns {Promise.<boolean>} deletion success status
*/
static deleteByKey(customerKey) {
return this.deleteByKeySOAP(customerKey);
}
/**
* Delete a data extension from the specified business unit
*
* @param {string} customerKey Identifier of metadata
* @returns {boolean} deletion success flag
*/
static async deleteByKeySOAP(customerKey) {
const [deKey, fieldKey] = customerKey.split('.');
customerKey = `[${deKey}].[${fieldKey}]`;
// get the object id
const response = await this.retrieveForCache(
{
filter: {
leftOperand: 'CustomerKey',
operator: 'equals',
rightOperand: customerKey,
},
},
['Name', 'ObjectID']
);
const fieldObjectID = response.metadata[customerKey].ObjectID;
if (!fieldObjectID) {
Util.logger.error(`Could not find ${customerKey} on your BU`);
return false;
}
// normal code
const keyObj = {
CustomerKey: deKey,
Fields: {
Field: {
ObjectID: fieldObjectID,
},
},
};
try {
this.client.soap.delete(
'DataExtension', // yes, not DataExtensionField
keyObj,
null
);
Util.logger.info(`- deleted ${this.definition.type}: ${customerKey}`);
this.postDeleteTasks(customerKey);
return true;
} catch (ex) {
const errorMsg = ex.results?.length
? `${ex.results[0].StatusMessage} (Code ${ex.results[0].ErrorCode})`
: ex.message;
Util.logger.error(
`- error deleting ${this.definition.type} '${customerKey}': ${errorMsg}`
);
return false;
}
}
/**
* clean up after deleting a metadata item
*
* @param {string} customerKey Identifier of metadata item
* @returns {void}
*/
static async postDeleteTasks(customerKey) {
// TODO actually clean up local dataextension json
Util.logger.warn(
` - The dataExtension for ${customerKey} wasn't updated locally yet after removing this field.`
);
}
}
// Assign definition to static attributes
DataExtensionField.definition = require('../MetadataTypeDefinitions').dataExtensionField;
module.exports = DataExtensionField;