mirror of
https://github.com/strapi/strapi.git
synced 2025-11-11 07:39:16 +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.
|
// Public node modules.
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const Mongoose = require('mongoose').Mongoose;
|
const mongoose = require('mongoose');
|
||||||
|
const Mongoose = mongoose.Mongoose;
|
||||||
const mongooseUtils = require('mongoose/lib/utils');
|
const mongooseUtils = require('mongoose/lib/utils');
|
||||||
|
|
||||||
// Local helpers.
|
// Local helpers.
|
||||||
@ -176,6 +177,14 @@ module.exports = function (strapi) {
|
|||||||
collection.schema.options.toObject = collection.schema.options.toJSON = {
|
collection.schema.options.toObject = collection.schema.options.toJSON = {
|
||||||
virtuals: true,
|
virtuals: true,
|
||||||
transform: function (doc, returned, opts) {
|
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 => {
|
morphAssociations.forEach(association => {
|
||||||
if (Array.isArray(returned[association.alias]) && returned[association.alias].length > 0) {
|
if (Array.isArray(returned[association.alias]) && returned[association.alias].length > 0) {
|
||||||
// Reformat data by bypassing the many-to-many relationship.
|
// Reformat data by bypassing the many-to-many relationship.
|
||||||
|
|||||||
@ -7,15 +7,6 @@
|
|||||||
module.exports = mongoose => {
|
module.exports = mongoose => {
|
||||||
require('mongoose-float').loadType(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 {
|
return {
|
||||||
convertType: mongooseType => {
|
convertType: mongooseType => {
|
||||||
switch (mongooseType.toLowerCase()) {
|
switch (mongooseType.toLowerCase()) {
|
||||||
@ -31,8 +22,9 @@ module.exports = mongoose => {
|
|||||||
case 'timestamp':
|
case 'timestamp':
|
||||||
return Date;
|
return Date;
|
||||||
case 'decimal':
|
case 'decimal':
|
||||||
|
return 'Float';
|
||||||
case 'float':
|
case 'float':
|
||||||
return 'Number';
|
return mongoose.Schema.Types.Decimal128;
|
||||||
case 'json':
|
case 'json':
|
||||||
return 'Mixed';
|
return 'Mixed';
|
||||||
case 'biginteger':
|
case 'biginteger':
|
||||||
|
|||||||
@ -62,7 +62,7 @@ module.exports = {
|
|||||||
const { source } = ctx.request.query;
|
const { source } = ctx.request.query;
|
||||||
|
|
||||||
// Find an entry using `queries` system
|
// 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
|
// Entry not found
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user