Skip to content

Commit

Permalink
fixup! apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
biniam committed Apr 9, 2019
1 parent 12ed873 commit ba4faa6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
37 changes: 19 additions & 18 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ MongoDB.prototype.toDatabase = function(model, data) {
var props = this._models[model].properties;

if (this.settings.enableGeoIndexing !== true) {
visitAllProperties(data, modelInstance, coerceDecimalProperties);
visitAllProperties(data, modelInstance, coerceDecimalProperty);
// Override custom column names
data = this.fromPropertyToDatabaseNames(model, data);
return data;
Expand All @@ -430,7 +430,7 @@ MongoDB.prototype.toDatabase = function(model, data) {
}
}

visitAllProperties(data, modelInstance, coerceDecimalProperties);
visitAllProperties(data, modelInstance, coerceDecimalProperty);
// Override custom column names
data = this.fromPropertyToDatabaseNames(model, data);
if (debug.enabled) debug('toDatabase data: ', util.inspect(data));
Expand Down Expand Up @@ -2000,28 +2000,28 @@ function optimizedFindOrCreate(model, filter, data, options, callback) {
*
* @param {*} data Plain Data Object for the matching property definition(s)
* @param {*} modelCtorOrDef Model constructor or definition
* @param {*} setValue A callback function which takes a property value and
* @param {*} visitor A callback function which takes a property value and
* definition to apply custom property coercion
*/
function visitAllProperties(data, modelCtorOrDef, setValue) {
if (data === null) return;
function visitAllProperties(data, modelCtorOrDef, visitor) {
if (data === null || data === undefined) return;
const modelProps = modelCtorOrDef.properties ? modelCtorOrDef.properties : modelCtorOrDef.definition.properties;
const allProps = new Set(Object.keys(data).concat(Object.keys(modelProps)));
for (const p of allProps) {
const value = data[p];
const def = modelProps[p];
if (def && def.type && isNestedModel(def.type)) {
if (Array.isArray(def.type)) {
if (value === null || value.length === 0) continue;
if (value === null || value === undefined) break;
for (const it of value) {
visitAllProperties(it, def.type[0].definition, setValue);
visitAllProperties(it, def.type[0].definition, visitor);
}
} else {
if (value === null) continue;
visitAllProperties(value, def.type.definition, setValue);
if (value === null) break;
visitAllProperties(value, def.type.definition, visitor);
}
} else {
data[p] = setValue(value, def);
visitor(value, def, (newValue) => { data[p] = newValue; });
}
}
}
Expand All @@ -2030,17 +2030,18 @@ function visitAllProperties(data, modelCtorOrDef, setValue) {
*
* @param {*} propValue Property value to coerce into a Decimal128 value
* @param {*} propDef Property definition to check if property is MongoDB
* decima type
* Decimal128 type
*/
function coerceDecimalProperties(propValue, propDef) {
function coerceDecimalProperty(propValue, propDef, setValue) {
var updatedValue;
if (hasDataType('decimal128', propDef)) {
if (Array.isArray(propValue)) {
return propValue.map(val => Decimal128.fromString(val));
updatedValue = propValue.map(val => Decimal128.fromString(val));
return setValue(updatedValue);
} else {
return Decimal128.fromString(propValue);
updatedValue = Decimal128.fromString(propValue);
return setValue(updatedValue);
}
} else {
return propValue;
}
}

Expand All @@ -2064,7 +2065,7 @@ function isNestedModel(propType) {
*/
function hasDataType(dataType, propertyDef) {
return propertyDef && propertyDef.mongodb &&
propertyDef.mongodb.dataType &&
propertyDef.mongodb.dataType.toLowerCase() === dataType.toLowerCase();
propertyDef.mongodb.dataType &&
propertyDef.mongodb.dataType.toLowerCase() === dataType.toLowerCase();
}

55 changes: 23 additions & 32 deletions test/decimal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('model with decimal property', function() {
});
});

it('destroyAll - deletes all with where', function(done) {
OrderDecimal.create({count: '0.0006'})
it('destroyAll - deletes all with where', function() {
return OrderDecimal.create({count: '0.0006'})
.then(function() {
return OrderDecimal.destroyAll({count: '0.0005'});
})
Expand All @@ -65,13 +65,11 @@ describe('model with decimal property', function() {
.then(function(r) {
r.length.should.equal(1);
r[0].count.should.deepEqual(Decimal128.fromString('0.0006'));
done();
})
.catch(done);
});
});

context('nested decimal props', function() {
it('should create/update instance for array of decimal props', function(done) {
it('should create/update instance for array of decimal props', function() {
modelWithDecimalArray = db.define('modelWithDecimalArray', {
randomReview: {
type: [String],
Expand Down Expand Up @@ -99,27 +97,25 @@ describe('model with decimal property', function() {
};
let instanceId;

modelWithDecimalArray.create(createData)
return modelWithDecimalArray.create(createData)
.then(function(inst) {
instanceId = inst.id;
return findModelInstanceAsync('modelWithDecimalArray', instanceId);
return findRawModelDataAsync('modelWithDecimalArray', instanceId);
})
.then(function(createdInstance) {
createdInstance.randomReview[0].should.be.instanceOf(Decimal128);
createdInstance.randomReview[0].should.deepEqual(Decimal128.fromString('3.5'));
return modelWithDecimalArray.updateAll({id: instanceId}, updateData);
})
.then(function(inst) {
return findModelInstanceAsync('modelWithDecimalArray', instanceId);
return findRawModelDataAsync('modelWithDecimalArray', instanceId);
})
.then(function(updatedInstance) {
updatedInstance.randomReview[0].should.be.instanceOf(Decimal128);
updatedInstance.randomReview[0].should.deepEqual(Decimal128.fromString('5.5'));
done();
})
.catch(done);
});
});
it('should create/update instance for nested decimal prop inside array', function(done) {
it('should create/update instance for nested decimal prop inside array', function() {
modelWithDecimalNestedArray = db.define('modelWithDecimalNestedArray', {
tickets: {
type: [
Expand Down Expand Up @@ -174,25 +170,23 @@ describe('model with decimal property', function() {
modelWithDecimalNestedArray.create(createData)
.then(function(inst) {
instanceId = inst.id;
return findModelInstanceAsync('modelWithDecimalNestedArray', instanceId);
return findRawModelDataAsync('modelWithDecimalNestedArray', instanceId);
})
.then(function(createdInstance) {
createdInstance.tickets[0].unitprice.should.be.instanceOf(Decimal128);
createdInstance.tickets[0].unitprice.should.deepEqual(Decimal128.fromString('19.5'));
return modelWithDecimalNestedArray.updateAll({id: instanceId}, updateData);
})
.then(function(inst) {
return findModelInstanceAsync('modelWithDecimalNestedArray', instanceId);
return findRawModelDataAsync('modelWithDecimalNestedArray', instanceId);
})
.then(function(updatedInstance) {
updatedInstance.tickets[0].unitprice.should.be.instanceOf(Decimal128);
updatedInstance.tickets[0].unitprice.should.deepEqual(Decimal128.fromString('27.5'));
done();
})
.catch(done);
});
});

it('should create/update instance for nested decimal prop inside object', function(done) {
it('should create/update instance for nested decimal prop inside object', function() {
modelWithDecimalNestedObject = db.define('modelWithDecimalNestedObject', {
awards: {
type: {
Expand Down Expand Up @@ -229,27 +223,25 @@ describe('model with decimal property', function() {

let instanceId;

modelWithDecimalNestedObject.create(createData)
return modelWithDecimalNestedObject.create(createData)
.then(function(inst) {
instanceId = inst.id;
return findModelInstanceAsync('modelWithDecimalNestedObject', instanceId);
return findRawModelDataAsync('modelWithDecimalNestedObject', instanceId);
})
.then(function(createdInstance) {
createdInstance.awards.prizeMoney.should.be.instanceOf(Decimal128);
createdInstance.awards.prizeMoney.should.deepEqual(Decimal128.fromString('10000.00'));
return modelWithDecimalNestedObject.updateAll({id: instanceId}, updateData);
})
.then(function() {
return findModelInstanceAsync('modelWithDecimalNestedObject', instanceId);
return findRawModelDataAsync('modelWithDecimalNestedObject', instanceId);
})
.then(function(updatedInstance) {
updatedInstance.awards.prizeMoney.should.be.instanceOf(Decimal128);
updatedInstance.awards.prizeMoney.should.deepEqual(Decimal128.fromString('25000.00'));
done();
})
.catch(done);
});
});
it('should create/update instance for deeply nested decimal props', function(done) {
it('should create/update instance for deeply nested decimal props', function() {
modelWithDeepNestedDecimalProps = db.define('modelWithDeepNestedDecimalProps', {
imdb: {
type: {
Expand Down Expand Up @@ -439,10 +431,10 @@ describe('model with decimal property', function() {
};
let instanceId;

modelWithDeepNestedDecimalProps.create(createData)
return modelWithDeepNestedDecimalProps.create(createData)
.then(function(inst) {
instanceId = inst.id;
return findModelInstanceAsync('modelWithDeepNestedDecimalProps', instanceId);
return findRawModelDataAsync('modelWithDeepNestedDecimalProps', instanceId);
})
.then(function(createdInstance) {
createdInstance.imdb.rating.should.be.instanceOf(Decimal128);
Expand All @@ -466,7 +458,7 @@ describe('model with decimal property', function() {
return modelWithDeepNestedDecimalProps.updateAll({id: instanceId}, updateData);
})
.then(function() {
return findModelInstanceAsync('modelWithDeepNestedDecimalProps', instanceId);
return findRawModelDataAsync('modelWithDeepNestedDecimalProps', instanceId);
})
.then(function(updatedInstance) {
updatedInstance.imdb.rating.should.be.instanceOf(Decimal128);
Expand All @@ -487,13 +479,12 @@ describe('model with decimal property', function() {
.testDecimal.should.be.instanceOf(Decimal128);
updatedInstance.imdb.innerArray[0].nestedArray[0]
.testDecimal.should.deepEqual(Decimal128.fromString('77.77'));
done();
});
});

function findModelInstance(modelName, id, cb) {
function findRawModelData(modelName, id, cb) {
db.connector.execute(modelName, 'findOne', {_id: {$eq: id}}, {safe: true}, cb);
}
var findModelInstanceAsync = promisify(findModelInstance);
const findRawModelDataAsync = promisify(findRawModelData);
});
});

0 comments on commit ba4faa6

Please sign in to comment.