I'm working on a project which uses knex for database connections. I'm also using flow for typings. I'm attempting to connect to the database as follows:
const db = knex({
client: 'mysql',
connection: {
host: containerized() ? 'database' : process.env.DB_URL || 'localhost',
user: process.env.DB_USER || 'user',
password: process.env.DB_PASSWORD || 'password',
database: process.env.DB_DATABASE || 'database',
},
});
where db has the type Knex$Knex
. I attempt to set up the database with:
export function init(): Promise<Knex$Knex<*>> {
return new Promise((resolve, reject) => {
PromiseReduce([
() => db.schema.hasTable('users').then((exists) => {
if (!exists) {
return db.schema.createTable('users', (table) => {
...
});
}
return null;
}),
...
]).then(resolve).catch(reject);
}).then(() => db);
}
This all works when it's run, but flow is erroring, saying that db.schema
is not a property of Knex$Knex
; and looking at the type declaration, this is the case. Is there a preferred method to do this, or is it simply a case of the flow declarations being wrong?
This is because the flow-typed definition for knex v0.14.x does not include typing for the schema.
The error can be quickly fixed locally by adding schema: Object
to the declaration of the Knex$Knex<R>
class.
A fix for the issue would require typing the schema for knex properly and adding it to the flow-typed repository.