Knex.js Postgres returns 布尔字段为“0”或“1”而不是布尔值
Knex.js with Postgres returns boolean fields as "0" or "1" instead of booleans
当我使用 Knex.js 查询 Postgres 数据库布尔字段时,它 return 的结果是 "0"
或 "1"
(作为字符串)而不是布尔值 true
和 false
.
有没有办法让 Knex/Postgres return 布尔字段自动成为布尔值?
编辑: 我正在使用 Knex
和 node-postgres
,
这是我的 table 定义:
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');
});
我需要使用 pg.types 模块:
import { types } from "pg";
types.setTypeParser(16, (value) => { // 16 is the type enum vaue of boolean
return Boolean(parseInt(value));
});
当我使用 Knex.js 查询 Postgres 数据库布尔字段时,它 return 的结果是 "0"
或 "1"
(作为字符串)而不是布尔值 true
和 false
.
有没有办法让 Knex/Postgres return 布尔字段自动成为布尔值?
编辑: 我正在使用 Knex
和 node-postgres
,
这是我的 table 定义:
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');
});
我需要使用 pg.types 模块:
import { types } from "pg";
types.setTypeParser(16, (value) => { // 16 is the type enum vaue of boolean
return Boolean(parseInt(value));
});