mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
Merge pull request #1019 from strapi/mongoose/decimal
Improve Decimal128 and Float support with MongoDB
This commit is contained in:
commit
1c03f63465
@ -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.
|
||||
|
||||
@ -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':
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user