Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Int32
✓ import mongoose from 'mongoose';
import { loadType } from 'mongoose-int32';
loadType(mongoose);
const Int32 = mongoose.Types.Int32;
✗ import Int32 from 'mongoose-int32'; // Symbol not exported directly
The package exports a loadType function which must be called with a Mongoose instance. Int32 is then available as mongoose.Types.Int32.
loadType
✓ import { loadType } from 'mongoose-int32';
loadType(mongoose);
✗ import loadType from 'mongoose-int32'; // This is correct as of v0.6.0, but named import is preferred
loadType is also available as default export: import loadType from 'mongoose-int32';
Int32Type (Schema)
✓ const schema = new mongoose.Schema({ myField: mongoose.Types.Int32 });
✗ const schema = new mongoose.Schema({ myField: Int32 }); // Int32 not in scope unless loaded
After loadType(mongoose), the Int32 type is attached to mongoose.Types, not the imported variable.
Shows how to load the Int32 type, define a schema field, and create a document with an Int32 value.
const mongoose = require('mongoose');
const { loadType } = require('mongoose-int32');
loadType(mongoose);
const Int32 = mongoose.Types.Int32;
const schema = new mongoose.Schema({
count: { type: Int32, required: true }
});
const Model = mongoose.model('Item', schema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/test');
const item = await Model.create({ count: 100 });
console.log('Count:', item.count); // 100 as Int32
}
Errors
Common errors & fixes
TypeError: mongoose.Types.Int32 is not a constructor
loadType was not called or was called after schema definition
fixCall loadType(mongoose) before any schema uses Int32.
SchemaType 'Int32' is not a valid type at path 'count'
Int32 type not registered because loadType failed or not called
fixEnsure loadType(mongoose) is executed without errors.
CastError: Cast to Int32 failed for value "12345678901" at path "count"
Value exceeds 32-bit integer range (12345678901 > 2147483647)
fixEnsure values are within the 32-bit signed integer range.
Audit
Dependencies
mongooserequiredpeer dependency; the type is loaded into a Mongoose instance