knex.js将布尔字段返回为“0”或“1”,而不是布尔值

omvjsjqw  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(462)

当我使用knex.js查询postgres数据库布尔字段时,它返回的结果为 "0""1" (作为字符串)而不是布尔值 truefalse .
有没有办法让knex/postgres自动将布尔字段作为布尔值返回?
编辑:我正在使用 Knex 具有 node-postgres ,以下是我的表格定义:

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');
  });
ecbunoof

ecbunoof1#

我需要使用pg.types模块:

import { types } from "pg";

types.setTypeParser(16, (value) => { // 16 is the type enum vaue of boolean
    return Boolean(parseInt(value));
});

相关问题