'Using console log in Knex migration not logging
My issue: The console.log('tableNobject: ', tableNobject) does not log in knex migration.
The following code is what I have tried:
// solution A
export async function up(knex: Knex) {
const tableAobject = await knex.select('*').from('tableAobject');
console.log('tableAobject: ', tableAobject);
}
// solution B
export async function up(knex: Knex) {
await knex.select('*').from('tableBobject').then((tableBobject) => {
console.log('tableBobject: ', tableBobject);
});
}
However my terminal output is the following:
Migration Starting ...
Migrated
Migration Done.
These logs come from our migration script where we call database.migrate.latest()
My expected terminal output for the code above would be something like this:
Migration Starting ...
tableNobject: [
{
id: 'randomId'
someData: 'someDataString'
...
},
...
]
Migrated
Migration Done.
I know logging tables that you get from knex is possible because when I set up a test script outside the migration flow I can log the table without issues.
I have tried the following addition of settings:
const configA = {
...
debug: true,
}
const configB = {
...
log: {
warn(message) {
console.log(message)
},
error(message) {
console.log(message)
},
deprecate(message) {
console.log(message)
},
debug(message) {
console.log(message)
},
}
}
const configC = {
...
debug: true,
log: {
warn(message) {
console.log(message)
},
error(message) {
console.log(message)
},
deprecate(message) {
console.log(message)
},
debug(message) {
console.log(message)
},
}
};
The different settings above dont give me the logging in the terminal as I want.
The base settings (I dont know if this gives an added value):
const config = {
client: 'postgresql',
connection: {
host: '127.0.0.1',
port: '5432',
database: 'projectName_develop',
user: 'user',
password: 'dev',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
directory: path.join(__dirname, 'migrations'),
},
seeds: {
directory: path.join(__dirname, 'seeds'),
},
asyncStackTraces: true,
};
Solution 1:[1]
So the reason why my console.log didnt log was because the migrations are TypeScript files.
I forgot to run the command: npm run build-ts-backed:watch
So the solution was easier than the issue at hand. Please remember to run your TypeScript compiler when you test in TypeScript.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TessavWalstijn |
