* Creates a new Db instance. * * Db name cannot contain a dot, the server may apply more restrictions when an operation is run. * * @param client - The MongoClient for the database. * @param databaseName - The name of the database this instance represents. * @param options - Option
(client: MongoClient, databaseName: string, options?: DbOptions)
| 149 | * @param options - Optional settings for Db construction. |
| 150 | */ |
| 151 | constructor(client: MongoClient, databaseName: string, options?: DbOptions) { |
| 152 | options = options ?? {}; |
| 153 | |
| 154 | // Filter the options |
| 155 | options = filterOptions(options, DB_OPTIONS_ALLOW_LIST); |
| 156 | |
| 157 | // Ensure there are no dots in database name |
| 158 | if (typeof databaseName === 'string' && databaseName.includes('.')) { |
| 159 | throw new MongoInvalidArgumentError(`Database names cannot contain the character '.'`); |
| 160 | } |
| 161 | |
| 162 | // Internal state of the db object |
| 163 | this.s = { |
| 164 | // Options |
| 165 | options, |
| 166 | // Unpack read preference |
| 167 | readPreference: ReadPreference.fromOptions(options), |
| 168 | // Merge bson options |
| 169 | bsonOptions: resolveBSONOptions(options, client), |
| 170 | // Set up the primary key factory or fallback to ObjectId |
| 171 | pkFactory: options?.pkFactory ?? DEFAULT_PK_FACTORY, |
| 172 | // ReadConcern |
| 173 | readConcern: ReadConcern.fromOptions(options), |
| 174 | writeConcern: WriteConcern.fromOptions(options), |
| 175 | // Namespace |
| 176 | namespace: new MongoDBNamespace(databaseName) |
| 177 | }; |
| 178 | |
| 179 | this.client = client; |
| 180 | } |
| 181 | |
| 182 | get databaseName(): string { |
| 183 | return this.s.namespace.db; |
no test coverage detected