Skip to content

Commit

Permalink
fix(generators): Harden mongodb.js to reliably extract database from …
Browse files Browse the repository at this point in the history
…any connection string (#3264)
  • Loading branch information
jermsam authored Jan 5, 2024
1 parent 210f103 commit 7b0f82c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/generators/src/connection/templates/mongodb.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
`
Expand Down

0 comments on commit 7b0f82c

Please sign in to comment.