| 
									
										
										
										
											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'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 11:06:58 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-02 13:11:46 +01:00
										 |  |  | module.exports = (mongoose = Mongoose) => { | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |   const convertType = mongooseType => { | 
					
						
							|  |  |  |     switch (mongooseType.toLowerCase()) { | 
					
						
							|  |  |  |       case 'array': | 
					
						
							|  |  |  |         return Array; | 
					
						
							|  |  |  |       case 'boolean': | 
					
						
							|  |  |  |         return 'Boolean'; | 
					
						
							|  |  |  |       case 'binary': | 
					
						
							|  |  |  |         return 'Buffer'; | 
					
						
							|  |  |  |       case 'date': | 
					
						
							|  |  |  |       case 'datetime': | 
					
						
							|  |  |  |       case 'time': | 
					
						
							|  |  |  |       case 'timestamp': | 
					
						
							|  |  |  |         return Date; | 
					
						
							|  |  |  |       case 'decimal': | 
					
						
							|  |  |  |         return 'Decimal'; | 
					
						
							|  |  |  |       case 'float': | 
					
						
							|  |  |  |         return 'Float'; | 
					
						
							|  |  |  |       case 'json': | 
					
						
							|  |  |  |         return 'Mixed'; | 
					
						
							|  |  |  |       case 'biginteger': | 
					
						
							| 
									
										
										
										
											2019-08-08 14:29:09 +02:00
										 |  |  |         return 'Long'; | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |       case 'integer': | 
					
						
							|  |  |  |         return 'Number'; | 
					
						
							|  |  |  |       case 'uuid': | 
					
						
							|  |  |  |         return 'ObjectId'; | 
					
						
							|  |  |  |       case 'email': | 
					
						
							|  |  |  |       case 'enumeration': | 
					
						
							|  |  |  |       case 'password': | 
					
						
							|  |  |  |       case 'string': | 
					
						
							|  |  |  |       case 'text': | 
					
						
							| 
									
										
										
										
											2019-07-29 18:05:18 +02:00
										 |  |  |       case 'richtext': | 
					
						
							| 
									
										
										
										
											2019-07-04 15:27:27 +02:00
										 |  |  |         return 'String'; | 
					
						
							|  |  |  |       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 => { | 
					
						
							|  |  |  |     if (isMongoId(value)) { | 
					
						
							|  |  |  |       return mongoose.Types.ObjectId(value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return value; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     convertType, | 
					
						
							|  |  |  |     valueToId, | 
					
						
							|  |  |  |     isMongoId, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-09-28 11:42:26 +02:00
										 |  |  | }; |