繁体   English   中英

在knex中将whereRaw子句用于Postgres客户端

[英]Using whereRaw clause in knex for postgres client

如果我想从给定以下参数的表中获取所有列,则以下分页查询工作正常

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输出,但是我不确定这是否有帮助,因为它只是从提到的列中获取所有数据。

我发现的一种解决方案是在查询中与whereRaw一起使用另一个where子句。 但是我仍然想听听是否有人知道我在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
}

暂无
暂无

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

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