-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinsertUpdateValidate.js
More file actions
98 lines (83 loc) · 3.71 KB
/
Copy pathinsertUpdateValidate.js
File metadata and controls
98 lines (83 loc) · 3.71 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
'use strict';
const hdbUtils = require('../../../utility/common_utils.ts');
const log = require('../../../utility/logging/harper_logger.ts');
const { getDatabases } = require('../../../resources/databases.ts');
const { ClientError } = require('../../../utility/errors/hdbError.ts');
const { UNSET_ATTRIBUTES } = require('../../../utility/hdbTerms.ts');
module.exports = insertUpdateValidate;
//IMPORTANT - This code is the same code as the async validation() function in dataLayer/insert - make sure any changes
// below are also made there. This is to resolve a circular dependency.
/**
* Takes an insert/update object and validates attributes, also looks for dups and get a list of all attributes from the record set
* @param {Object} writeObject
* @returns {{schema_table: any, hashes: any[], attributes: string[]}}
*/
function insertUpdateValidate(writeObject) {
// Need to validate these outside of the validator as the getTableSchema call will fail with
// invalid values.
if (hdbUtils.isEmpty(writeObject)) {
throw new ClientError('invalid update parameters defined.');
}
if (hdbUtils.isEmptyOrZeroLength(writeObject.schema)) {
throw new ClientError('invalid schema specified.');
}
if (hdbUtils.isEmptyOrZeroLength(writeObject.table)) {
throw new ClientError('invalid table specified.');
}
if (!Array.isArray(writeObject.records)) {
throw new ClientError('records must be an array');
}
let schemaTable = getDatabases()[writeObject.schema]?.[writeObject.table];
if (hdbUtils.isEmpty(schemaTable)) {
throw new ClientError(`could not retrieve schema:${writeObject.schema} and table ${writeObject.table}`);
}
let hash_attribute = schemaTable.primaryKey;
let dups = new Set();
let attributes = {};
let isUpdate = false;
if (writeObject.operation === 'update') {
isUpdate = true;
}
writeObject.records.forEach((record) => {
if (isUpdate && hdbUtils.isEmptyOrZeroLength(record[hash_attribute])) {
log.error('a valid hash attribute must be provided with update record:', record);
throw new ClientError('a valid hash attribute must be provided with update record, check log for more info');
}
if (
!hdbUtils.isEmptyOrZeroLength(record[hash_attribute]) &&
(record[hash_attribute] === 'null' || record[hash_attribute] === 'undefined')
) {
log.error(`a valid hash value must be provided with ${writeObject.operation} record:`, record);
throw new ClientError(
`Invalid hash value: '${record[hash_attribute]}' is not a valid hash attribute value, check log for more info`
);
}
if (
!hdbUtils.isEmpty(record[hash_attribute]) &&
record[hash_attribute] !== '' &&
dups.has(hdbUtils.autoCast(record[hash_attribute]))
) {
record.skip = true;
}
dups.add(hdbUtils.autoCast(record[hash_attribute]));
for (let attr in record) {
// `__unset__` names attributes to remove; it is not one itself. Collecting it would make an
// open (non-schemaDefined) table register `__unset__` as a real attribute, since
// upsertRecords feeds this list straight to Table.addAttributes.
//
// Deliberately does NOT add the names it removes, unlike the async twin's list in
// dataLayer/insert.ts: that one is the bulk-load attribute-permission input, where a removal
// must be authorized, while this one creates table attributes — registering an attribute in
// order to delete it would be backwards. Keep both comments in step.
if (attr === UNSET_ATTRIBUTES) continue;
attributes[attr] = 1;
}
});
//in case the hash_attribute was not on the object(s) for inserts where they want to auto-key we manually add the hash_attribute to attributes
attributes[hash_attribute] = 1;
return {
schema_table: schemaTable,
hashes: Array.from(dups),
attributes: Object.keys(attributes),
};
}