Skip to content

Commit bff162d

Browse files
authored
Merge pull request #15575 from Automattic/vkarpov15/base-connection-improvements
types(connection+collection): make BaseCollection and BaseConnection usable as values
2 parents 6f52dcb + afe905a commit bff162d

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

types/collection.d.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
declare module 'mongoose' {
22
import mongodb = require('mongodb');
33

4-
/*
5-
* section collection.js
6-
*/
7-
interface CollectionBase<T extends mongodb.Document> extends mongodb.Collection<T> {
8-
/*
9-
* Abstract methods. Some of these are already defined on the
10-
* mongodb.Collection interface so they've been commented out.
4+
export class BaseCollection<T extends mongodb.Document> extends mongodb.Collection<T> {
5+
/**
6+
* Collection constructor
7+
* @param name name of the collection
8+
* @param conn A MongooseConnection instance
9+
* @param opts optional collection options
1110
*/
11+
constructor(name: string, conn: Connection, opts?: any);
12+
13+
/*
14+
* Abstract methods. Some of these are already defined on the
15+
* mongodb.Collection interface so they've been commented out.
16+
*/
1217
ensureIndex(...args: any[]): any;
1318
findAndModify(...args: any[]): any;
1419
getIndexes(...args: any[]): any;
1520

21+
/** Formatter for debug print args */
22+
$format(arg: any, color?: boolean, shell?: boolean): string;
23+
/** Debug print helper */
24+
$print(name: string, i: string | number, args: any[], color?: boolean, shell?: boolean): void;
25+
1626
/** The collection name */
17-
collectionName: string;
27+
get collectionName(): string;
1828
/** The Connection instance */
1929
conn: Connection;
2030
/** The collection name */
2131
name: string;
2232
}
2333

24-
export type BaseCollection<T extends mongodb.Document> = CollectionBase<T>;
25-
2634
/*
2735
* section drivers/node-mongodb-native/collection.js
2836
*/
29-
interface Collection<T extends mongodb.Document = mongodb.Document> extends CollectionBase<T> {
37+
class Collection<T extends mongodb.Document = mongodb.Document> extends BaseCollection<T> {
3038
/**
3139
* Collection constructor
3240
* @param name name of the collection
3341
* @param conn A MongooseConnection instance
3442
* @param opts optional collection options
3543
*/
36-
// eslint-disable-next-line @typescript-eslint/no-misused-new
37-
new(name: string, conn: Connection, opts?: any): Collection<T>;
38-
/** Formatter for debug print args */
39-
$format(arg: any, color?: boolean, shell?: boolean): string;
40-
/** Debug print helper */
41-
$print(name: string, i: string | number, args: any[], color?: boolean, shell?: boolean): void;
44+
constructor(name: string, conn: Connection, opts?: any);
45+
4246
/** Retrieves information about this collections indexes. */
4347
getIndexes(): ReturnType<mongodb.Collection<T>['indexInformation']>;
4448
}
45-
let Collection: Collection;
4649
}

types/connection.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ declare module 'mongoose' {
7171
};
7272
}[keyof SchemaMap];
7373

74-
export type BaseConnection = Connection;
75-
7674
class Connection extends events.EventEmitter implements SessionStarter {
7775
/** Runs a [db-level aggregate()](https://www.mongodb.com/docs/manual/reference/method/db.aggregate/) on this connection's underlying `db` */
7876
aggregate<ResultType = unknown>(pipeline?: PipelineStage[] | null, options?: AggregateOptions): Aggregate<Array<ResultType>>;
@@ -286,4 +284,5 @@ declare module 'mongoose' {
286284
withSession<T = any>(executor: (session: ClientSession) => Promise<T>): Promise<T>;
287285
}
288286

287+
export class BaseConnection extends Connection {}
289288
}

0 commit comments

Comments
 (0)