Skip to content

Commit

Permalink
Merge pull request #11520 from Uzlopak/extract-mongooseoptions
Browse files Browse the repository at this point in the history
chore: extract and improve mongooseoptions
  • Loading branch information
vkarpov15 committed Mar 15, 2022
2 parents 7cd091b + 8626396 commit f5fe027
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 112 deletions.
113 changes: 1 addition & 112 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
/// <reference path="./cursor.d.ts" />
/// <reference path="./document.d.ts" />
/// <reference path="./error.d.ts" />
/// <reference path="./mongooseoptions.d.ts" />
/// <reference path="./pipelinestage.d.ts" />
/// <reference path="./schemaoptions.d.ts" />

import events = require('events');
import mongodb = require('mongodb');
import mongoose = require('mongoose');
import stream = require('stream');

declare module 'mongoose' {

Expand Down Expand Up @@ -175,117 +175,6 @@ declare module 'mongoose' {

type Mongoose = typeof mongoose;

interface MongooseOptions {
/** true by default. Set to false to skip applying global plugins to child schemas */
applyPluginsToChildSchemas?: boolean;

/**
* false by default. Set to true to apply global plugins to discriminator schemas.
* This typically isn't necessary because plugins are applied to the base schema and
* discriminators copy all middleware, methods, statics, and properties from the base schema.
*/
applyPluginsToDiscriminators?: boolean;

/**
* Set to `true` to make Mongoose call` Model.createCollection()` automatically when you
* create a model with `mongoose.model()` or `conn.model()`. This is useful for testing
* transactions, change streams, and other features that require the collection to exist.
*/
autoCreate?: boolean;

/**
* true by default. Set to false to disable automatic index creation
* for all models associated with this Mongoose instance.
*/
autoIndex?: boolean;

/** enable/disable mongoose's buffering mechanism for all connections and models */
bufferCommands?: boolean;

bufferTimeoutMS?: number;

/** false by default. Set to `true` to `clone()` all schemas before compiling into a model. */
cloneSchemas?: boolean;

/**
* If `true`, prints the operations mongoose sends to MongoDB to the console.
* If a writable stream is passed, it will log to that stream, without colorization.
* If a callback function is passed, it will receive the collection name, the method
* name, then all arguments passed to the method. For example, if you wanted to
* replicate the default logging, you could output from the callback
* `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
*/
debug?:
| boolean
| { color?: boolean; shell?: boolean }
| stream.Writable
| ((collectionName: string, methodName: string, ...methodArgs: any[]) => void);

/** If set, attaches [maxTimeMS](https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/) to every query */
maxTimeMS?: number;

/**
* true by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that
* returns `this` for convenience with populate. Set this to false to remove the getter.
*/
objectIdGetter?: boolean;

/**
* Set to `true` to default to overwriting models with the same name when calling
* `mongoose.model()`, as opposed to throwing an `OverwriteModelError`.
*/
overwriteModels?: boolean;

/**
* If `false`, changes the default `returnOriginal` option to `findOneAndUpdate()`,
* `findByIdAndUpdate`, and `findOneAndReplace()` to false. This is equivalent to
* setting the `new` option to `true` for `findOneAndX()` calls by default. Read our
* `findOneAndUpdate()` [tutorial](https://mongoosejs.com/docs/tutorials/findoneandupdate.html)
* for more information.
*/
returnOriginal?: boolean;

/**
* false by default. Set to true to enable [update validators](
* https://mongoosejs.com/docs/validation.html#update-validators
* ) for all validators by default.
*/
runValidators?: boolean;

sanitizeFilter?: boolean;

sanitizeProjection?: boolean;

/**
* true by default. Set to false to opt out of Mongoose adding all fields that you `populate()`
* to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
*/
selectPopulatedPaths?: boolean;

setDefaultsOnInsert?: boolean;

/** true by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas. */
strict?: boolean | 'throw';

/** true by default. set to `false` to allow populating paths that aren't in the schema */
strictPopulate?: boolean;

/**
* false by default, may be `false`, `true`, or `'throw'`. Sets the default
* [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
*/
strictQuery?: boolean | 'throw';

/**
* `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to
* `toJSON()`, for determining how Mongoose documents get serialized by `JSON.stringify()`
*/
toJSON?: ToObjectOptions;

/** `{ transform: true, flattenDecimals: true }` by default. Overwrites default objects to `toObject()` */
toObject?: ToObjectOptions;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface ClientSession extends mongodb.ClientSession { }

Expand Down
180 changes: 180 additions & 0 deletions types/mongooseoptions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import stream = require('stream');

declare module 'mongoose' {

interface MongooseOptions {
/**
* Set to false to skip applying global plugins to child schemas.
*
* @default true
*/
applyPluginsToChildSchemas?: boolean;

/**
* Set to true to apply global plugins to discriminator schemas.
* This typically isn't necessary because plugins are applied to the base schema and
* discriminators copy all middleware, methods, statics, and properties from the base schema.
*
* @default false
*/
applyPluginsToDiscriminators?: boolean;

/**
* autoCreate is true by default unless readPreference is secondary or secondaryPreferred,
* which means Mongoose will attempt to create every model's underlying collection before
* creating indexes. If readPreference is secondary or secondaryPreferred, Mongoose will
* default to false for both autoCreate and autoIndex because both createCollection() and
* createIndex() will fail when connected to a secondary.
*/
autoCreate?: boolean;

/**
* Set to false to disable automatic index creation for all models associated with this Mongoose instance.
*
* @default true
*/
autoIndex?: boolean;

/**
* enable/disable mongoose's buffering mechanism for all connections and models.
*
* @default true
*/
bufferCommands?: boolean;

/**
* If bufferCommands is on, this option sets the maximum amount of time Mongoose
* buffering will wait before throwing an error.
* If not specified, Mongoose will use 10000 (10 seconds).
*
* @default 10000
*/
bufferTimeoutMS?: number;

/**
* Set to `true` to `clone()` all schemas before compiling into a model.
*
* @default false
*/
cloneSchemas?: boolean;

/**
* If `true`, prints the operations mongoose sends to MongoDB to the console.
* If a writable stream is passed, it will log to that stream, without colorization.
* If a callback function is passed, it will receive the collection name, the method
* name, then all arguments passed to the method. For example, if you wanted to
* replicate the default logging, you could output from the callback
* `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
*
* @default false
*/
debug?:
| boolean
| { color?: boolean; shell?: boolean; }
| stream.Writable
| ((collectionName: string, methodName: string, ...methodArgs: any[]) => void);

/** If set, attaches [maxTimeMS](https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/) to every query */
maxTimeMS?: number;

/**
* Mongoose adds a getter to MongoDB ObjectId's called `_id` that
* returns `this` for convenience with populate. Set this to false to remove the getter.
*
* @default true
*/
objectIdGetter?: boolean;

/**
* Set to `true` to default to overwriting models with the same name when calling
* `mongoose.model()`, as opposed to throwing an `OverwriteModelError`.
*
* @default false
*/
overwriteModels?: boolean;

/**
* If `false`, changes the default `returnOriginal` option to `findOneAndUpdate()`,
* `findByIdAndUpdate`, and `findOneAndReplace()` to false. This is equivalent to
* setting the `new` option to `true` for `findOneAndX()` calls by default. Read our
* `findOneAndUpdate()` [tutorial](https://mongoosejs.com/docs/tutorials/findoneandupdate.html)
* for more information.
*
* @default true
*/
returnOriginal?: boolean;

/**
* Set to true to enable [update validators](
* https://mongoosejs.com/docs/validation.html#update-validators
* ) for all validators by default.
*
* @default false
*/
runValidators?: boolean;

/**
* Sanitizes query filters against [query selector injection attacks](
* https://thecodebarbarian.com/2014/09/04/defending-against-query-selector-injection-attacks.html
* ) by wrapping any nested objects that have a property whose name starts with $ in a $eq.
*/
sanitizeFilter?: boolean;

sanitizeProjection?: boolean;

/**
* Set to false to opt out of Mongoose adding all fields that you `populate()`
* to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
*
* @default true
*/
selectPopulatedPaths?: boolean;

/**
* Mongoose also sets defaults on update() and findOneAndUpdate() when the upsert option is
* set by adding your schema's defaults to a MongoDB $setOnInsert operator. You can disable
* this behavior by setting the setDefaultsOnInsert option to false.
*
* @default true
*/
setDefaultsOnInsert?: boolean;

/**
* Sets the default strict mode for schemas.
* May be `false`, `true`, or `'throw'`.
*
* @default true
*/
strict?: boolean | 'throw';

/**
* Set to `false` to allow populating paths that aren't in the schema.
*
* @default true
*/
strictPopulate?: boolean;

/**
* Sets the default [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
* May be `false`, `true`, or `'throw'`.
*
* @default false
*/
strictQuery?: boolean | 'throw';

/**
* Overwrites default objects to `toJSON()`, for determining how Mongoose
* documents get serialized by `JSON.stringify()`
*
* @default { transform: true, flattenDecimals: true }
*/
toJSON?: ToObjectOptions;

/**
* Overwrites default objects to `toObject()`
*
* @default { transform: true, flattenDecimals: true }
*/
toObject?: ToObjectOptions;
}
}

0 comments on commit f5fe027

Please sign in to comment.