Skip to content

Commit

Permalink
Merge pull request #14257 from Automattic/vkarpov15/listcollections
Browse files Browse the repository at this point in the history
feat(connection): add listCollections() helper to connections
  • Loading branch information
vkarpov15 committed Jan 14, 2024
2 parents abdac8d + 6c21fcc commit dad0da9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
20 changes: 20 additions & 0 deletions lib/connection.js
Expand Up @@ -605,6 +605,26 @@ Connection.prototype.dropCollection = async function dropCollection(collection)
return this.db.dropCollection(collection);
};

/**
* Helper for MongoDB Node driver's `listCollections()`.
* Returns an array of collection objects.
*
* @method listCollections
* @return {Promise<Collection[]>}
* @api public
*/

Connection.prototype.listCollections = async function listCollections() {
if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {
await new Promise(resolve => {
this._queue.push({ fn: resolve });
});
}

const cursor = this.db.listCollections();
return await cursor.toArray();
};

/**
* Helper for `dropDatabase()`. Deletes the given database, including all
* collections, documents, and indexes.
Expand Down
15 changes: 12 additions & 3 deletions test/connection.test.js
Expand Up @@ -94,7 +94,7 @@ describe('connections:', function() {
}));
await Model.init();

const res = await conn.db.listCollections().toArray();
const res = await conn.listCollections();
assert.ok(!res.map(c => c.name).includes('gh8814_Conn'));
await conn.close();
});
Expand Down Expand Up @@ -185,16 +185,25 @@ describe('connections:', function() {
size: 1024
});

const collections = await conn.db.listCollections().toArray();
const collections = await conn.listCollections();

const names = collections.map(function(c) { return c.name; });
assert.ok(names.indexOf('gh5712') !== -1);
assert.ok(collections[names.indexOf('gh5712')].options.capped);
await conn.createCollection('gh5712_0');
const collectionsAfterCreation = await conn.db.listCollections().toArray();
const collectionsAfterCreation = await conn.listCollections();
const newCollectionsNames = collectionsAfterCreation.map(function(c) { return c.name; });
assert.ok(newCollectionsNames.indexOf('gh5712') !== -1);
});

it('listCollections()', async function() {
await conn.dropDatabase();
await conn.createCollection('test1176');
await conn.createCollection('test94112');

const collections = await conn.listCollections();
assert.deepStrictEqual(collections.map(coll => coll.name).sort(), ['test1176', 'test94112']);
});
});

it('should allow closing a closed connection', async function() {
Expand Down
4 changes: 4 additions & 0 deletions test/types/connection.test.ts
Expand Up @@ -70,6 +70,10 @@ expectType<Connection>(conn.useDb('test', {}));
expectType<Connection>(conn.useDb('test', { noListener: true }));
expectType<Connection>(conn.useDb('test', { useCache: true }));

expectType<Promise<string[]>>(
conn.listCollections().then(collections => collections.map(coll => coll.name))
);

export function autoTypedModelConnection() {
const AutoTypedSchema = autoTypedSchema();
const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema);
Expand Down
6 changes: 6 additions & 0 deletions types/connection.d.ts
Expand Up @@ -121,6 +121,12 @@ declare module 'mongoose' {
*/
readonly id: number;

/**
* Helper for MongoDB Node driver's `listCollections()`.
* Returns an array of collection names.
*/
listCollections(): Promise<Pick<mongodb.CollectionInfo, 'name' | 'type'>[]>;

/**
* A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing
* a map from model names to models. Contains all models that have been
Expand Down

0 comments on commit dad0da9

Please sign in to comment.