UNPKG

11.1 kBTypeScriptView Raw
1declare module 'mongoose' {
2 import mongodb = require('mongodb');
3
4 interface SchemaTimestampsConfig {
5 createdAt?: boolean | string;
6 updatedAt?: boolean | string;
7 currentTime?: () => (NativeDate | number);
8 }
9
10 type TypeKeyBaseType = string;
11
12 type DefaultTypeKey = 'type';
13 interface SchemaOptions<
14 DocType = unknown,
15 TInstanceMethods = {},
16 QueryHelpers = {},
17 TStaticMethods = {},
18 TVirtuals = {},
19 THydratedDocumentType = HydratedDocument<DocType, TInstanceMethods, QueryHelpers>
20 > {
21 /**
22 * By default, Mongoose's init() function creates all the indexes defined in your model's schema by
23 * calling Model.createIndexes() after you successfully connect to MongoDB. If you want to disable
24 * automatic index builds, you can set autoIndex to false.
25 */
26 autoIndex?: boolean;
27 /**
28 * Similar to autoIndex, except for automatically creates any Atlas search indexes defined in your
29 * schema. Unlike autoIndex, this option defaults to false.
30 */
31 autoSearchIndex?: boolean;
32 /**
33 * If set to `true`, Mongoose will call Model.createCollection() to create the underlying collection
34 * in MongoDB if autoCreate is set to true. Calling createCollection() sets the collection's default
35 * collation based on the collation option and establishes the collection as a capped collection if
36 * you set the capped schema option.
37 */
38 autoCreate?: boolean;
39 /**
40 * By default, mongoose buffers commands when the connection goes down until the driver manages to reconnect.
41 * To disable buffering, set bufferCommands to false.
42 */
43 bufferCommands?: boolean;
44 /**
45 * If bufferCommands is on, this option sets the maximum amount of time Mongoose buffering will wait before
46 * throwing an error. If not specified, Mongoose will use 10000 (10 seconds).
47 */
48 bufferTimeoutMS?: number;
49 /**
50 * Mongoose supports MongoDBs capped collections. To specify the underlying MongoDB collection be capped, set
51 * the capped option to the maximum size of the collection in bytes.
52 */
53 capped?: boolean | number | { size?: number; max?: number; autoIndexId?: boolean; };
54 /** Sets a default collation for every query and aggregation. */
55 collation?: mongodb.CollationOptions;
56
57 /** Arbitrary options passed to `createCollection()` */
58 collectionOptions?: mongodb.CreateCollectionOptions;
59
60 /** The timeseries option to use when creating the model's collection. */
61 timeseries?: mongodb.TimeSeriesCollectionOptions;
62
63 /** The number of seconds after which a document in a timeseries collection expires. */
64 expireAfterSeconds?: number;
65
66 /** The time after which a document in a timeseries collection expires. */
67 expires?: number | string;
68
69 /**
70 * Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName
71 * method. This method pluralizes the name. Set this option if you need a different name for your collection.
72 */
73 collection?: string;
74 /**
75 * When you define a [discriminator](/docs/discriminators.html), Mongoose adds a path to your
76 * schema that stores which discriminator a document is an instance of. By default, Mongoose
77 * adds an `__t` path, but you can set `discriminatorKey` to overwrite this default.
78 *
79 * @default '__t'
80 */
81 discriminatorKey?: string;
82
83 /**
84 * Option for nested Schemas.
85 *
86 * If true, skip building indexes on this schema's path.
87 *
88 * @default false
89 */
90 excludeIndexes?: boolean;
91 /**
92 * Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field
93 * cast to a string, or in the case of ObjectIds, its hexString.
94 */
95 id?: boolean;
96 /**
97 * Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema
98 * constructor. The type assigned is an ObjectId to coincide with MongoDB's default behavior. If you
99 * don't want an _id added to your schema at all, you may disable it using this option.
100 */
101 _id?: boolean;
102 /**
103 * Mongoose will, by default, "minimize" schemas by removing empty objects. This behavior can be
104 * overridden by setting minimize option to false. It will then store empty objects.
105 */
106 minimize?: boolean;
107 /**
108 * Optimistic concurrency is a strategy to ensure the document you're updating didn't change between when you
109 * loaded it using find() or findOne(), and when you update it using save(). Set to `true` to enable
110 * optimistic concurrency.
111 */
112 optimisticConcurrency?: boolean;
113 /**
114 * If `plugin()` called with tags, Mongoose will only apply plugins to schemas that have
115 * a matching tag in `pluginTags`
116 */
117 pluginTags?: string[];
118 /**
119 * Allows setting query#read options at the schema level, providing us a way to apply default ReadPreferences
120 * to all queries derived from a model.
121 */
122 read?: string;
123 /**
124 * Set a default readConcern for all queries at the schema level
125 */
126 readConcern?: { level: 'local' | 'available' | 'majority' | 'snapshot' | 'linearizable' }
127 /** Allows setting write concern at the schema level. */
128 writeConcern?: WriteConcern;
129 /** defaults to true. */
130 safe?: boolean | { w?: number | string; wtimeout?: number; j?: boolean };
131 /**
132 * The shardKey option is used when we have a sharded MongoDB architecture. Each sharded collection is
133 * given a shard key which must be present in all insert/update operations. We just need to set this
134 * schema option to the same shard key and we'll be all set.
135 */
136 shardKey?: Record<string, unknown>;
137 /**
138 * The strict option, (enabled by default), ensures that values passed to our model constructor that were not
139 * specified in our schema do not get saved to the db.
140 */
141 strict?: boolean | 'throw';
142 /**
143 * equal to `strict` by default, may be `false`, `true`, or `'throw'`. Sets the default
144 * [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
145 */
146 strictQuery?: boolean | 'throw';
147 /** Exactly the same as the toObject option but only applies when the document's toJSON method is called. */
148 toJSON?: ToObjectOptions<THydratedDocumentType>;
149 /**
150 * Documents have a toObject method which converts the mongoose document into a plain JavaScript object.
151 * This method accepts a few options. Instead of applying these options on a per-document basis, we may
152 * declare the options at the schema level and have them applied to all of the schema's documents by
153 * default.
154 */
155 toObject?: ToObjectOptions<THydratedDocumentType>;
156 /**
157 * By default, if you have an object with key 'type' in your schema, mongoose will interpret it as a
158 * type declaration. However, for applications like geoJSON, the 'type' property is important. If you want to
159 * control which key mongoose uses to find type declarations, set the 'typeKey' schema option.
160 */
161 typeKey?: string;
162
163 /**
164 * By default, documents are automatically validated before they are saved to the database. This is to
165 * prevent saving an invalid document. If you want to handle validation manually, and be able to save
166 * objects which don't pass validation, you can set validateBeforeSave to false.
167 */
168 validateBeforeSave?: boolean;
169 /**
170 * By default, validation will run on modified and required paths before saving to the database.
171 * You can choose to have Mongoose only validate modified paths by setting validateModifiedOnly to true.
172 */
173 validateModifiedOnly?: boolean;
174 /**
175 * The versionKey is a property set on each document when first created by Mongoose. This keys value
176 * contains the internal revision of the document. The versionKey option is a string that represents
177 * the path to use for versioning. The default is '__v'.
178 *
179 * @default '__v'
180 */
181 versionKey?: string | boolean;
182 /**
183 * By default, Mongoose will automatically select() any populated paths for you, unless you explicitly exclude them.
184 *
185 * @default true
186 */
187 selectPopulatedPaths?: boolean;
188 /**
189 * skipVersioning allows excluding paths from versioning (i.e., the internal revision will not be
190 * incremented even if these paths are updated). DO NOT do this unless you know what you're doing.
191 * For subdocuments, include this on the parent document using the fully qualified path.
192 */
193 skipVersioning?: { [key: string]: boolean; };
194 /**
195 * Validation errors in a single nested schema are reported
196 * both on the child and on the parent schema.
197 * Set storeSubdocValidationError to false on the child schema
198 * to make Mongoose only report the parent error.
199 */
200 storeSubdocValidationError?: boolean;
201 /**
202 * The timestamps option tells mongoose to assign createdAt and updatedAt fields to your schema. The type
203 * assigned is Date. By default, the names of the fields are createdAt and updatedAt. Customize the
204 * field names by setting timestamps.createdAt and timestamps.updatedAt.
205 */
206 timestamps?: boolean | SchemaTimestampsConfig;
207
208 /**
209 * Using `save`, `isNew`, and other Mongoose reserved names as schema path names now triggers a warning, not an error.
210 * You can suppress the warning by setting { suppressReservedKeysWarning: true } schema options. Keep in mind that this
211 * can break plugins that rely on these reserved names.
212 */
213 suppressReservedKeysWarning?: boolean,
214
215 /**
216 * Model Statics methods.
217 */
218 statics?: IfEquals<
219 TStaticMethods,
220 {},
221 { [name: string]: (this: Model<DocType>, ...args: any[]) => unknown },
222 AddThisParameter<TStaticMethods, Model<DocType>>
223 >
224
225 /**
226 * Document instance methods.
227 */
228 methods?: IfEquals<
229 TInstanceMethods,
230 {},
231 Record<any, (this: THydratedDocumentType, ...args: any) => unknown>,
232 AddThisParameter<TInstanceMethods, THydratedDocumentType> & AnyObject
233 >
234
235 /**
236 * Query helper functions.
237 */
238 query?: IfEquals<
239 QueryHelpers,
240 {},
241 Record<any, <T extends QueryWithHelpers<unknown, THydratedDocumentType, QueryHelpers, DocType>>(this: T, ...args: any) => T>,
242 QueryHelpers
243 >
244
245 /**
246 * Set whether to cast non-array values to arrays.
247 * @default true
248 */
249 castNonArrays?: boolean;
250
251 /**
252 * Virtual paths.
253 */
254 virtuals?: SchemaOptionsVirtualsPropertyType<DocType, TVirtuals, TInstanceMethods>,
255
256 /**
257 * Set to `true` to default to overwriting models with the same name when calling `mongoose.model()`, as opposed to throwing an `OverwriteModelError`.
258 * @default false
259 */
260 overwriteModels?: boolean;
261 }
262
263 interface DefaultSchemaOptions {
264 typeKey: 'type';
265 id: true;
266 _id: true;
267 timestamps: false;
268 versionKey: '__v'
269 }
270}