| 
									
										
										
										
											2016-07-14 11:06:58 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 13:11:46 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const Mongoose = require('mongoose'); | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  | const { parseType } = require('strapi-utils'); | 
					
						
							| 
									
										
										
										
											2019-02-02 13:11:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 11:06:58 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 13:11:46 +01:00
										 |  |  | module.exports = (mongoose = Mongoose) => { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |   mongoose.Schema.Types.Decimal = require('mongoose-float').loadType(mongoose, 2); | 
					
						
							|  |  |  |   mongoose.Schema.Types.Float = require('mongoose-float').loadType(mongoose, 20); | 
					
						
							| 
									
										
										
										
											2016-07-14 12:15:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-09 11:49:21 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Convert MongoDB ID to the stringify version as GraphQL throws an error if not. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Refer to: https://github.com/graphql/graphql-js/commit/3521e1429eec7eabeee4da65c93306b51308727b#diff-87c5e74dd1f7d923143e0eee611f598eR183
 | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-03-13 19:27:18 +01:00
										 |  |  |   mongoose.Types.ObjectId.prototype.valueOf = function() { | 
					
						
							| 
									
										
										
										
											2018-12-09 11:49:21 +01:00
										 |  |  |     return this.toString(); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-07-14 12:15:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-13 15:28:54 +01:00
										 |  |  |   const convertType = (name, attr) => { | 
					
						
							| 
									
										
										
										
											2020-01-06 11:15:00 +01:00
										 |  |  |     switch (attr.type.toLowerCase()) { | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'array': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: Array }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'boolean': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'Boolean' }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'binary': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'Buffer' }; | 
					
						
							|  |  |  |       case 'time': | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           type: String, | 
					
						
							|  |  |  |           validate: value => parseType({ type: 'time', value }), | 
					
						
							|  |  |  |           set: value => parseType({ type: 'time', value }), | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'date': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { | 
					
						
							|  |  |  |           type: String, | 
					
						
							|  |  |  |           validate: value => parseType({ type: 'date', value }), | 
					
						
							|  |  |  |           set: value => parseType({ type: 'date', value }), | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'datetime': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { | 
					
						
							|  |  |  |           type: Date, | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'timestamp': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { | 
					
						
							|  |  |  |           type: Date, | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'decimal': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'Decimal' }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'float': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'Float' }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'json': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'Mixed' }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'biginteger': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'Long' }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'integer': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'Number' }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'uuid': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'ObjectId' }; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'enumeration': | 
					
						
							| 
									
										
										
										
											2020-01-06 11:15:00 +01:00
										 |  |  |         return { | 
					
						
							|  |  |  |           type: 'String', | 
					
						
							|  |  |  |           enum: attr.enum.concat(null), | 
					
						
							|  |  |  |           default: null, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       case 'email': | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'password': | 
					
						
							|  |  |  |       case 'string': | 
					
						
							|  |  |  |       case 'text': | 
					
						
							| 
									
										
										
										
											2019-07-29 18:05:18 +02:00
										 |  |  |       case 'richtext': | 
					
						
							| 
									
										
										
										
											2019-12-05 15:26:01 +01:00
										 |  |  |         return { type: 'String' }; | 
					
						
							| 
									
										
										
										
											2020-02-13 15:28:54 +01:00
										 |  |  |       case 'uid': { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           type: 'String', | 
					
						
							|  |  |  |           index: { | 
					
						
							|  |  |  |             unique: true, | 
					
						
							|  |  |  |             partialFilterExpression: { [name]: { $type: 'string' } }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       default: | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-03-13 19:27:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |   const isMongoId = value => { | 
					
						
							|  |  |  |     if (value instanceof mongoose.Types.ObjectId) { | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-02 13:11:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |     if (!_.isString(value)) { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-02 13:11:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |     // Here we don't use mongoose.Types.ObjectId.isValid method because it's a weird check,
 | 
					
						
							|  |  |  |     // it returns for instance true for any integer value
 | 
					
						
							|  |  |  |     const hexadecimal = /^[0-9A-F]+$/i; | 
					
						
							|  |  |  |     return hexadecimal.test(value) && value.length === 24; | 
					
						
							| 
									
										
										
										
											2016-09-28 11:42:26 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-11-27 18:48:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |   const valueToId = value => { | 
					
						
							| 
									
										
										
										
											2020-01-07 12:51:21 +01:00
										 |  |  |     if (Array.isArray(value)) return value.map(valueToId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |     if (isMongoId(value)) { | 
					
						
							|  |  |  |       return mongoose.Types.ObjectId(value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return value; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     convertType, | 
					
						
							|  |  |  |     valueToId, | 
					
						
							|  |  |  |     isMongoId, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-09-28 11:42:26 +02:00
										 |  |  | }; |