mvom
Version:
Multivalue Object Mapper
55 lines (45 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _utils = require("../utils");
// #region types
// #endregion
/** Abstract Base Schema Type */
class BaseSchemaType {
validators = [];
/** Create an array of foreign key definitions that will be validated before save */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
transformForeignKeyDefinitionsToDb(value) {
return [];
}
/** Cast to complex data type (when applicable) */
cast(value) {
return value;
}
/** Get data from a multivalue array at a given path */
getFromMvArray(path, record = []) {
// if the entire contents of the record at the base path is null then we must treat this as a special case:
// - returning undefined won't alter the behavior of scalar data types (e.g. string, Boolean) since the undefined
// value will essentially get typecast back into null which is handled by all scalar types.
// - it is important to all "array" types that null be interpreted differently, however. A null for a value intended
// to become an array needs to be treated as an empty array, which is different than an array of null. Returning
// undefined here will allow the array types to detect this event and behave accordingly.
const attributeData = record[path[0]];
if (attributeData == null) {
return undefined;
}
if (path.length === 3 && Array.isArray(attributeData) && attributeData[path[1]] == null) {
return undefined;
}
// lodash.get will not work here because "array" data might be returned from multi-value that still
// appears like a non-array; if that happens, lodash.get would return the character at that string position instead;
// this reducer ensures that the appropriate value is retrieved.
return path.slice(1).reduce((acc, pathPart) => (0, _utils.ensureArray)(acc)[pathPart], attributeData);
}
/** Get value from mv data */
/** Set value into mv data */
/** Validate value */
}
var _default = exports.default = BaseSchemaType;