Skip to content

Instantly share code, notes, and snippets.

@jukkatupamaki
Last active June 21, 2023 07:03
Show Gist options
  • Save jukkatupamaki/9893e5f111862d06044b73fa944a8741 to your computer and use it in GitHub Desktop.
Save jukkatupamaki/9893e5f111862d06044b73fa944a8741 to your computer and use it in GitHub Desktop.
How to use Knex.js in a TypeScript project
import { Knex } from 'knex'
export async function up(knex: Knex): Promise<any> {
await knex.schema.createTable('test_setup', (table: Knex.TableBuilder) => {
table.integer('foobar');
});
}
export async function down(knex: Knex): Promise<any> {
await knex.schema.dropTable('test_setup');
}
import { Knex } from "knex";
const config: Knex.Config = {
client: "pg",
connection: {
connectionString: process.env.DATABASE_URL,
timezone: "utc",
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: "knex_migrations",
directory: "migrations",
},
};
export default config;
{
"name": "my-app",
"version": "0.0.1",
"description": "",
"main": "src/server.js",
"private": true,
"scripts": {
"knex:migrate:make": "knex --knexfile src/database/knexfile.ts migrate:make -x ts",
"knex:migrate:latest": "knex --knexfile src/database/knexfile.ts migrate:latest",
"knex:migrate:rollback": "knex --knexfile src/database/knexfile.ts migrate:rollback"
},
"dependencies": {
"knex": "^0.95.6",
"pg": "^8.6.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
}
}

Create migration files

This creates a .ts file in migrations directory

npm run knex:migrate:make -- some-migration-name

Run migrations

npm run knex:migrate:latest

Rollback

npm run knex:migrate:rollback

Knex TypeScript issues

  • knexfile.ts requires require('ts-node/register'); to work.
  • ES6/ES2015 module syntax does not work in knexfile or in any files that it require()s

knex/knex#3003

knex/knex#2998

@jaimecigna
Copy link

For anyone encountering the "unknown option '-x'" issue, i solved this by doing the following in the package.json scripts

"knex": "./node_modules/.bin/knex --knexfile src/my/path/to/database/knexfile.ts"

Now i can use it like so:
npm run knex migrate:make some_migration

Copy link

ghost commented Aug 9, 2020

@acbd-ca try:
import 'dotenv/config'

@HamishWHC
Copy link

Hey @tukkajukka, how would you import the knexfile config into your ts src files to actually use the config? Also, how do you avoid placing DB creds into GitHub with this? Should knexfile fetch from envvars?

@jukkatupamaki
Copy link
Author

jukkatupamaki commented Aug 28, 2020

@HamishWHC environment variables is the way to go with any credentials, e.g. process.env.DATABASE_URL reads database credentials from env vars.

Did you try importing the Knexfile with require() or ES6 import?

@grumd
Copy link

grumd commented May 7, 2021

If you still have the "-x" issue, explanation is simple, there's a mistake in the gist.
Instead of doing "knex -x ts migrate:make migration_name" you need to do "knex migrate:make -x ts migration_name".

My script works like this:

// package.json script:
"knex:migrate:make": "knex --knexfile src/knexfile.ts migrate:make -x ts",

// in terminal:
npm run knex:migrate:make migration_name

@jukkatupamaki
Copy link
Author

@grumd thanks, updated.

@Hoxtygen
Copy link

In the migration file I'm getting this error Cannot use namespace Knex as a type

@jengel3
Copy link

jengel3 commented May 25, 2021

In the migration file I'm getting this error Cannot use namespace Knex as a type

Knex typings changed. Try the following:

import { Knex } from 'knex'

@Hoxtygen
Copy link

In the migration file I'm getting this error Cannot use namespace Knex as a type

Knex typings changed. Try the following:

import { Knex } from 'knex'

Thanks, that works.

@jukkatupamaki
Copy link
Author

@jengel3 @Hoxtygen thanks, updated the gist.

@jfollmann
Copy link

jfollmann commented Aug 11, 2021

Now you can specify to knex generate ts files on knexfile.ts.

const config: Knex.Config = {
  client: 'pg',
  connection: {
    host: 'localhost',
    user: 'postgres'
  },
  migrations: {
    extension: 'ts', //define work typescript to migrations
    directory: 'src/knex/migrations',
    tableName: 'migrations_history',
  },
  seeds: {
    extension: 'ts', //define work typescript to seeds
    directory: 'src/knex/seeds',
  },
};

This makes it easier than always to specifying the -x ts flag.
In case you're interested, I set up this example project: https://github.com/jfollmann/knex-migrations-ts

Thank you for sharing this note.

@piyushgarg-dev
Copy link

[SOLVED]: npm install ts-node
and then try to run knex migrations

@sostenesapollo
Copy link

[SOLVED]: npm install ts-node
and then try to run knex migrations

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