File tree 4 files changed +45
-0
lines changed
4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -647,6 +647,30 @@ Connection.prototype.listCollections = async function listCollections() {
647
647
return await cursor . toArray ( ) ;
648
648
} ;
649
649
650
+ /**
651
+ * Helper for MongoDB Node driver's `listDatabases()`.
652
+ * Returns an object with a `databases` property that contains an
653
+ * array of database objects.
654
+ *
655
+ * #### Example:
656
+ * const { databases } = await mongoose.connection.listDatabases();
657
+ * databases; // [{ name: 'mongoose_test', sizeOnDisk: 0, empty: false }]
658
+ *
659
+ * @method listCollections
660
+ * @return {Promise<{ databases: Array<{ name: string }> }> }
661
+ * @api public
662
+ */
663
+
664
+ Connection . prototype . listDatabases = async function listDatabases ( ) {
665
+ if ( ( this . readyState === STATES . connecting || this . readyState === STATES . disconnected ) && this . _shouldBufferCommands ( ) ) {
666
+ await new Promise ( resolve => {
667
+ this . _queue . push ( { fn : resolve } ) ;
668
+ } ) ;
669
+ }
670
+
671
+ return await this . db . admin ( ) . listDatabases ( ) ;
672
+ } ;
673
+
650
674
/**
651
675
* Helper for `dropDatabase()`. Deletes the given database, including all
652
676
* collections, documents, and indexes.
Original file line number Diff line number Diff line change @@ -1580,6 +1580,17 @@ describe('connections:', function() {
1580
1580
} ) ;
1581
1581
assert . ok ( session ) ;
1582
1582
} ) ;
1583
+ it ( 'listDatabases() should return a list of database objects with a name property (gh-9048)' , async function ( ) {
1584
+ const connection = await mongoose . createConnection ( start . uri ) . asPromise ( ) ;
1585
+ // If this test is running in isolation, then the `start.uri` db might not
1586
+ // exist yet, so create this collection (and the associated db) just in case
1587
+ await connection . createCollection ( 'tests' ) . catch ( ( ) => { } ) ;
1588
+
1589
+ const { databases } = await connection . listDatabases ( ) ;
1590
+ assert . ok ( connection . name ) ;
1591
+ console . log ( databases ) ;
1592
+ assert . ok ( databases . map ( database => database . name ) . includes ( connection . name ) ) ;
1593
+ } ) ;
1583
1594
describe ( 'createCollections()' , function ( ) {
1584
1595
it ( 'should create collections for all models on the connection with the createCollections() function (gh-13300)' , async function ( ) {
1585
1596
const m = new mongoose . Mongoose ( ) ;
Original file line number Diff line number Diff line change @@ -78,6 +78,10 @@ expectType<Promise<string[]>>(
78
78
conn . listCollections ( ) . then ( collections => collections . map ( coll => coll . name ) )
79
79
) ;
80
80
81
+ expectType < Promise < string [ ] > > (
82
+ conn . listDatabases ( ) . then ( dbs => dbs . databases . map ( db => db . name ) )
83
+ ) ;
84
+
81
85
export function autoTypedModelConnection ( ) {
82
86
const AutoTypedSchema = autoTypedSchema ( ) ;
83
87
const AutoTypedModel = connection . model ( 'AutoTypeModelConnection' , AutoTypedSchema ) ;
Original file line number Diff line number Diff line change @@ -132,6 +132,12 @@ declare module 'mongoose' {
132
132
*/
133
133
listCollections ( ) : Promise < Pick < mongodb . CollectionInfo , 'name' | 'type' > [ ] > ;
134
134
135
+ /**
136
+ * Helper for MongoDB Node driver's `listDatabases()`.
137
+ * Returns an array of database names.
138
+ */
139
+ listDatabases ( ) : Promise < mongodb . ListDatabasesResult > ;
140
+
135
141
/**
136
142
* A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing
137
143
* a map from model names to models. Contains all models that have been
You can’t perform that action at this time.
0 commit comments