关于结合 where builders 并将其用作任何其他变量中的查询块的新手问题

Newbie question on combining where builders and using it as a query block in any other variable

我如何使用 .where 和 .andWhere 查询构建器(我在下面的公司中使用过)作为一个块,我的意思是像另一个变量中的组合查询构建器(如果可能)?

const companies = await db('company')
  .join('address as a1', 'a1.id', 'company.address_id')
  .select('company.name', 'company.membership', 'a1.name as address')
  .where((builder) => {
    if (membership === 'all') {
      builder.whereNotNull('company.membership');
    } else {
      const data = membership === 'member' ? true : false;
      builder.where('company.membership', data);
    }
  })
  .andWhere((builder) => {
    if (location === 'all') {
      builder.whereNotNull('company.id');
    } else {
      builder.whereIn('company.address_id', function () {
        this.select('id').from('address').where('id', address_id);
      });
    }
  });

const count = await db('company')
  .count('company.address_id')
  .join('address as a1', 'a1.id', 'company.address_id')
  .where((builder) => {
    if (membership === 'all') {
      builder.whereNotNull('company.membership');
    } else {
      const data = membership === 'member' ? true : false;
      builder.where('company.membership', data);
    }
  })
  .andWhere((builder) => {
    if (location === 'all') {
      builder.whereNotNull('company.id');
    } else {
      builder.whereIn('company.address_id', function () {
        this.select('id').from('address').where('id', address_id);
      });
    }
  });

我需要像下面这样的计数变量,而不是重复代码块。

const count = await db('company')
  .count('company.address_id')
  .join('address as a1', 'a1.id', 'company.address_id')
  .COMBINED QUERY of where and andWhere in companies <-----------

Knex 有解决这个问题的方法吗?

你有几个选择:

  1. 按函数重用 where 块。
const whereBuilder = (builder) => {
  if (membership === 'all') {
    builder.whereNotNull('company.membership');
  } else {
    const data = membership === 'member' ? true : false;
    builder.where('company.membership', data);
  }

  if (location === 'all') {
    builder.whereNotNull('company.id');
  } else {
    builder.whereIn('company.address_id', function () {
      this.select('id').from('address').where('id', address_id);
    });
  }
};

const companies = await db('company')
  .join('address as a1', 'a1.id', 'company.address_id')
  .select('company.name', 'company.membership', 'a1.name as address')
  .where(whereBuilder);

const count = await db('company')
  .count('company.address_id')
  .join('address as a1', 'a1.id', 'company.address_id')
  .where(whereBuilder);
  1. 重用整个查询
// --------------V pay attention that there is no await
const baseQuery = db('company')
  .join('address as a1', 'a1.id', 'company.address_id')
  .where((builder) => {
    if (membership === 'all') {
      builder.whereNotNull('company.membership');
    } else {
      const data = membership === 'member' ? true : false;
      builder.where('company.membership', data);
    }
  })
  .andWhere((builder) => {
    if (location === 'all') {
      builder.whereNotNull('company.id');
    } else {
      builder.whereIn('company.address_id', function () {
        this.select('id').from('address').where('id', address_id);
      });
    }
  })

const companies = await baseQuery.clone()
  .select('company.name', 'company.membership', 'a1.name as address');

const count = await baseQuery.clone().count('company.address_id')