From 6c6fb8b087924bcd60d4f4fc1d0f6b96c6b63d09 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 13 Jan 2024 07:51:18 -0500 Subject: [PATCH 1/2] feat(connection): add listCollections() helper to connections --- lib/connection.js | 24 ++++++++++++++++++++++++ test/connection.test.js | 8 ++++++++ types/connection.d.ts | 6 ++++++ 3 files changed, 38 insertions(+) diff --git a/lib/connection.js b/lib/connection.js index 675c84e813c..10d78ba9b15 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -605,6 +605,30 @@ 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} + * @api public + */ + +Connection.prototype.listCollections = async function listCollections() { + if (arguments.length >= 2 && typeof arguments[1] === 'function') { + throw new MongooseError('Connection.prototype.dropCollection() no longer accepts a callback'); + } + + 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. diff --git a/test/connection.test.js b/test/connection.test.js index f84aaa9dd82..fbdc41ada9a 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -195,6 +195,14 @@ describe('connections:', function() { 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'); + + const collections = await conn.listCollections(); + assert.deepStrictEqual(collections.map(coll => coll.name), ['test1176']); + }); }); it('should allow closing a closed connection', async function() { diff --git a/types/connection.d.ts b/types/connection.d.ts index c34260fde34..5048de16311 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -121,6 +121,12 @@ declare module 'mongoose' { */ readonly id: number; + /** + * Helper for MongoDB Node driver's `listCollections()`. + * Returns an array of collection names. + */ + listCollections(collection: string): Promise[]>; + /** * A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing * a map from model names to models. Contains all models that have been From 6c21fcc00fb46a09a4f3f1dd07f845aa3eb2b4ec Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 13 Jan 2024 14:56:50 -0500 Subject: [PATCH 2/2] address code review comments --- lib/connection.js | 4 ---- test/connection.test.js | 9 +++++---- test/types/connection.test.ts | 4 ++++ types/connection.d.ts | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 10d78ba9b15..de1903b64bc 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -615,10 +615,6 @@ Connection.prototype.dropCollection = async function dropCollection(collection) */ Connection.prototype.listCollections = async function listCollections() { - if (arguments.length >= 2 && typeof arguments[1] === 'function') { - throw new MongooseError('Connection.prototype.dropCollection() no longer accepts a callback'); - } - if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) { await new Promise(resolve => { this._queue.push({ fn: resolve }); diff --git a/test/connection.test.js b/test/connection.test.js index fbdc41ada9a..4b5ac0493c6 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -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(); }); @@ -185,13 +185,13 @@ 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); }); @@ -199,9 +199,10 @@ describe('connections:', function() { 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), ['test1176']); + assert.deepStrictEqual(collections.map(coll => coll.name).sort(), ['test1176', 'test94112']); }); }); diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index 52dd27e8dc5..01e42bec123 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -70,6 +70,10 @@ expectType(conn.useDb('test', {})); expectType(conn.useDb('test', { noListener: true })); expectType(conn.useDb('test', { useCache: true })); +expectType>( + conn.listCollections().then(collections => collections.map(coll => coll.name)) +); + export function autoTypedModelConnection() { const AutoTypedSchema = autoTypedSchema(); const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema); diff --git a/types/connection.d.ts b/types/connection.d.ts index 5048de16311..7e5595cd1f1 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -125,7 +125,7 @@ declare module 'mongoose' { * Helper for MongoDB Node driver's `listCollections()`. * Returns an array of collection names. */ - listCollections(collection: string): Promise[]>; + listCollections(): Promise[]>; /** * A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing