@@ -6,7 +6,7 @@ import * as CONSTANTS from './constants';
6
6
import { AggregationCursor } from './cursor/aggregation_cursor' ;
7
7
import { ListCollectionsCursor } from './cursor/list_collections_cursor' ;
8
8
import { RunCommandCursor , type RunCursorCommandOptions } from './cursor/run_command_cursor' ;
9
- import { MongoAPIError , MongoInvalidArgumentError } from './error' ;
9
+ import { MongoInvalidArgumentError } from './error' ;
10
10
import type { MongoClient , PkFactory } from './mongo_client' ;
11
11
import type { TODO_NODE_3286 } from './mongo_types' ;
12
12
import type { AggregateOptions } from './operations/aggregate' ;
@@ -134,20 +134,24 @@ export class Db {
134
134
public static SYSTEM_JS_COLLECTION = CONSTANTS . SYSTEM_JS_COLLECTION ;
135
135
136
136
/**
137
- * Creates a new Db instance
137
+ * Creates a new Db instance.
138
+ *
139
+ * Db name cannot contain a dot, the server may apply more restrictions when an operation is run.
138
140
*
139
141
* @param client - The MongoClient for the database.
140
142
* @param databaseName - The name of the database this instance represents.
141
- * @param options - Optional settings for Db construction
143
+ * @param options - Optional settings for Db construction.
142
144
*/
143
145
constructor ( client : MongoClient , databaseName : string , options ?: DbOptions ) {
144
146
options = options ?? { } ;
145
147
146
148
// Filter the options
147
149
options = filterOptions ( options , DB_OPTIONS_ALLOW_LIST ) ;
148
150
149
- // Ensure we have a valid db name
150
- validateDatabaseName ( databaseName ) ;
151
+ // Ensure there are no dots in database name
152
+ if ( typeof databaseName === 'string' && databaseName . includes ( '.' ) ) {
153
+ throw new MongoInvalidArgumentError ( `Database names cannot contain the character '.'` ) ;
154
+ }
151
155
152
156
// Internal state of the db object
153
157
this . s = {
@@ -218,6 +222,8 @@ export class Db {
218
222
* Create a new collection on a server with the specified options. Use this to create capped collections.
219
223
* More information about command options available at https://www.mongodb.com/docs/manual/reference/command/create/
220
224
*
225
+ * Collection namespace validation is performed server-side.
226
+ *
221
227
* @param name - The name of the collection to create
222
228
* @param options - Optional settings for the command
223
229
*/
@@ -294,6 +300,8 @@ export class Db {
294
300
/**
295
301
* Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.
296
302
*
303
+ * Collection namespace validation is performed server-side.
304
+ *
297
305
* @param name - the collection name we wish to access.
298
306
* @returns return the new Collection instance
299
307
*/
@@ -519,19 +527,3 @@ export class Db {
519
527
return new RunCommandCursor ( this , command , options ) ;
520
528
}
521
529
}
522
-
523
- // TODO(NODE-3484): Refactor into MongoDBNamespace
524
- // Validate the database name
525
- function validateDatabaseName ( databaseName : string ) {
526
- if ( typeof databaseName !== 'string' )
527
- throw new MongoInvalidArgumentError ( 'Database name must be a string' ) ;
528
- if ( databaseName . length === 0 )
529
- throw new MongoInvalidArgumentError ( 'Database name cannot be the empty string' ) ;
530
- if ( databaseName === '$external' ) return ;
531
-
532
- const invalidChars = [ ' ' , '.' , '$' , '/' , '\\' ] ;
533
- for ( let i = 0 ; i < invalidChars . length ; i ++ ) {
534
- if ( databaseName . indexOf ( invalidChars [ i ] ) !== - 1 )
535
- throw new MongoAPIError ( `database names cannot contain the character '${ invalidChars [ i ] } '` ) ;
536
- }
537
- }
0 commit comments