Give up() and down() functions access to connection
See original GitHub issueI’m missing a way to access the DB connection information inside the up and down functions in migrations.
When using the CLI tool there are several ways to pass connection information (db uri): environment variables, the config file, and command line arguments. This is great, and migrate-mongoose uses this information to connect to the database, update the migration collection, and to give  the up and down functions of migrations access to mongoose models.
In some of the migrations, I’d like to get more direct access to the mongo db to rename collections. My problem is that I can’t figure out how to get the connection information that I provided to migrate-mongoose inside the up and down functions. The this context only lets me create mongoose models.
Ideally, I’d like to be able to do something like the following:
async function up() {
  const uri = this.config.dbConnectionUri // attach config object to mongoose connection.model
  const mongoClient = new MongoClient(uri, { useNewUrlParser: true });
  ... 
}
OR
async function up() {
  const mongoClient = this.client // pass mongoose connection instead of connection.model
  ... 
}
Does this make sense or am I missing something that is already in place?
Issue Analytics
- State:
 - Created 4 years ago
 - Comments:5
 

Top Related StackOverflow Question
@marceliwac That is what I ended up doing, too!
My dissatisfaction is with the fact that now I need to tell both sequelize and my own connection handler the connection information. It would be much nicer if there was a way to either get the connection or connection information from sequelize inside the up() and down() methods. This would avoid duplication and potential inconsistencies.
The required change in the sequelize migration code would be minimal. The best way to preserve backwards compatibility that I can think of is to pass the connection object as a second parameter to the up() and down() method. (The first parameter is an error callback.) Does that sound reasonable? If so, I can open a PR.
@eweitnauer That does sound like a solution. Either way, currently it seems like the behaviour concerning the mongoose connection seems rather poorly documented so it would be great to continue this dialog (perhaps with someone from the group of more regular maintainers - @balmasi ?).