Mongoose helper for Lad, which is used as an alternative to the default
mongoose.connect()
for multiple connection management. See the Forward Email codebase for further insight into setup.
Requires mongoose v6+ to be a dependency in your project.
npm:
npm install @ladjs/mongoose mongoose
const Mongoose = require('@ladjs/mongoose');
const { Schema } = require('mongoose');
(async () => {
const m = new Mongoose({
mongo: {
options: { heartbeatFrequencyMS: 100, serverSelectionTimeoutMS: 1000 }
}
});
const conn1 = await m.createConnection('mongodb://server-one.example.com/database-name').asPromise();
const conn2 = await m.createConnection('mongodb://server-two.example.com/database-name').asPromise();
const UserSchema = new Schema({ name: String });
const LogSchema = new Schema({ name: String });
const Users = conn1.model('Users', UserSchema);
const Logs = conn2.model('Logs', LogSchema);
// write to the server-one.example.com database
await Users.create({ name: 'test' });
// write to the server-two.example.com database
await Logs.create({ name: 'test' });
})();
Note that instances of this class Mongoose
only have the method createConnection
. It is not 1:1 with mongoose
normal singleton.
You should use @ladjs/graceful to manage process SIGHUP and graceful exits.
Option | Type | Default | Description |
---|---|---|---|
logger |
Object | console |
Custom logger function, see cabin or axe as a default if desired. |
mongo |
Object | --- | See below uri and options properties. |
mongo.uri |
String | undefined | mongo connection URI which is passed as the first argument to mongoose.createConnection see mongo options docs |
mongo.options |
Object | undefined | mongo connection options which is passed as the second argument to mongoose.createConnection see mongo options docs |
bindEvents |
Boolean | true |
Whether or not to bind default events to the Mongoose connection using provided logger . |
hideMeta |
String or false Boolean |
"hide_meta" |
Appends a true boolean property to a property with this value in logs, e.g. console.log('mongoose disconnected', { hide_meta: true }); which is useful for preventing metadata object from being invoked as the second argument (this is meant for usage with Cabin and Axe and made for Forward Email). If you pass a false value then this property will not get populated. |
--- | --- | --- | --- |
allowDiskUse |
Boolean | undefined | set with mongoose.set |
applyPluginsToChildSchemas |
Boolean | undefined | set with mongoose.set |
applyPluginsToDiscriminators |
Boolean | undefined | set with mongoose.set |
autoCreate |
Boolean | undefined | set with mongoose.set |
autoIndex |
Boolean | undefined | set with mongoose.set |
bufferCommands |
Boolean | undefined | set with mongoose.set |
bufferTimeoutMS |
Boolean | undefined | set with mongoose.set |
debug |
Boolean | If process.env.MONGOOSE_DEBUG is truthy, then it is true , otherwise false |
set with mongoose.set |
id |
Boolean | undefined | set with mongoose.set |
timestamps.createdAt.immutable |
Boolean | undefined | set with mongoose.set |
maxTimeMS |
Number | 10000 |
set with mongoose.set |
objectIdGetter |
Boolean | undefined | set with mongoose.set |
overwriteModels |
Boolean | undefined | set with mongoose.set |
returnOriginal |
Boolean | undefined | set with mongoose.set |
runValidators |
Boolean | undefined | set with mongoose.set |
sanitizeFilter |
Boolean | undefined | set with mongoose.set |
selectPopulatedPaths |
Boolean | undefined | set with mongoose.set |
strict |
Boolean | true |
set with mongoose.set |
strictQuery |
Boolean | true |
set with mongoose.set |
toJSON |
Boolean | undefined | set with mongoose.set |
toObject |
Boolean | undefined | set with mongoose.set |
Name | Website |
---|---|
Nick Baugh | http://niftylettuce.com |
Spencer Snyder | http://spencersnyder.io |