@onehat/data 1.18.0 → 1.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1050,7 +1050,7 @@ describe('Repository Base', function() {
|
|
|
1050
1050
|
|
|
1051
1051
|
});
|
|
1052
1052
|
|
|
1053
|
-
describe('
|
|
1053
|
+
describe('utilities', function() {
|
|
1054
1054
|
it('setOptions', function() {
|
|
1055
1055
|
|
|
1056
1056
|
this.repository.setOptions({
|
|
@@ -1062,6 +1062,45 @@ describe('Repository Base', function() {
|
|
|
1062
1062
|
expect(this.repository.api.baseURL).to.be.eq('test123');
|
|
1063
1063
|
});
|
|
1064
1064
|
|
|
1065
|
+
it('unmapData', function() {
|
|
1066
|
+
|
|
1067
|
+
// Setup
|
|
1068
|
+
const
|
|
1069
|
+
schema = new Schema({
|
|
1070
|
+
name: 'baz',
|
|
1071
|
+
model: {
|
|
1072
|
+
idProperty: 'foo',
|
|
1073
|
+
displayProperty: 'bar',
|
|
1074
|
+
properties: [
|
|
1075
|
+
{ name: 'foo', type: 'int' },
|
|
1076
|
+
{ name: 'bar' },
|
|
1077
|
+
{ name: 'baz', mapping: 'baz.test.val', type: 'bool', defaultValue: null, },
|
|
1078
|
+
{ name: 'boo', mapping: 'baz.test.boo', type: 'bool', defaultValue: null, },
|
|
1079
|
+
],
|
|
1080
|
+
},
|
|
1081
|
+
}),
|
|
1082
|
+
data = {
|
|
1083
|
+
foo: 1,
|
|
1084
|
+
bar: 'one',
|
|
1085
|
+
baz: {
|
|
1086
|
+
test: {
|
|
1087
|
+
val: true,
|
|
1088
|
+
boo: false,
|
|
1089
|
+
},
|
|
1090
|
+
},
|
|
1091
|
+
};
|
|
1092
|
+
this.repository.schema = schema;
|
|
1093
|
+
|
|
1094
|
+
// Now do the test
|
|
1095
|
+
const unmapped = this.repository.unmapData({
|
|
1096
|
+
foo: 1,
|
|
1097
|
+
bar: 'one',
|
|
1098
|
+
baz: true,
|
|
1099
|
+
boo: false,
|
|
1100
|
+
});
|
|
1101
|
+
expect(unmapped).to.be.eql(data);
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1065
1104
|
it('toString', function() {
|
|
1066
1105
|
const str = this.repository.toString();
|
|
1067
1106
|
expect(str).to.be.eq('NullRepository {bar} - foo');
|
package/package.json
CHANGED
|
@@ -1037,13 +1037,6 @@ export default class Repository extends EventEmitter {
|
|
|
1037
1037
|
return entity;
|
|
1038
1038
|
}
|
|
1039
1039
|
|
|
1040
|
-
/**
|
|
1041
|
-
* Convenience function to add entity with mapped data.
|
|
1042
|
-
*/
|
|
1043
|
-
addMapped = (data, isPersisted = false) => {
|
|
1044
|
-
return this.add(data, isPersisted, true);
|
|
1045
|
-
}
|
|
1046
|
-
|
|
1047
1040
|
/**
|
|
1048
1041
|
* Convenience function to create multiple new Entities in storage medium.
|
|
1049
1042
|
* @param {array} data - Array of data objects or Entities.
|
|
@@ -2020,13 +2013,11 @@ export default class Repository extends EventEmitter {
|
|
|
2020
2013
|
|
|
2021
2014
|
|
|
2022
2015
|
|
|
2023
|
-
|
|
2024
|
-
//
|
|
2025
|
-
//
|
|
2026
|
-
//
|
|
2027
|
-
// /
|
|
2028
|
-
// \____/ .___/\__/_/\____/_/ /_/____/
|
|
2029
|
-
// /_/
|
|
2016
|
+
// __ ____ _ ___ __ _
|
|
2017
|
+
// / / / / /_(_) (_) /_(_)__ _____
|
|
2018
|
+
// / / / / __/ / / / __/ / _ \/ ___/
|
|
2019
|
+
// / /_/ / /_/ / / / /_/ / __(__ )
|
|
2020
|
+
// \____/\__/_/_/_/\__/_/\___/____/
|
|
2030
2021
|
|
|
2031
2022
|
/**
|
|
2032
2023
|
* Set config options after Repository has already been initialized
|
|
@@ -2037,6 +2028,60 @@ export default class Repository extends EventEmitter {
|
|
|
2037
2028
|
_.merge(this, options);
|
|
2038
2029
|
}
|
|
2039
2030
|
|
|
2031
|
+
unmapData = (mappedData) => {
|
|
2032
|
+
const propertiesDef = this.schema?.model?.properties;
|
|
2033
|
+
if (!propertiesDef) {
|
|
2034
|
+
throw Error('No properties defined!');
|
|
2035
|
+
}
|
|
2036
|
+
|
|
2037
|
+
// Simply the definitions
|
|
2038
|
+
const
|
|
2039
|
+
UNMAPPED = 'UNMAPPED',
|
|
2040
|
+
properties = {};
|
|
2041
|
+
_.each(propertiesDef, (def) => {
|
|
2042
|
+
properties[def.name] = def.mapping || UNMAPPED;
|
|
2043
|
+
});
|
|
2044
|
+
|
|
2045
|
+
// Build the unmapped data
|
|
2046
|
+
const unmappedData = {};
|
|
2047
|
+
_.forOwn(mappedData, (value, field) => {
|
|
2048
|
+
const mapping = properties[field];
|
|
2049
|
+
if (mapping === UNMAPPED) {
|
|
2050
|
+
// Simple, just add the value
|
|
2051
|
+
unmappedData[field] = value;
|
|
2052
|
+
} else {
|
|
2053
|
+
// This is the more complicated one. Need to build up the hierarchy of unmapped data
|
|
2054
|
+
|
|
2055
|
+
const
|
|
2056
|
+
mapStack = mapping.split('.'),
|
|
2057
|
+
rawValue = value;
|
|
2058
|
+
|
|
2059
|
+
// Build up the hierarchy
|
|
2060
|
+
let thisValue = {},
|
|
2061
|
+
current = thisValue,
|
|
2062
|
+
i,
|
|
2063
|
+
total = mapStack.length;
|
|
2064
|
+
for (i = 0; i < total; i++) {
|
|
2065
|
+
let path = mapStack[i];
|
|
2066
|
+
if (current && !current.hasOwnProperty(path)) {
|
|
2067
|
+
current[path] = {}; // walk the path
|
|
2068
|
+
}
|
|
2069
|
+
if (i < total -1) {
|
|
2070
|
+
current = current[path];
|
|
2071
|
+
} else {
|
|
2072
|
+
current[path] = rawValue; // Last one, so set the thisValue
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
_.merge(unmappedData, thisValue);
|
|
2076
|
+
|
|
2077
|
+
}
|
|
2078
|
+
});
|
|
2079
|
+
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
return unmappedData;
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2040
2085
|
/**
|
|
2041
2086
|
* Set error handler for this repository
|
|
2042
2087
|
* @param {function} handler - the error handler
|