Registry / database / mongoose-int32

mongoose-int32

JSON →
library0.6.0jsnpmunverified

Mongoose custom type for storing 32-bit signed integers (BSON Type 16) in MongoDB. Current stable version is 0.6.0. Works with Mongoose 4.4+, 5.x, and 6.x. The main purpose is to enforce Int32 representation instead of default 64-bit doubles for fields where exact 32-bit integer storage is required. Key differentiator: it prevents accidental type coercion to 64-bit floats. Release cadence: low, no recent updates.

npm install mongoose-int32
INSTALL
IMPORT
SIG · MONGOOSE-INT32
M
mongoose-int32
databasejavascriptv0.6.0
harness data pending
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 }
Debug
Known issues
gotchaloadType must be called before defining any schemas that use Int32; otherwise, the type will not be recognized.
fix
Call loadType(mongoose) first, then define schemas.
affects: all
deprecatedMongoose 6.x and later have built-in Int32 support via mongoose.Types.Int32. This package may be unnecessary.
fix
Consider using native Mongoose Int32 if using Mongoose 6+. Import from 'mongoose' directly.
affects: >=6.0.0
gotchaInt32 values outside the 32-bit signed integer range will be truncated or cause validation errors.
fix
Validate values to be within -2147483648 and 2147483647 before assigning.
affects: all
Errors
Common errors & fixes
TypeError: mongoose.Types.Int32 is not a constructor
loadType was not called or was called after schema definition
fix
Call 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
fix
Ensure 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)
fix
Ensure values are within the 32-bit signed integer range.
Upgrade
Version history
0.6.0latest on npm
Audit
Dependencies
mongooserequiredpeer dependency; the type is loaded into a Mongoose instance
Agent activity
2 hits · last 30 days
node
2
Resources
mongoose-int32 — npm install mongoose-int32 · libregistry