forked from lykmapipo/mongoose-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
240 lines (195 loc) · 5.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict';
//dependencies
var path = require('path');
var Money = require('moneyjs');
var mongoose = require('mongoose');
var SchemaType = mongoose.SchemaType;
var errorMessages = mongoose.Error.messages;
//path mongoose Query to allow money type
require(path.join(__dirname, 'lib', 'query'));
/**
* @constructor
* @description mongoose money SchemaType
* @param {String} path
* @param {Object} options
* @inherits SchemaType
* @api private
*/
function MongooseMoney(path, options) {
SchemaType.call(this, path, options, 'Money');
}
/**
* @description specifies schema type's name, to defend against
* minifiers that mangle function names.
* @api private
*/
MongooseMoney.schemaName = 'Money';
/**
* @description inherits from mongoose SchemaType schema type.
*/
MongooseMoney.prototype = Object.create(SchemaType.prototype);
MongooseMoney.prototype.constructor = Money;
/**
* @function
* @description implement checkRequired method
* @param {any} value
* @return {Boolean}
*/
MongooseMoney.prototype.checkRequired = function checkRequired(value) {
return (value !== null) && (value instanceof Money);
};
/**
* @function
* @description sets a maximum money validator
* @param {Money} value minimum money allowed
* @param {String} [message] optional custom error message
* @return {MongooseMoney} this
* @see Customized Error Messages #error_messages_MongooseError-messages
* @api public
*/
MongooseMoney.prototype.min = function(value, message) {
// var self = this;
if (this.minValidator) {
this.validators = this.validators.filter(function(v) {
return v.validator !== this.minValidator;
}, this);
}
if (null !== value) {
var msg = message || errorMessages.Number.min;
msg = msg.replace(/{MIN}/, value);
this.minValidator = function(v) {
return v !== null && v.isGreaterThan(value);
};
this.validators.push({
validator: this.minValidator,
message: msg,
type: 'min'
});
}
return this;
};
/**
* @function
* @description sets a maximum money validator
* @param {Money} value maximum money allowed
* @param {String} [message] optional custom error message
* @return {MongooseMoney} this
* @see Customized Error Messages #error_messages_MongooseError-messages
* @api public
*/
MongooseMoney.prototype.max = function(value, message) {
if (this.maxValidator) {
this.validators = this.validators.filter(function(v) {
return v.validator !== this.maxValidator;
}, this);
}
if (null !== value) {
var msg = message || errorMessages.Number.max;
msg = msg.replace(/{MAX}/, value);
this.maxValidator = function(v) {
return v !== null && v.isLessThan(value);
};
this.validators.push({
validator: this.maxValidator,
message: msg,
type: 'max'
});
}
return this;
};
/**
* @function
* @description casts to money
* @param {Object} value the value to cast
* @param {Document} doc document that triggers the casting
* @param {Boolean} [init]
*/
MongooseMoney.prototype.cast = function(value /*,doc , init*/ ) {
//TODO make use of base currency to cast value to
//money
// console.log(value);
if (null === value) {
return value;
}
if ('' === value) {
return null;
}
if (value instanceof Money) {
return value;
}
if (value && value.amount && value.currency) {
if (value.time && value.time instanceof Date) {
return new Money(
value.amount,
Money[value.currency],
value.time
);
} else if (value.time && value.time instanceof String) {
return new Money(
value.amount,
Money[value.currency],
new Date(value.time)
);
} else {
return new Money(value.amount, Money[value.currency]);
}
}
throw new SchemaType.CastError('MongooseMoney', value);
};
/*!
* ignore
*/
function handleSingle(val) {
/*jshint validthis:true*/
return this.cast(val);
}
//query conditions that are supported for money schema types
MongooseMoney.prototype.$conditionalHandlers = {
'$gt': handleSingle,
'$gte': handleSingle,
'$lt': handleSingle,
'$lte': handleSingle,
'$ne': handleSingle
};
/**
* Casts contents for queries.
*
* @param {String} $conditional
* @param {any} [value] value to be casted for query
* @api private
*/
MongooseMoney.prototype.castForQuery = function($conditional, val) {
if (arguments.length === 2) {
var handler = this.$conditionalHandlers[$conditional];
if (!handler) {
throw new Error('Can\'t use ' + $conditional + ' with Money.');
}
return handler.call(this, val).valueOf();
} else {
return this.cast($conditional).valueOf();
}
};
//------------------------------------------------------------------------------
// Attach Types
//------------------------------------------------------------------------------
//extend mongoose schema types with money type
if (!mongoose.Schema.Types.Money) {
mongoose.Schema.Types.Money = MongooseMoney;
}
//extend mongoose schema types with money type
if (!mongoose.Types.Money) {
mongoose.Types.Money = MongooseMoney;
}
//------------------------------------------------------------------------------
// Plugins
//------------------------------------------------------------------------------
//auto index Money fields
mongoose.plugin(function(schema) {
//iterate over schema paths
schema.eachPath(function(pathName, schemaType) {
//add index to money fields
if (schemaType.instance === 'Money') {
schemaType.options.index = true;
}
});
});