UNPKG

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