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

knexfile.js for migrations #116

Closed
escobar5 opened this issue Sep 5, 2017 · 10 comments
Closed

knexfile.js for migrations #116

escobar5 opened this issue Sep 5, 2017 · 10 comments

Comments

@escobar5
Copy link

escobar5 commented Sep 5, 2017

I'm trying to configure knex migrations in my feathersjs app, I need to create a knexfile with the configurations for knex migrations to work, but I don't want to have my database configuration in two places (feathersjs config and knexfile), does anybody know how handle database configuration for both?

@daffl
Copy link
Member

daffl commented Sep 5, 2017

You can const app = require('./src/app'); in the Knexfile and then get either the Knex client itself via app.get('knexClient') or the database configuration via app.get('<db-name>').

@escobar5
Copy link
Author

escobar5 commented Sep 5, 2017

You're right, that should work. Thanks

@escobar5 escobar5 closed this as completed Sep 5, 2017
@kodayashi
Copy link

This helped me too, but @daffl I'm trying to sort out what the best practice is here. Migrating and seeding is a key feature, so would it make sense for feathers-knex to do this type of setup when installed?

In addition, the knex CLI isn't available when installed via the instructions in the README, is the preferred approach to also install knex globally to leverage the migration/seed apis?

Thanks!

@daffl
Copy link
Member

daffl commented Feb 22, 2018

Feathers adapters try to be as little in the way of the normal usage of the ORM/database as possible. So whatever the Knex documentation recommends will also work here. The main concern of this adapter is to connect a Knex model to the service interface and implement the common querying mechanism but that is pretty much it.

@kodayashi
Copy link

kodayashi commented Feb 22, 2018

Cool, fair enough. Didn't see anything in the knex docs to accommodate the feathers setup but like I said the above recommendation worked great. To get migrations/seeding working i just installed knex globally, ran knex init and modified the knexfile.js file to:

const app = require('./src/app');

module.exports = app.get('postgres');

Then I could use the standard default/production feathers config files and add migration instructions there, e.g.:

  "postgres": {
    "client": "pg",
    "connection": "postgres://user:password@localhost:5432/database",
    "seeds": {
      "directory": "../src/seeds"
    }

Then seeding worked great with knex seed:run default|production

I believe migrations will work as well with this strategy.

Thanks for the reply man!

@daffl
Copy link
Member

daffl commented Feb 22, 2018

If you would like to add that information to the Readme, a pull request would be very welcome :)

@carquiza
Copy link

Thanks for the solution. May I add that it seems to be necessary to also comment out the parts in the models that create tables, as they seem to run before the migrations are called.

@soulprovidr
Copy link

I know this is an old issue, but I echo the sentiment above re: migrations being a fundamental part of an application. This should really be addressed by the tool and in the documentation.

@daffl
Copy link
Member

daffl commented Sep 1, 2019

It's about adding the information above which can be done by editing the readme.

@joezappie
Copy link

joezappie commented May 8, 2021

I'd like to make a suggestion - I'm using typescript for my project and the provided method in the readme did not work well. I kept running into typescript errors when trying to require ./src/app. I installed typescript-require but that didn't really seem to work but I found a simpler way:

const config = require('config');
module.exports = config.get('postgres');

Can we update the readme to this as it supports both normal js and typescript without having to install any additional packages. It also is more efficient as theres in most cases probably no reason to load the entire app just for the knexfile. Correct me if I'm wrong though, this is my first featherjs app.

Edit Looking at the readme again, changing knexfile.js to knexfile.ts would have solved my issue as well (first time TS user as well). Curious though if it would still be better to recommend using the config module instead of requiring the entire app.

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

No branches or pull requests

6 participants