When I query with Knex.js a Postgres database boolean fields it returns the result as "0"
or "1"
(as strings) instead of the boolean values true
and false
.
Is there a way to make Knex/Postgres return boolean fields automatically as boolean values?
EDIT: I’m using Knex
with node-postgres
,
here are my table definitions:
knex.schema .createTable('users_table', (table) => { table.increments('id'); table.string('email').unique().notNullable(); table.string('full_name').notNullable(); table.timestamp('created_at').defaultTo(knex.fn.now()).notNullable(); table.index('email', 'email_unique', 'unique'); }) .createTable('users_credentials', (table) => { table.increments('id'); table.string('password').notNullable(); table.boolean('is_activated').defaultTo(false).notNullable(); table.integer('user_id').unsigned().references('users_table.id').notNullable(); table.index('user_id', 'user_id_unique', 'unique'); });
Advertisement
Answer
I needed to use the pg.types module:
import { types } from "pg"; types.setTypeParser(16, (value) => { // 16 is the type enum vaue of boolean return Boolean(parseInt(value)); });