-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give up() and down() functions access to connection #45
Comments
@eweitnauer Can't you access mongoose directly by importing/requiring it? My understanding is See: |
@robodude666 Based on what you sent, I tried to do the following in the migration code async function up() {
const mongoose = require('mongoose');
console.log(mongoose.connection);
} but only get a disconnected connection object: NativeConnection {
base:
Mongoose {
connections: [ [Circular] ],
models: {},
modelSchemas: {},
options: { pluralization: true },
_pluralize: [Function: pluralize],
Schema:
{ [Function: Schema] reserved: [Object], Types: [Object], ObjectId: [Function] },
model: [Function],
plugins: [ [Array], [Array], [Array], [Array] ] },
collections: {},
models: {},
config: { autoIndex: true },
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states:
{ '0': 'disconnected',
'1': 'connected',
'2': 'connecting',
'3': 'disconnecting',
'99': 'uninitialized',
disconnected: 0,
connected: 1,
connecting: 2,
disconnecting: 3,
uninitialized: 99 },
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
plugins: [],
_listening: false } I am not quite sure why, since mongoose should be connected at the time the script is called (see https://github.com/balmasi/migrate-mongoose/blob/master/src/lib.js#L48 and https://github.com/balmasi/migrate-mongoose/blob/master/src/lib.js#L172). |
@eweitnauer Effectively, you should be able to get connected connection object by importing mongoose (as you did) and calling |
@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 |
I'm missing a way to access the DB connection information inside the
up
anddown
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
anddown
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
anddown
functions. Thethis
context only lets me create mongoose models.Ideally, I'd like to be able to do something like the following:
OR
Does this make sense or am I missing something that is already in place?
The text was updated successfully, but these errors were encountered: