Skip to content

Commit dad0da9

Browse files
authored
Merge pull request #14257 from Automattic/vkarpov15/listcollections
feat(connection): add listCollections() helper to connections
2 parents abdac8d + 6c21fcc commit dad0da9

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

lib/connection.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,26 @@ Connection.prototype.dropCollection = async function dropCollection(collection)
605605
return this.db.dropCollection(collection);
606606
};
607607

608+
/**
609+
* Helper for MongoDB Node driver's `listCollections()`.
610+
* Returns an array of collection objects.
611+
*
612+
* @method listCollections
613+
* @return {Promise<Collection[]>}
614+
* @api public
615+
*/
616+
617+
Connection.prototype.listCollections = async function listCollections() {
618+
if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {
619+
await new Promise(resolve => {
620+
this._queue.push({ fn: resolve });
621+
});
622+
}
623+
624+
const cursor = this.db.listCollections();
625+
return await cursor.toArray();
626+
};
627+
608628
/**
609629
* Helper for `dropDatabase()`. Deletes the given database, including all
610630
* collections, documents, and indexes.

test/connection.test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('connections:', function() {
9494
}));
9595
await Model.init();
9696

97-
const res = await conn.db.listCollections().toArray();
97+
const res = await conn.listCollections();
9898
assert.ok(!res.map(c => c.name).includes('gh8814_Conn'));
9999
await conn.close();
100100
});
@@ -185,16 +185,25 @@ describe('connections:', function() {
185185
size: 1024
186186
});
187187

188-
const collections = await conn.db.listCollections().toArray();
188+
const collections = await conn.listCollections();
189189

190190
const names = collections.map(function(c) { return c.name; });
191191
assert.ok(names.indexOf('gh5712') !== -1);
192192
assert.ok(collections[names.indexOf('gh5712')].options.capped);
193193
await conn.createCollection('gh5712_0');
194-
const collectionsAfterCreation = await conn.db.listCollections().toArray();
194+
const collectionsAfterCreation = await conn.listCollections();
195195
const newCollectionsNames = collectionsAfterCreation.map(function(c) { return c.name; });
196196
assert.ok(newCollectionsNames.indexOf('gh5712') !== -1);
197197
});
198+
199+
it('listCollections()', async function() {
200+
await conn.dropDatabase();
201+
await conn.createCollection('test1176');
202+
await conn.createCollection('test94112');
203+
204+
const collections = await conn.listCollections();
205+
assert.deepStrictEqual(collections.map(coll => coll.name).sort(), ['test1176', 'test94112']);
206+
});
198207
});
199208

200209
it('should allow closing a closed connection', async function() {

test/types/connection.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ expectType<Connection>(conn.useDb('test', {}));
7070
expectType<Connection>(conn.useDb('test', { noListener: true }));
7171
expectType<Connection>(conn.useDb('test', { useCache: true }));
7272

73+
expectType<Promise<string[]>>(
74+
conn.listCollections().then(collections => collections.map(coll => coll.name))
75+
);
76+
7377
export function autoTypedModelConnection() {
7478
const AutoTypedSchema = autoTypedSchema();
7579
const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema);

types/connection.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ declare module 'mongoose' {
121121
*/
122122
readonly id: number;
123123

124+
/**
125+
* Helper for MongoDB Node driver's `listCollections()`.
126+
* Returns an array of collection names.
127+
*/
128+
listCollections(): Promise<Pick<mongodb.CollectionInfo, 'name' | 'type'>[]>;
129+
124130
/**
125131
* A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing
126132
* a map from model names to models. Contains all models that have been

0 commit comments

Comments
 (0)