Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connection): add listCollections() helper to connections #14257

Merged
merged 2 commits into from Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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