diff --git a/packages/generators/src/connection/templates/mongodb.tpl.ts b/packages/generators/src/connection/templates/mongodb.tpl.ts index 64c3c38d7f..52815df3f7 100644 --- a/packages/generators/src/connection/templates/mongodb.tpl.ts +++ b/packages/generators/src/connection/templates/mongodb.tpl.ts @@ -17,10 +17,14 @@ declare module './declarations' { export const ${database} = (app: Application) => { const connection = app.get('${database}') as string - const database = new URL(connection).pathname.substring(1) - const mongoClient = MongoClient.connect(connection) - .then(client => client.db(database)) + + const databaseRegex = /\/([a-zA-Z0-9_\\-]+)(?:\?|$)/; + const databaseName = databaseRegex.exec(connection)?.[1]; + + const mongoClient = MongoClient.connect(connection) + .then(client => client.db(databaseName)); + app.set('${database}Client', mongoClient) } `