Registry / database / mongoose-extend-schema

mongoose-extend-schema

JSON →
library1.0.0jsnpmunverified

A lightweight utility (v1.0.0, no recent updates) that extends a Mongoose schema by merging its fields with additional definitions. Unlike mongoose-extend-schema, native Mongoose does not provide a built-in schema inheritance method; this package offers a simple shallow merge approach. It is installed from npm, requires Node >=8.2.1 and Mongoose as a peer dependency. While straightforward, it has known issues: it only shallow-copies schema fields, does not copy indexes or other schema options, and can cause silent overrides if field names conflict. Use with caution for simple schema extension needs.

npm install mongoose-extend-schema
INSTALL
IMPORT
SIG · MONGOOSE-EXTEND-SC
M
mongoose-extend-schema
databasejavascriptv1.0.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.

extendSchema (default)
import extendSchema from 'mongoose-extend-schema';
const { extendSchema } = require('mongoose-extend-schema');
Package has no default export in ESM; use default import or CommonJS.
extendSchema (CommonJS)
const extendSchema = require('mongoose-extend-schema');
const { extendSchema } = require('mongoose-extend-schema');
The package uses module.exports = function, so you must assign the entire require result, not destructure.
extendSchema with options
const extendedSchema = extendSchema(baseSchema, definitions, { _id: false });
const extendedSchema = extendSchema(baseSchema, definitions, false);
Third argument must be an options object (e.g., { _id: false }), not a boolean or other primitive.

Demonstrates how to extend a Mongoose schema using mongoose-extend-schema, including connection and saving a document.

const mongoose = require('mongoose'); const extendSchema = require('mongoose-extend-schema'); const baseSchema = new mongoose.Schema({ name: String, email: { type: String, required: true } }); const extendedSchema = extendSchema(baseSchema, { phone: { type: String, required: true }, age: Number }); const ExtendedModel = mongoose.model('Extended', extendedSchema); async function run() { await mongoose.connect(process.env.MONGO_URI || 'mongodb://localhost:27017/test'); const doc = new ExtendedModel({ name: 'Alice', email: 'alice@example.com', phone: '123-456' }); await doc.save(); console.log('Saved:', doc); } run().catch(err => console.error(err));
Debug
Known issues
gotchaShallow merge: nested schema objects (e.g., subdocuments, arrays of embedded schemas) are not deeply merged; they may be overwritten or cause unexpected behavior.
fix
Manually deep-merge definitions or avoid nested structures.
affects: >=1.0.0
gotchaDoes not copy schema options (e.g., timestamps, toJSON, toObject) from the base schema to the extended one.
fix
Explicitly pass options as the third argument to extendSchema: extendSchema(base, defs, base.options).
affects: >=1.0.0
gotchaIndexes defined on the base schema are not carried over to the extended schema; you must redefine indexes on the extended schema.
fix
Add indexes manually to the extended schema after creation.
affects: >=1.0.0
breakingIf you define a field with the same name but different type in the extended schema, it silently overrides the base field without warning.
fix
Ensure field names are unique or carefully review overrides.
affects: >=1.0.0
gotchaThe package wraps schema creation inside a function, so error messages from Mongoose may reference an anonymous schema instead of the original name, making debugging harder.
fix
Use the 'options' argument to set a schema name: extendSchema(base, defs, { _id: false, id: false, ... })
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'extendSchema' of 'require(...)' as it is undefined.
Using ES6 destructuring on the CommonJS require result, which treats the function as the default export.
fix
Replace const { extendSchema } = require('mongoose-extend-schema'); with const extendSchema = require('mongoose-extend-schema');
Schema hasn't been registered for model "X". Use mongoose.model(name, schema)
Calling mongoose.model() with a schema that was not correctly created or registered.
fix
Ensure extendSchema returns a valid mongoose.Schema instance: const schema = extendSchema(...); mongoose.model('X', schema);
Cannot read property 'obj' of undefined
Passing an undefined or non-Schema object as the first argument to extendSchema.
fix
Check that the first argument is a valid mongoose.Schema instance, e.g., const base = new mongoose.Schema({...}); extendSchema(base, {...});
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
mongooserequiredpeer dependency required to create schemas
Agent activity
9 hits · last 30 days
node
8
Resources
mongoose-extend-schema — npm install mongoose-extend-schema · libregistry