I am setting up a project using Knex, Objection, Node and Typescript and when I tried to run a simple command like knex --knexfile ./src/db/knexfile.ts migrate:list --client pg
, I keep getting:
Error: Unable to acquire a connection
I don't know why this is happening. Here is my knexfile:
export const config: { [key: string]: Knex.Config } = {
development: {
client: "pg",
connection: {
database: "tests",
user: "postgres",
password: "examplepassword"
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: "knex_migrations",
loadExtensions: ['.js', '.ts']
},
acquireConnectionTimeout: 3000,
...knexSnakeCaseMappers,
},
Here is my file used for the connection:
import Knex from 'knex';
import { Model } from 'objection';
import { config } from "./knexfile";
const db = Knex(config.development);
Model.knex(db);
export default db;
Now in my main server file (index.ts), I simply import the db file. My index.ts looks like this:
import express from "express";
import names from "./routes/Names";
import "../src/db/db";
const app = express();
app.use(express.json());
app.use("/api/names", names);
app.listen(8080, () => {
console.log("We are online!");
});
The migration I am trying to list or run is:
import { Knex } from "knex";
import { TABLE_NAMES } from "../../constants/tableNames";
//import "../db"; <--- importing the connection yields the same error!
export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable(TABLE_NAMES.Names, (table) => {
table.increments('id').primary();
table.string('name', 255).notNullable();
table.json('hobbies');
table.string('email', 255).notNullable();
table.timestamp('createdAt');
table.timestamp('updatedAt');
});
}
export async function down(knex: Knex): Promise<void> {
return knex.schema.dropTableIfExists(TABLE_NAMES.Names);
}
I don't know what I am doing wrong?