1 | declare module 'mongoose' {
|
2 | import mongodb = require('mongodb');
|
3 | import events = require('events');
|
4 |
|
5 | /** The Mongoose module's default connection. Equivalent to `mongoose.connections[0]`, see [`connections`](#mongoose_Mongoose-connections). */
|
6 | const connection: Connection;
|
7 |
|
8 | /** An array containing all connections associated with this Mongoose instance. */
|
9 | const connections: Connection[];
|
10 |
|
11 | /** Opens Mongoose's default connection to MongoDB, see [connections docs](https://mongoosejs.com/docs/connections.html) */
|
12 | function connect(uri: string, options?: ConnectOptions): Promise<Mongoose>;
|
13 |
|
14 | /** Creates a Connection instance. */
|
15 | function createConnection(uri: string, options?: ConnectOptions): Connection;
|
16 | function createConnection(): Connection;
|
17 |
|
18 | function disconnect(): Promise<void>;
|
19 |
|
20 | /**
|
21 | * Connection ready state
|
22 | *
|
23 | * - 0 = disconnected
|
24 | * - 1 = connected
|
25 | * - 2 = connecting
|
26 | * - 3 = disconnecting
|
27 | * - 99 = uninitialized
|
28 | */
|
29 | enum ConnectionStates {
|
30 | disconnected = 0,
|
31 | connected = 1,
|
32 | connecting = 2,
|
33 | disconnecting = 3,
|
34 | uninitialized = 99,
|
35 | }
|
36 |
|
37 | /** Expose connection states for user-land */
|
38 | const STATES: typeof ConnectionStates;
|
39 |
|
40 | interface ConnectOptions extends mongodb.MongoClientOptions {
|
41 | /** Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection. */
|
42 | bufferCommands?: boolean;
|
43 | /** The name of the database you want to use. If not provided, Mongoose uses the database name from connection string. */
|
44 | dbName?: string;
|
45 | /** username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility. */
|
46 | user?: string;
|
47 | /** password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility. */
|
48 | pass?: string;
|
49 | /** Set to false to disable automatic index creation for all models associated with this connection. */
|
50 | autoIndex?: boolean;
|
51 | /** Set to `false` to disable Mongoose automatically calling `createCollection()` on every model created on this connection. */
|
52 | autoCreate?: boolean;
|
53 | }
|
54 |
|
55 | class Connection extends events.EventEmitter implements SessionStarter {
|
56 | /** Returns a promise that resolves when this connection successfully connects to MongoDB */
|
57 | asPromise(): Promise<this>;
|
58 |
|
59 | /** Closes the connection */
|
60 | close(force?: boolean): Promise<void>;
|
61 |
|
62 | /** Closes and destroys the connection. Connection once destroyed cannot be reopened */
|
63 | destroy(force?: boolean): Promise<void>;
|
64 |
|
65 | /** Retrieves a collection, creating it if not cached. */
|
66 | collection<T extends AnyObject = AnyObject>(name: string, options?: mongodb.CreateCollectionOptions): Collection<T>;
|
67 |
|
68 | /** A hash of the collections associated with this connection */
|
69 | readonly collections: { [index: string]: Collection };
|
70 |
|
71 | /** A hash of the global options that are associated with this connection */
|
72 | readonly config: any;
|
73 |
|
74 | /** The mongodb.Db instance, set when the connection is opened */
|
75 | readonly db: mongodb.Db | undefined;
|
76 |
|
77 | /**
|
78 | * Helper for `createCollection()`. Will explicitly create the given collection
|
79 | * with specified options. Used to create [capped collections](https://www.mongodb.com/docs/manual/core/capped-collections/)
|
80 | * and [views](https://www.mongodb.com/docs/manual/core/views/) from mongoose.
|
81 | */
|
82 | createCollection<T extends AnyObject = AnyObject>(name: string, options?: mongodb.CreateCollectionOptions): Promise<mongodb.Collection<T>>;
|
83 |
|
84 | /**
|
85 | * https://mongoosejs.com/docs/api/connection.html#Connection.prototype.createCollections()
|
86 | */
|
87 | createCollections(continueOnError?: boolean): Promise<Record<string, Error | mongodb.Collection<any>>>;
|
88 |
|
89 | /**
|
90 | * Removes the model named `name` from this connection, if it exists. You can
|
91 | * use this function to clean up any models you created in your tests to
|
92 | * prevent OverwriteModelErrors.
|
93 | */
|
94 | deleteModel(name: string | RegExp): this;
|
95 |
|
96 | /**
|
97 | * Helper for `dropCollection()`. Will delete the given collection, including
|
98 | * all documents and indexes.
|
99 | */
|
100 | dropCollection(collection: string): Promise<void>;
|
101 |
|
102 | /**
|
103 | * Helper for `dropDatabase()`. Deletes the given database, including all
|
104 | * collections, documents, and indexes.
|
105 | */
|
106 | dropDatabase(): Promise<void>;
|
107 |
|
108 | /** Gets the value of the option `key`. */
|
109 | get(key: string): any;
|
110 |
|
111 | /**
|
112 | * Returns the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
|
113 | * that this connection uses to talk to MongoDB.
|
114 | */
|
115 | getClient(): mongodb.MongoClient;
|
116 |
|
117 | /**
|
118 | * The host name portion of the URI. If multiple hosts, such as a replica set,
|
119 | * this will contain the first host name in the URI
|
120 | */
|
121 | readonly host: string;
|
122 |
|
123 | /**
|
124 | * A number identifier for this connection. Used for debugging when
|
125 | * you have [multiple connections](/docs/connections.html#multiple_connections).
|
126 | */
|
127 | readonly id: number;
|
128 |
|
129 | /**
|
130 | * Helper for MongoDB Node driver's `listCollections()`.
|
131 | * Returns an array of collection names.
|
132 | */
|
133 | listCollections(): Promise<Pick<mongodb.CollectionInfo, 'name' | 'type'>[]>;
|
134 |
|
135 | /**
|
136 | * Helper for MongoDB Node driver's `listDatabases()`.
|
137 | * Returns an array of database names.
|
138 | */
|
139 | listDatabases(): Promise<mongodb.ListDatabasesResult>;
|
140 |
|
141 | /**
|
142 | * A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing
|
143 | * a map from model names to models. Contains all models that have been
|
144 | * added to this connection using [`Connection#model()`](/docs/api/connection.html#connection_Connection-model).
|
145 | */
|
146 | readonly models: Readonly<{ [index: string]: Model<any> }>;
|
147 |
|
148 | /** Defines or retrieves a model. */
|
149 | model<TSchema extends Schema = any>(
|
150 | name: string,
|
151 | schema?: TSchema,
|
152 | collection?: string,
|
153 | options?: CompileModelOptions
|
154 | ): Model<
|
155 | InferSchemaType<TSchema>,
|
156 | ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
157 | ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
158 | {},
|
159 | HydratedDocument<
|
160 | InferSchemaType<TSchema>,
|
161 | ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
|
162 | ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>
|
163 | >,
|
164 | TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
|
165 | model<T, U, TQueryHelpers = {}>(
|
166 | name: string,
|
167 | schema?: Schema<T, any, any, TQueryHelpers, any, any, any>,
|
168 | collection?: string,
|
169 | options?: CompileModelOptions
|
170 | ): U;
|
171 | model<T>(name: string, schema?: Schema<T, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
|
172 |
|
173 | /** Returns an array of model names created on this connection. */
|
174 | modelNames(): Array<string>;
|
175 |
|
176 | /** The name of the database this connection points to. */
|
177 | readonly name: string;
|
178 |
|
179 | /** Opens the connection with a URI using `MongoClient.connect()`. */
|
180 | openUri(uri: string, options?: ConnectOptions): Promise<Connection>;
|
181 |
|
182 | /** The password specified in the URI */
|
183 | readonly pass: string;
|
184 |
|
185 | /**
|
186 | * The port portion of the URI. If multiple hosts, such as a replica set,
|
187 | * this will contain the port from the first host name in the URI.
|
188 | */
|
189 | readonly port: number;
|
190 |
|
191 | /** Declares a plugin executed on all schemas you pass to `conn.model()` */
|
192 | plugin<S extends Schema = Schema, O = AnyObject>(fn: (schema: S, opts?: any) => void, opts?: O): Connection;
|
193 |
|
194 | /** The plugins that will be applied to all models created on this connection. */
|
195 | plugins: Array<any>;
|
196 |
|
197 | /**
|
198 | * Connection ready state
|
199 | *
|
200 | * - 0 = disconnected
|
201 | * - 1 = connected
|
202 | * - 2 = connecting
|
203 | * - 3 = disconnecting
|
204 | * - 99 = uninitialized
|
205 | */
|
206 | readonly readyState: ConnectionStates;
|
207 |
|
208 | /** Sets the value of the option `key`. */
|
209 | set(key: string, value: any): any;
|
210 |
|
211 | /**
|
212 | * Set the [MongoDB driver `MongoClient`](https://mongodb.github.io/node-mongodb-native/4.9/classes/MongoClient.html) instance
|
213 | * that this connection uses to talk to MongoDB. This is useful if you already have a MongoClient instance, and want to
|
214 | * reuse it.
|
215 | */
|
216 | setClient(client: mongodb.MongoClient): this;
|
217 |
|
218 | /**
|
219 | * _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions)
|
220 | * for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),
|
221 | * and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
222 | */
|
223 | startSession(options?: ClientSessionOptions): Promise<ClientSession>;
|
224 |
|
225 | /**
|
226 | * Makes the indexes in MongoDB match the indexes defined in every model's
|
227 | * schema. This function will drop any indexes that are not defined in
|
228 | * the model's schema except the `_id` index, and build any indexes that
|
229 | * are in your schema but not in MongoDB.
|
230 | */
|
231 | syncIndexes(options?: SyncIndexesOptions): Promise<ConnectionSyncIndexesResult>;
|
232 |
|
233 | /**
|
234 | * _Requires MongoDB >= 3.6.0._ Executes the wrapped async function
|
235 | * in a transaction. Mongoose will commit the transaction if the
|
236 | * async function executes successfully and attempt to retry if
|
237 | * there was a retryable error.
|
238 | */
|
239 | transaction<ReturnType = unknown>(fn: (session: mongodb.ClientSession) => Promise<ReturnType>, options?: mongodb.TransactionOptions): Promise<ReturnType>;
|
240 |
|
241 | /** Switches to a different database using the same connection pool. */
|
242 | useDb(name: string, options?: { useCache?: boolean, noListener?: boolean }): Connection;
|
243 |
|
244 | /** The username specified in the URI */
|
245 | readonly user: string;
|
246 |
|
247 | /** Watches the entire underlying database for changes. Similar to [`Model.watch()`](/docs/api/model.html#model_Model-watch). */
|
248 | watch<ResultType extends mongodb.Document = any>(pipeline?: Array<any>, options?: mongodb.ChangeStreamOptions): mongodb.ChangeStream<ResultType>;
|
249 |
|
250 | withSession<T = any>(executor: (session: ClientSession) => Promise<T>): Promise<T>;
|
251 | }
|
252 |
|
253 | }
|
254 |
|
\ | No newline at end of file |