@@ -6,7 +6,7 @@ import * as CONSTANTS from './constants';
66import { AggregationCursor } from './cursor/aggregation_cursor' ;
77import { ListCollectionsCursor } from './cursor/list_collections_cursor' ;
88import { RunCommandCursor , type RunCursorCommandOptions } from './cursor/run_command_cursor' ;
9- import { MongoAPIError , MongoInvalidArgumentError } from './error' ;
9+ import { MongoInvalidArgumentError } from './error' ;
1010import type { MongoClient , PkFactory } from './mongo_client' ;
1111import type { TODO_NODE_3286 } from './mongo_types' ;
1212import type { AggregateOptions } from './operations/aggregate' ;
@@ -134,20 +134,24 @@ export class Db {
134134 public static SYSTEM_JS_COLLECTION = CONSTANTS . SYSTEM_JS_COLLECTION ;
135135
136136 /**
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.
138140 *
139141 * @param client - The MongoClient for the database.
140142 * @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.
142144 */
143145 constructor ( client : MongoClient , databaseName : string , options ?: DbOptions ) {
144146 options = options ?? { } ;
145147
146148 // Filter the options
147149 options = filterOptions ( options , DB_OPTIONS_ALLOW_LIST ) ;
148150
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+ }
151155
152156 // Internal state of the db object
153157 this . s = {
@@ -218,6 +222,8 @@ export class Db {
218222 * Create a new collection on a server with the specified options. Use this to create capped collections.
219223 * More information about command options available at https://www.mongodb.com/docs/manual/reference/command/create/
220224 *
225+ * Collection namespace validation is performed server-side.
226+ *
221227 * @param name - The name of the collection to create
222228 * @param options - Optional settings for the command
223229 */
@@ -294,6 +300,8 @@ export class Db {
294300 /**
295301 * Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.
296302 *
303+ * Collection namespace validation is performed server-side.
304+ *
297305 * @param name - the collection name we wish to access.
298306 * @returns return the new Collection instance
299307 */
@@ -519,19 +527,3 @@ export class Db {
519527 return new RunCommandCursor ( this , command , options ) ;
520528 }
521529}
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