How to Configure Knex.js with TypeScript
Knex.js is a popular SQL query builder for JavaScript and TypeScript applications. In this guide, we will walk through the process of setting up Knex.js with TypeScript.
1. Install Necessary Dependencies
Before we begin, ensure you have Node.js and npm (or Yarn) installed. Next, we'll need to install knex
, the database driver for your chosen database (e.g., pg
for PostgreSQL), and TypeScript dependencies:
npm install knex pg typescript ts-node @types/node
Replace pg
with the driver for your chosen database if it's not PostgreSQL.
2. Initialize Knex and TypeScript
-
Knex Configuration: Initialize a new Knex configuration file:
npx knex init
This will generate a
knexfile.js
in your project root. -
TypeScript Configuration: Initialize a new TypeScript configuration file:
npx tsc --init
This will generate a
tsconfig.json
in your project root.
3. Modify the Knex Configuration to Use TypeScript
Rename knexfile.js
to knexfile.ts
. Now, modify it to use TypeScript import/export syntax and configuration. Below is a basic configuration for a PostgreSQL database:
import { Config } from 'knex'; const config: Config = { client: 'pg', connection: { host: '127.0.0.1', user: 'your_database_user', password: 'your_database_password', database: 'your_database_name' }, migrations: { directory: './migrations' }, seeds: { directory: './seeds' } }; export default config;
Adjust the connection details to match your database setup.
4. Update TypeScript Configuration
Modify your tsconfig.json
to handle the Knex setup. Here are some recommended changes:
{ "compilerOptions": { "target": "ES6", "module": "CommonJS", "strict": true, "esModuleInterop": true, "outDir": "./dist", "skipLibCheck": true, "rootDirs": ["."], }, "include": ["**/*.ts"], "exclude": ["node_modules"] }
5. Create Migrations with TypeScript
To create a migration with TypeScript:
npx knex migrate:make name_of_migration --ext ts
This will create a TypeScript file in the migrations directory. From there, you can use the Knex migration API to define your database changes.
6. Run Migrations
You can use ts-node
to run migrations:
npx ts-node ./node_modules/.bin/knex migrate:latest
7. Create Seeds with TypeScript (Optional)
If you want to seed your database with initial data:
npx knex seed:make name_of_seed --ext ts
This will create a TypeScript seed file where you can define your initial data.
8. Run Seeds (Optional)
Run the seeds using:
npx ts-node ./node_modules/.bin/knex seed:run
Tips
- Always remember to use the
-ext ts
flag when creating migrations or seeds to ensure they are generated as TypeScript files. - If you face any issues with types, ensure you have the latest
@types/knex
package installed. - External tools like Basedash are great for viewing and editing the data in your database once you go live.
Now, you should be all set to use Knex.js with TypeScript in your project!
Invite only
We're building the next generation of data visualization.
How to turn webpages into editable canvases with a JavaScript bookmarklet
Kris Lachance
How to fix the "not all code paths return a value" issue in TypeScript
Kris Lachance
Working with WebSockets in Node.js using TypeScript
Kris Lachance
Type Annotations Can Only Be Used in TypeScript Files
Kris Lachance
Guide to TypeScript Recursive Type
Kris Lachance
"No overload matches this call" in TypeScript
Kris Lachance