UNPKG

3.35 kBTypeScriptView Raw
1declare module 'mongoose' {
2 import mongodb = require('mongodb');
3
4 /**
5 * Makes the indexes in MongoDB match the indexes defined in every model's
6 * schema. This function will drop any indexes that are not defined in
7 * the model's schema except the `_id` index, and build any indexes that
8 * are in your schema but not in MongoDB.
9 */
10 function syncIndexes(options?: SyncIndexesOptions): Promise<ConnectionSyncIndexesResult>;
11
12 interface IndexManager {
13 /**
14 * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#createIndex)
15 * function.
16 */
17 createIndexes(options?: mongodb.CreateIndexesOptions): Promise<void>;
18
19 /**
20 * Does a dry-run of Model.syncIndexes(), meaning that
21 * the result of this function would be the result of
22 * Model.syncIndexes().
23 */
24 diffIndexes(options?: Record<string, unknown>): Promise<IndexesDiff>
25
26 /**
27 * Sends `createIndex` commands to mongo for each index declared in the schema.
28 * The `createIndex` commands are sent in series.
29 */
30 ensureIndexes(options?: mongodb.CreateIndexesOptions): Promise<void>;
31
32 /**
33 * Lists the indexes currently defined in MongoDB. This may or may not be
34 * the same as the indexes defined in your schema depending on whether you
35 * use the [`autoIndex` option](/docs/guide.html#autoIndex) and if you
36 * build indexes manually.
37 */
38 listIndexes(): Promise<Array<any>>;
39
40 /**
41 * Makes the indexes in MongoDB match the indexes defined in this model's
42 * schema. This function will drop any indexes that are not defined in
43 * the model's schema except the `_id` index, and build any indexes that
44 * are in your schema but not in MongoDB.
45 */
46 syncIndexes(options?: SyncIndexesOptions): Promise<Array<string>>;
47 }
48
49 interface IndexesDiff {
50 /** Indexes that would be created in mongodb. */
51 toCreate: Array<any>
52 /** Indexes that would be dropped in mongodb. */
53 toDrop: Array<any>
54 }
55
56 type IndexDirection = 1 | -1 | '2d' | '2dsphere' | 'geoHaystack' | 'hashed' | 'text' | 'ascending' | 'asc' | 'descending' | 'desc';
57 type IndexDefinition = Record<string, IndexDirection>;
58
59 interface SyncIndexesOptions extends mongodb.CreateIndexesOptions {
60 continueOnError?: boolean
61 }
62 type ConnectionSyncIndexesResult = Record<string, OneCollectionSyncIndexesResult>;
63 type OneCollectionSyncIndexesResult = Array<string> & mongodb.MongoServerError;
64
65 interface IndexOptions extends mongodb.CreateIndexesOptions {
66 /**
67 * `expires` utilizes the `ms` module from [guille](https://github.com/guille/) allowing us to use a friendlier syntax:
68 *
69 * @example
70 * ```js
71 * const schema = new Schema({ prop1: Date });
72 *
73 * // expire in 24 hours
74 * schema.index({ prop1: 1 }, { expires: 60*60*24 })
75 *
76 * // expire in 24 hours
77 * schema.index({ prop1: 1 }, { expires: '24h' })
78 *
79 * // expire in 1.5 hours
80 * schema.index({ prop1: 1 }, { expires: '1.5h' })
81 *
82 * // expire in 7 days
83 * schema.index({ prop1: 1 }, { expires: '7d' })
84 * ```
85 */
86 expires?: number | string;
87 weights?: Record<string, number>;
88 }
89
90 type SearchIndexDescription = mongodb.SearchIndexDescription;
91}