Skip to content

Instantly share code, notes, and snippets.

@jukkatupamaki
Last active November 9, 2024 17:31
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"
}
}

ts-node

Make sure you have ts-node installed: npm install ts-node

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

@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

@Gredys
Copy link

Gredys commented Oct 9, 2024

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

Finally the ONLY working solution that I found :/
Wasted like ~2+ hours to find this, thank you

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