简体   繁体   English

如何在 knex 查询中使用数据库条件?

[英]How to use database conditions inside knex query?

Using knex I have created 2 related tables - users and users_info :使用knex我创建了 2 个相关表 - usersusers_info

Here is users table:这是users表:

exports.up = function(knex) {
  return knex.schema.createTable('users', table => {
    table.uuid('id').notNullable().defaultTo(knex.raw('gen_random_uuid ()')).primary()
    table.string('email').notNullable()
    table.string('password').notNullable()
    table.string('personal_id').notNullable()
    table.string('two_fa').nullable()
    table.boolean('notify').nullable().defaultTo(false)
    table.boolean('changed_email').notNullable().defaultTo(false)
    table.timestamp('changed_password_at').nullable().defaultTo(null)
    table.timestamp('created_at').defaultTo(knex.fn.now())
    table.timestamp('updated_at').defaultTo(knex.fn.now())
  })
};

Here is users_info table:这是users_info表:

exports.up = function(knex) {
  return knex.schema.createTable('users_info', table => {
    table.uuid('id').notNullable().defaultTo(knex.raw('gen_random_uuid ()')).primary()
    table.uuid('user_id')
      .references('id')
      .inTable('users')
      .notNullable()
    table.string('username').notNullable()
    table.string('first_name').nullable()
    table.string('last_name').nullable()
    table.boolean('show_email').notNullable().defaultTo(false)
    table.timestamp('created_at').defaultTo(knex.fn.now())
    table.timestamp('updated_at').defaultTo(knex.fn.now())
  })
};

Also, I have query, where I get data from both of tables:另外,我有查询,我从两个表中获取数据:

const result = knex(tableName)
      .leftJoin('users_info', 'users_info.user_id', 'users.id')
      .where('users.personal_id', personalId)
      .first(
        'users.personal_id as personalId',
        'users_info.first_name',
        'users_info.last_name',
      )

What I want to do, is to get email field from users table only when value of show_email field in users_info table is true.我想要做的是,仅当users_info表中show_email字段的值为 true 时才从users表中获取email字段。

How can I do this?我怎样才能做到这一点? I know it is possible to do, because my database (I use postgres ) has if statements in its engine, but I don't know how to do it with knex query.我知道可以这样做,因为我的数据库(我使用postgres )在其引擎中有if语句,但我不知道如何使用knex查询来做到这一点。

Actually, IF ELSE statements are used to create functions or procedures.实际上, IF ELSE语句用于创建函数或过程。

For example:例如:

DO $$
DECLARE
  a integer := 10;
  b integer := 20;
BEGIN 
  IF a > b THEN
    RAISE NOTICE 'a is greater than b';
  END IF;

  IF a < b THEN
    RAISE NOTICE 'a is less than b';
  END IF;

  IF a = b THEN
    RAISE NOTICE 'a is equal to b';
  END IF;
END $$;

If you want to get data using conditions, you need to use CASE statements.如果要使用条件获取数据,则需要使用CASE语句。 In this case, to get user's email if show_email value is true using knex query, you can do this like this:在这种情况下,如果show_email值为真,则使用 knex 查询获取用户的电子邮件,您可以这样做:

const result = knex(tableName)
      .leftJoin('users_info', 'users_info.user_id', 'users.id')
      .where('users.personal_id', personalId)
      .first(
        'users.personal_id as personalId',
        'users_info.first_name',
        'users_info.last_name',
        knex.raw(`(CASE WHEN users_info.show_email IS TRUE THEN email END) as email`)
      )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM