Merge pull request #1019 from strapi/mongoose/decimal

Improve Decimal128 and Float support with MongoDB
This commit is contained in:
Jim LAURIE 2018-04-24 15:56:49 +02:00 committed by GitHub
commit 1c03f63465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 12 deletions

View File

@ -6,7 +6,8 @@
// Public node modules.
const _ = require('lodash');
const Mongoose = require('mongoose').Mongoose;
const mongoose = require('mongoose');
const Mongoose = mongoose.Mongoose;
const mongooseUtils = require('mongoose/lib/utils');
// Local helpers.
@ -176,6 +177,14 @@ module.exports = function (strapi) {
collection.schema.options.toObject = collection.schema.options.toJSON = {
virtuals: true,
transform: function (doc, returned, opts) {
// Remover $numberDecimal nested property.
Object.keys(returned)
.filter(key => returned[key] instanceof mongoose.Types.Decimal128)
.forEach((key, index) => {
// Parse to float number.
returned[key] = parseFloat(returned[key].toString());
});
morphAssociations.forEach(association => {
if (Array.isArray(returned[association.alias]) && returned[association.alias].length > 0) {
// Reformat data by bypassing the many-to-many relationship.

View File

@ -7,15 +7,6 @@
module.exports = mongoose => {
require('mongoose-float').loadType(mongoose);
const SchemaTypes = mongoose.Schema.Types;
// Note: The decimal format isn't well supported by MongoDB.
// It's recommended to use Float or Number type instead.
//
// SchemaTypes.Decimal.prototype.cast = function (value) {
// return value.toString();
// };
return {
convertType: mongooseType => {
switch (mongooseType.toLowerCase()) {
@ -31,8 +22,9 @@ module.exports = mongoose => {
case 'timestamp':
return Date;
case 'decimal':
return 'Float';
case 'float':
return 'Number';
return mongoose.Schema.Types.Decimal128;
case 'json':
return 'Mixed';
case 'biginteger':

View File

@ -62,7 +62,7 @@ module.exports = {
const { source } = ctx.request.query;
// Find an entry using `queries` system
const entry = await strapi.plugins['content-manager'].services['contentmanager'].fetch(ctx.params, source);
const entry = await strapi.plugins['content-manager'].services['contentmanager'].fetch(ctx.params, source, null, false);
// Entry not found
if (!entry) {