fix: null value not persisted for properties of type JSON, Any, or Object
Signed-off-by: Siim Sams <siim.sams@katanamrp.com>
This commit is contained in:
parent
c905f025df
commit
ae3cf25e77
|
@ -179,10 +179,12 @@ DataAccessObject._forDB = function(data) {
|
|||
const res = {};
|
||||
for (const propName in data) {
|
||||
const type = this.getPropertyType(propName);
|
||||
if (type === 'JSON' || type === 'Any' || type === 'Object' || data[propName] instanceof Array) {
|
||||
res[propName] = JSON.stringify(data[propName]);
|
||||
const value = data[propName];
|
||||
if (value !== null && (type === 'JSON' || type === 'Any' ||
|
||||
type === 'Object' || value instanceof Array)) {
|
||||
res[propName] = JSON.stringify(value);
|
||||
} else {
|
||||
res[propName] = data[propName];
|
||||
res[propName] = value;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
|
|
@ -1363,6 +1363,58 @@ describe('Model define with scopes configuration', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('DataAccessObject._forDB', function() {
|
||||
const ds = new DataSource('memory');
|
||||
const dao = ds.DataAccessObject;
|
||||
|
||||
it('should return input data if dataSource is not relational', function() {
|
||||
const inputData = {testKey: 'testValue'};
|
||||
dao.getDataSource = () => ({isRelational: () => false});
|
||||
|
||||
const outputData = dao._forDB(inputData);
|
||||
|
||||
assert.deepEqual(outputData, inputData);
|
||||
});
|
||||
|
||||
it('should return JSON stringified values for appropriate types', function() {
|
||||
const inputData = {
|
||||
key1: [1, 2, 3],
|
||||
key2: {subKey: 'value'},
|
||||
key3: 'nonJSONvalue',
|
||||
};
|
||||
dao.getDataSource = () => ({isRelational: () => true});
|
||||
dao.getPropertyType = (propName) => (propName !== 'key3' ? 'JSON' : 'String');
|
||||
|
||||
const outputData = dao._forDB(inputData);
|
||||
|
||||
assert.deepEqual(outputData, {
|
||||
key1: JSON.stringify([1, 2, 3]),
|
||||
key2: JSON.stringify({subKey: 'value'}),
|
||||
key3: 'nonJSONvalue',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return original value for non JSON, non Array types', function() {
|
||||
const inputData = {key1: 'string', key2: 123, key3: true};
|
||||
dao.getDataSource = () => ({isRelational: () => true});
|
||||
dao.getPropertyType = () => 'String';
|
||||
|
||||
const outputData = dao._forDB(inputData);
|
||||
|
||||
assert.deepEqual(outputData, inputData);
|
||||
});
|
||||
|
||||
it('should not process null values', function() {
|
||||
const inputData = {key1: 'value', key2: null};
|
||||
dao.getDataSource = () => ({isRelational: () => true});
|
||||
dao.getPropertyType = (propName) => 'JSON';
|
||||
|
||||
const outputData = dao._forDB(inputData);
|
||||
|
||||
assert.deepEqual(outputData, {key1: JSON.stringify('value'), key2: null});
|
||||
});
|
||||
});
|
||||
|
||||
describe('DataAccessObject', function() {
|
||||
let ds, model, where, error, filter;
|
||||
|
||||
|
|
Loading…
Reference in New Issue