UNPKG

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