Skip to content

Commit 021f84b

Browse files
requested changes
1 parent 68f2ebd commit 021f84b

File tree

23 files changed

+157
-93
lines changed

23 files changed

+157
-93
lines changed

docs/guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ The permitted SchemaTypes are:
8080
* [Decimal128](api/mongoose.html#mongoose_Mongoose-Decimal128)
8181
* [Map](schematypes.html#maps)
8282
* [UUID](schematypes.html#uuid)
83+
* [Int32](schematypes.html#int32)
8384

8485
Read more about [SchemaTypes here](schematypes.html).
8586

docs/schematypes.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Check out [Mongoose's plugins search](http://plugins.mongoosejs.io) to find plug
5555
* [Schema](#schemas)
5656
* [UUID](#uuid)
5757
* [BigInt](#bigint)
58+
* [Int32](#int32)
5859

5960
### Example
6061

@@ -68,6 +69,7 @@ const schema = new Schema({
6869
mixed: Schema.Types.Mixed,
6970
_someId: Schema.Types.ObjectId,
7071
decimal: Schema.Types.Decimal128,
72+
32bitInt: Schema.Types.Int32,
7173
array: [],
7274
ofString: [String],
7375
ofNumber: [Number],
@@ -647,6 +649,21 @@ const question = new Question({ answer: 42n });
647649
typeof question.answer; // 'bigint'
648650
```
649651

652+
### Int32 {#int32}
653+
654+
Mongoose supports 32-bit integers as a SchemaType.
655+
Int32s are stored as [32-bit integers in MongoDB (BSON type "int")](https://www.mongodb.com/docs/manual/reference/bson-types/).
656+
657+
```javascript
658+
const studentsSchema = new Schema({
659+
id: Int32
660+
});
661+
const Student = mongoose.model('Student', schema);
662+
663+
const student = new Student({ id: 1339 });
664+
typeof student.id; // 'number'
665+
```
666+
650667
## Getters {#getters}
651668

652669
Getters are like virtuals for paths defined in your schema. For example,

docs/source/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const files = [
4040
'lib/types/subdocument.js',
4141
'lib/types/arraySubdocument.js',
4242
'lib/types/buffer.js',
43-
'lib/types/.js',
43+
'lib/types/decimal128.js',
44+
'lib/types/int32.js',
4445
'lib/types/map.js',
4546
'lib/types/array/methods/index.js'
4647
];

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module.exports.Decimal128 = mongoose.Decimal128;
4646
module.exports.Mixed = mongoose.Mixed;
4747
module.exports.Date = mongoose.Date;
4848
module.exports.Number = mongoose.Number;
49+
module.exports.Int32 = mongoose.Int32;
4950
module.exports.Error = mongoose.Error;
5051
module.exports.MongooseError = mongoose.MongooseError;
5152
module.exports.now = mongoose.now;

lib/browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ exports.Schema = require('./schema');
5050
* - [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids)
5151
* - [Map](https://mongoosejs.com/docs/schematypes.html#maps)
5252
* - [Subdocument](https://mongoosejs.com/docs/schematypes.html#schemas)
53-
* - Int32
53+
* - [Int32](https://mongoosejs.com/docs/schematypes.html#int32)
5454
*
5555
* Using this exposed access to the `ObjectId` type, we can construct ids on demand.
5656
*

lib/cast/int32.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = function castInt32(val) {
2727

2828
const _val = Number(val);
2929

30-
if (!isNaN(_val) && _val === Math.round(_val) && _val > INT32_MIN && _val < INT32_MAX) {
30+
if (_val !== _val | 0 && _val > INT32_MIN && _val < INT32_MAX) {
3131
return _val;
3232
}
3333
assert.ok(false);

lib/drivers/browser/int32.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*!
2+
* ignore
3+
*/
4+
5+
'use strict';
6+
7+
module.exports = require('bson').Int32;

lib/mongoose.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ Mongoose.prototype.VirtualType = VirtualType;
987987
* - [ObjectId](https://mongoosejs.com/docs/schematypes.html#objectids)
988988
* - [Map](https://mongoosejs.com/docs/schematypes.html#maps)
989989
* - [Subdocument](https://mongoosejs.com/docs/schematypes.html#schemas)
990-
* - Int32
990+
* - [Int32](https://mongoosejs.com/docs/schematypes.html#int32)
991991
*
992992
* Using this exposed access to the `ObjectId` type, we can construct ids on demand.
993993
*
@@ -1142,15 +1142,15 @@ Mongoose.prototype.Decimal128 = SchemaTypes.Decimal128;
11421142

11431143
/**
11441144
* The Mongoose Int32 [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
1145-
* declaring paths in your schema that should be 32-bit integers floating points.
1145+
* declaring paths in your schema that should be 32-bit integers.
11461146
* Do not use this to create a new Int32 instance, use `mongoose.Types.Int32`
11471147
* instead.
11481148
*
11491149
* #### Example:
11501150
*
1151-
* const vehicleSchema = new Schema({ numberOfCows: mongoose.Int32 });
1151+
* const vehicleSchema = new Schema({ numTires: mongoose.Int32 });
11521152
*
1153-
* @property Decimal128
1153+
* @property Int32
11541154
* @api public
11551155
*/
11561156
Mongoose.prototype.Int32 = SchemaTypes.Int32;

lib/query.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,6 @@ Query.prototype.getOptions = function() {
16341634
* - [projection](https://mongoosejs.com/docs/api/query.html#Query.prototype.projection())
16351635
* - sanitizeProjection
16361636
* - useBigInt64
1637-
* - promoteValues
16381637
*
16391638
* The following options are only for all operations **except** `updateOne()`, `updateMany()`, `deleteOne()`, and `deleteMany()`:
16401639
*

lib/schema.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,8 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
14941494
name = 'ObjectId';
14951495
} else if (type === Types.Decimal128) {
14961496
name = 'Decimal128';
1497+
} else if (type === Types.Int32) {
1498+
name = 'Int32';
14971499
} else {
14981500
name = type == null ? '' + type : type.toString();
14991501
}

0 commit comments

Comments
 (0)