question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Give up() and down() functions access to connection

See original GitHub issue

I’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:open
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
eweitnauercommented, Sep 17, 2019

@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.

0reactions
marceliwaccommented, Sep 19, 2019

@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 ?).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shut down a custom Access application remotely - Office
Describes how to gracefully shut down a front-end Access database ... there is no built in way to force users to quit Microsoft...
Read more >
Passing around Database Connection - PHP - SitePoint Forums
As for passing in the database connection itself the easiest way is to just use the global keyword inside your function like the...
Read more >
How to fix 8 common remote desktop connection problems
First, try to establish a session from a client that has been able to successfully connect in the past. The goal is to...
Read more >
How and Why to Split Your Microsoft Access Database
In this video, we'll talk about what splitting your database means, the benefits and drawbacks, and I'll show you how to do it....
Read more >
Microsoft Access: Is it still relevant in 2022? - Comparitech
For example, Access can attach to an Oracle database, so business applications written in Access can connect through to data held in an ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found