Skip to content

Commit

Permalink
address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 13, 2024
1 parent 6c6fb8b commit 6c21fcc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
4 changes: 0 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
9 changes: 5 additions & 4 deletions test/connection.test.js
Original file line number Diff line number Diff line change
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,23 +185,24 @@ 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), ['test1176']);
assert.deepStrictEqual(collections.map(coll => coll.name).sort(), ['test1176', 'test94112']);
});
});

Expand Down
4 changes: 4 additions & 0 deletions test/types/connection.test.ts
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion types/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ declare module 'mongoose' {
* Helper for MongoDB Node driver's `listCollections()`.
* Returns an array of collection names.
*/
listCollections(collection: string): Promise<Pick<mongodb.CollectionInfo, 'name' | 'type'>[]>;
listCollections(): Promise<Pick<mongodb.CollectionInfo, 'name' | 'type'>[]>;

/**
* A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing
Expand Down

0 comments on commit 6c21fcc

Please sign in to comment.