56 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-07-14 11:06:58 +02:00
'use strict';
/**
* Module dependencies
*/
module.exports = (mongoose = new Mongoose()) => {
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
*/
mongoose.Types.ObjectId.prototype.valueOf = function () {
return this.toString();
};
2016-07-14 12:15:42 +02:00
2016-07-14 11:06:58 +02:00
return {
2016-07-14 12:15:42 +02:00
convertType: mongooseType => {
2016-07-14 11:06:58 +02:00
switch (mongooseType.toLowerCase()) {
2018-03-17 16:43:25 +00:00
case 'array':
2018-06-17 13:24:13 +02:00
return Array;
2016-07-14 11:06:58 +02:00
case 'boolean':
2016-09-28 11:42:26 +02:00
return 'Boolean';
2016-07-14 11:06:58 +02:00
case 'binary':
return 'Buffer';
2018-03-17 16:42:28 +00:00
case 'date':
case 'datetime':
case 'time':
case 'timestamp':
return Date;
case 'decimal':
return 'Decimal';
2018-03-17 16:42:28 +00:00
case 'float':
return 'Float';
2018-03-17 16:42:28 +00:00
case 'json':
return 'Mixed';
case 'biginteger':
case 'integer':
return 'Number';
2016-07-14 11:06:58 +02:00
case 'uuid':
return 'ObjectId';
2018-03-17 16:42:28 +00:00
case 'email':
2016-07-14 11:06:58 +02:00
case 'enumeration':
2018-03-17 16:42:28 +00:00
case 'password':
case 'string':
case 'text':
2016-07-14 11:06:58 +02:00
return 'String';
default:
}
}
2016-09-28 11:42:26 +02:00
};
};