在 knex 中为 postgres 客户端使用 whereRaw 子句

Using whereRaw clause in knex for postgres client

如果我想在给定以下参数

的情况下从 table 中获取所有列,则以下分页查询可以正常工作
getOrdersPagination(limit, after) {

    let where = '';
    if (after > 0) {
        where = `id < ${after}`;
    }

    return knex
        .select(
            'id',
            'patient_id AS patientId',
            'pharmacy_id AS pharmacyId',
            'customer_id AS customerId',
            'department_id AS departmentId',
            'user_id AS userId',
            'status',
            'info',
            'created_at AS createdAt'
        )
        .from('order')
        .whereRaw(where)
        .orderBy('id', 'desc')
        .limit(limit);
}

但是,如果我传递另一个参数(在本例中为 status)以进一步过滤掉返回的行,我将得到与上述查询完全相同的输出。 status 字段(整数)对我的 getOrdersByStatusPagination 查询没有任何影响。

getOrdersByStatusPagination(limit, after, status) {

    let where = '';
    if (after > 0) {
        where = `id < ${after} AND status = ${status}`;
    }

    return knex
        .select(
            'id',
            'patient_id AS patientId',
            'pharmacy_id AS pharmacyId',
            'customer_id AS customerId',
            'department_id AS departmentId',
            'user_id AS userId',
            'status',
            'info',
            'created_at AS createdAt'
        )
        .from('order')
        .whereRaw(where)
        .orderBy('id', 'desc')
        .limit(limit);
}

我想知道我是否正确地使用 whereRaw 子句来包含多个使用 AND 运算符的 where 语句。

http://knexjs.org/#Builder-whereRaw

我还可以包括查询的示例 JSON 输出,但我不确定这是否有任何帮助,因为它只是从提到的列中获取所有数据。

我想出的一个解决方案是在查询中使用另一个 where 子句和 whereRaw。但我仍然想听听是否有人知道如何在 whereRaw 选项

中使用 status arg

使用另一个 where 子句的解决方案

getOrdersByStatusPagination(limit, after, status) {

    let where = '';
    if (after > 0) {
        where = `id < ${after}`;
    }

    return knex
        .select(
            'id',
            'patient_id AS patientId',
            'pharmacy_id AS pharmacyId',
            'customer_id AS customerId',
            'department_id AS departmentId',
            'user_id AS userId',
            'status',
            'info',
            'created_at AS createdAt'
        )
        .from('order')
        .whereRaw(where)
        .where('status', '=', status)
        .orderBy('id', 'desc')
        .limit(limit);
}

那么,这是 knex 解决问题的方法

基本思路是。您创建了一个 knex 构建器的实例,并有条件地向其添加子句。

getOrdersByStatusPagination (limit, after, status) {
    const builder = knex
        .select(
            'id',
            'patient_id AS patientId',
            'pharmacy_id AS pharmacyId',
            'customer_id AS customerId',
            'department_id AS departmentId',
            'user_id AS userId',
            'status',
            'info',
            'created_at AS createdAt'
        )
        .from('order')
        .whereRaw(where)
        .where('status', status) // No need for '=' argument. Knex inserts it by default
        .orderBy('id', 'desc')
        .limit(limit);

    if (after > 0) {
        builder.where('id', '<', after) // No need for .whereRaw method. You can use the same as for statuses' '='
        // or builder.whereRaw(`id < ${after}`)
    }
    return builder
}