-
Notifications
You must be signed in to change notification settings - Fork 0
feat(NODE-6503): Add Int32 as a SchemaType #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
05ce588
df7c715
68f2ebd
021f84b
538629c
4492a3e
8226905
7c6ff5c
c5dc226
9537974
49804ed
c6bbafb
18d18a7
0e4b257
010d1e2
0f78bf1
9dcada3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ module.exports.Decimal128 = mongoose.Decimal128; | |
| module.exports.Mixed = mongoose.Mixed; | ||
| module.exports.Date = mongoose.Date; | ||
| module.exports.Number = mongoose.Number; | ||
| module.exports.Int32 = mongoose.Int32; | ||
|
||
| module.exports.Error = mongoose.Error; | ||
| module.exports.MongooseError = mongoose.MongooseError; | ||
| module.exports.now = mongoose.now; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
|
|
||
| /** | ||
| * Given a value, cast it to a Int32, or throw an `Error` if the value | ||
| * cannot be casted. `null` and `undefined` are considered valid. | ||
| * | ||
| * @param {Any} value | ||
| * @return {Number} | ||
| * @throws {Error} if `value` does not represent an integer, or is beyond the bounds of an 32-bit integer. | ||
| * @api private | ||
| */ | ||
|
|
||
| module.exports = function castInt32(val) { | ||
| if (val == null) { | ||
| return val; | ||
| } | ||
| if (val === '') { | ||
| return null; | ||
| } | ||
|
|
||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (typeof val === 'string' || typeof val === 'number') { | ||
|
|
||
| const INT32_MAX = 0x7FFFFFFF; | ||
| const INT32_MIN = -0x80000000; | ||
|
|
||
| const _val = Number(val); | ||
baileympearson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (_val === (_val | 0) && _val > INT32_MIN && _val < INT32_MAX) { | ||
| return _val; | ||
| } | ||
| assert.ok(false); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| module.exports = require('bson').Int32; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -987,6 +987,7 @@ Mongoose.prototype.VirtualType = VirtualType; | |
| * - [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids) | ||
| * - [Map](https://mongoosejs.com/docs/schematypes.html#maps) | ||
| * - [Subdocument](https://mongoosejs.com/docs/schematypes.html#schemas) | ||
| * - [Int32](https://mongoosejs.com/docs/schematypes.html#int32) | ||
| * | ||
| * Using this exposed access to the `ObjectId` type, we can construct ids on demand. | ||
| * | ||
|
|
@@ -1138,6 +1139,22 @@ Mongoose.prototype.syncIndexes = function(options) { | |
|
|
||
| Mongoose.prototype.Decimal128 = SchemaTypes.Decimal128; | ||
|
|
||
|
|
||
| /** | ||
| * The Mongoose Int32 [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for | ||
| * declaring paths in your schema that should be 32-bit integers. | ||
| * Do not use this to create a new Int32 instance, use `mongoose.Types.Int32` | ||
| * instead. | ||
| * | ||
| * #### Example: | ||
| * | ||
| * const vehicleSchema = new Schema({ numTires: mongoose.Int32 }); | ||
| * | ||
| * @property Int32 | ||
| * @api public | ||
| */ | ||
|
||
| Mongoose.prototype.Int32 = SchemaTypes.Int32; | ||
|
|
||
| /** | ||
| * The Mongoose Mixed [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for | ||
| * declaring paths in your schema that Mongoose's change tracking, casting, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.