H
Hono4mo ago
sanner

Knex + Hono + Bun Connection Errors

I was using knex on bun+hono and was attempting to make a connection via the following syntax
import { Knex, knex } from 'knex';
import pg from 'pg';

const client = knex({
client: 'pg', // Replace 'pg' with your database client if different
connection: {
host: "hostDB",
port: 5432,
user: "postgres",
password: "passwordDB",
database: "databaseName",
ssl: {
rejectUnauthorized: false,
},
},
searchPath: ['knex', 'public'], // Optional: Specify search path for PostgreSQL
});
import { Knex, knex } from 'knex';
import pg from 'pg';

const client = knex({
client: 'pg', // Replace 'pg' with your database client if different
connection: {
host: "hostDB",
port: 5432,
user: "postgres",
password: "passwordDB",
database: "databaseName",
ssl: {
rejectUnauthorized: false,
},
},
searchPath: ['knex', 'public'], // Optional: Specify search path for PostgreSQL
});
and i was getting the following error
TypeError: this.driver.Client is not a constructor
at Client_PG._acquireOnlyConnection (file:///home/sanner/Coding/node_modules/knex/lib/dialects/postgres/index.js:83:24)
at Client_PG.acquireRawConnection (file:///home/sanner/Coding//node_modules/knex/lib/dialects/postgres/index.js:101:17)
at create (file:///home/
/node_modules/knex/lib/client.js:262:39)
TypeError: this.driver.Client is not a constructor
at Client_PG._acquireOnlyConnection (file:///home/sanner/Coding/node_modules/knex/lib/dialects/postgres/index.js:83:24)
at Client_PG.acquireRawConnection (file:///home/sanner/Coding//node_modules/knex/lib/dialects/postgres/index.js:101:17)
at create (file:///home/
/node_modules/knex/lib/client.js:262:39)
I was trying to connect to Knex with a postgres client however I am getting this error, I tried many things such as creating a client and adding that to client. This code was able to work on a pnpm worker with nodejs, however it does not work with bun+hono
1 Reply
proudyyyy
proudyyyy3mo ago
What if you try writing it uppercase? Something like this simple with just a local file is working for me.
import Knex from "knex";

export const db = Knex({
client: 'sqlite3',
connection: {
filename: "./db.sqlite"
},
useNullAsDefault: true
})

db.schema.hasTable('users').then(exists => {
if (!exists) {
return db.schema.createTable('users', table => {
table.increments('id').primary();
table.string('username').notNullable();
table.string('email').unique().notNullable();
table.string('password').notNullable();
table.datetime('created_at').notNullable().defaultTo(db.fn.now());
table.datetime('updated_at').notNullable().defaultTo(db.fn.now());
table.text('appearance').notNullable().defaultTo("default");
table.string('icon');
table.text('biography');
});
}
});
import Knex from "knex";

export const db = Knex({
client: 'sqlite3',
connection: {
filename: "./db.sqlite"
},
useNullAsDefault: true
})

db.schema.hasTable('users').then(exists => {
if (!exists) {
return db.schema.createTable('users', table => {
table.increments('id').primary();
table.string('username').notNullable();
table.string('email').unique().notNullable();
table.string('password').notNullable();
table.datetime('created_at').notNullable().defaultTo(db.fn.now());
table.datetime('updated_at').notNullable().defaultTo(db.fn.now());
table.text('appearance').notNullable().defaultTo("default");
table.string('icon');
table.text('biography');
});
}
});
@sanner

Did you find this page helpful?