-
I'm interested in using this library for some of my automated tests. My mongo db is setup with authentication enabled - using an authentication database, I don't see any documentation that would allow me to specify the equivalent of --authenticationDatabase . Is it possible? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
Hi @ssnyder, you can authenticate by using username and password parameters or by providing connectionURI. See the readme for particular solution (for example, if you want to use cli - see the readme: https://github.com/pkosiec/mongo-seeding/blob/master/cli/README.md). In JS library you can provide username and password for database object or DB connection uri. Does this answer your question? PS sorry for mistakes, I'm writing this on my mobile because currently I don't have access to my computer. EDIT: |
Beta Was this translation helpful? Give feedback.
-
I'm closing this for now, assuming that all questions have been answered. Feel free to reopen this and comment, if that's not true 🙂 |
Beta Was this translation helpful? Give feedback.
-
I would like to re-open this topic. It is possible to do this with a uri like this: This is necessary when a user is not using the default "admin" name for thier authentication database. For example, in my case I am using "users". I would still prefer to use ENV variables seperately rather than passing the whole URI so at present I cant use it this way. To make it easy when using docker, can I suggest to add a new ENV called DB_MONGO_AUTH_DB. If it's not set, no need to do anything, but if is set, then you can simply add it as ?authSource=$DB_MONGO_AUTH_DB when constructing the uri in code. I would suggest to change it like follows: getDbConnectionUri({
protocol,
host,
port,
name,
username,
password,
authDb,
}: SeederDatabaseConfigObject) {
let credentials = '';
if (username) {
credentials = `${username}${password ? `:${password}` : ''}@`;
}
let options = []
if (authDb) {
options.push(`authSource=${authDb}`)
}
if (protocol === 'mongodb+srv') {
return `${protocol}://${credentials}${host}/${name}?${options.join('&')}`;
}
return `${protocol}://${credentials}${host}:${port}/${name}?${options.join('&')}`;
} Well, I guess in this format we could also pass an options array or string, but just to keep it simple authSource is the only option I need. |
Beta Was this translation helpful? Give feedback.
-
Yes, it is possible by providing your own DB_URI.
Hmm, to be honest I don't want to support all possible options for DB URI with separate environmental variables. So maybe it would be better to have a generic env variable like |
Beta Was this translation helpful? Give feedback.
-
That sounds like a very tidy and flexible solution 👍 |
Beta Was this translation helpful? Give feedback.
-
Great - I created a new issue for adding this feature. It should be quite easy to add it, so it will come soon. |
Beta Was this translation helpful? Give feedback.
The functionality has been added as a part of #78 (PR: #80)