Skip to content
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

CLI: adds support for asynchronous knexfile loading #3748

Merged
merged 3 commits into from Mar 24, 2020

Conversation

flovilmart
Copy link
Contributor

We plan to use knex CLI but our credentials are safely stored in a vault, which requires to be accessed asynchronously.

This PR adds the support for letting users have the knexfile options returned asynchronously.

bin/cli.js Outdated
if (typeof config === 'function') {
config = config();
}
if (config instanceof Promise) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant, you can simply await config() and if it's not a promise, it will just return the value.

@@ -65,6 +65,33 @@ module.exports = {
);
});

it('Run migrations with knexfile returning function passed', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add same tests for seeder execution? Ideally the two should work symmetrically.

@kibertoad
Copy link
Collaborator

One thing that I'm concerned about is that this implements support for async knexfile usage in CLI context, but not for general knex usage, meaning knexfile is no longer universally applicable.

You may want to check out #3497 - it may potentially address your use-case; if it's not, we can proceed with your implementation.

@flovilmart
Copy link
Contributor Author

@kibertoad you're right, #3497 addresses my needs for now.
Would you like to have full async support for the knexfile implemented extending my current implementation, or are you happy with the current implementation?
If so, I can update my PR to extend the support. otherwise, I'll close as I can work around it.

@kibertoad
Copy link
Collaborator

Your extension is pretty non-intrusive; if the rest can be done in similarly non-intrusive way, and you could provide documentation for that, I think it could be merged.

@flovilmart
Copy link
Contributor Author

but not for general knex usage, meaning knexfile is no longer universally applicable.

@kibertoad I am not sure I follow. I can't see in the codebase where it loads knexfile. Can you point it out?

@kibertoad
Copy link
Collaborator

@flovilmart lib/knex.js -> function Knex(config) {

Now I'm thinking whether or not user would expect promise to be supported here; it actually probably is going to be way simpler if they would resolve knexfile from the promise up-front and then passed that to the knex. So you are right, no additional changes are needed.

@flovilmart
Copy link
Contributor Author

flovilmart commented Mar 23, 2020

awesome. Then again, I am not particularly attached to this PR, but it make is easier to deal with async knexfiles, the current implementation supports only for the connection, which is great and just what I need.

@kibertoad
Copy link
Collaborator

@flovilmart Could you also submit PR to https://github.com/knex/documentation to mention that knexfile can return a promise?

@flovilmart
Copy link
Contributor Author

@kibertoad opened this one: knex/documentation#259

@kibertoad
Copy link
Collaborator

There's a minor conflict, could you please fix it?

@kibertoad kibertoad merged commit 41d02ba into knex:master Mar 24, 2020
@kibertoad
Copy link
Collaborator

Thanks!

@flovilmart
Copy link
Contributor Author

Thanks for the quick response @kibertoad. I praised your commitment in my team this morning 🙌

@kibertoad
Copy link
Collaborator

@flovilmart Ahaha, I'm touched! How urgently do you need this feature? I can cherry-pick this change to 0.20 prior to next major release, since it looks non-breaking.

@flovilmart
Copy link
Contributor Author

it's not urgent at all, we're in the process of integrating migrations to our codebases. If you wanna go ahead and cherry pick, that's appreciated nonetheless :)

kibertoad pushed a commit that referenced this pull request Apr 6, 2020
kibertoad pushed a commit that referenced this pull request Apr 12, 2020
@kibertoad
Copy link
Collaborator

Released in 0.20.14.

@flovilmart
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants