knex之类的查询动态添加 [英] knex like query dynamically add

查看:321
本文介绍了knex之类的查询动态添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`let add_query = ".andWhere('company_name', 'like','%marker%')";
knex('franchisee').where('franchisee_name', 'like', '%jackmarker%')${add_query}
.then((resp) => {
    console.log("resp >>",resp)
})`

听到我想在节点js中使用knex npm执行类似查询的操作. 我使用变量add_query使用此变量想要动态地进行knex查询.

Hear i want to execute like query using knex npm in node js. I take variable add_query using this variable want to make knex query dynamically.

喜欢此查询-

`knex('franchisee').where('franchisee_name', 'like','%jackmarker%').andWhere('company_name', 'like','%marker%')
.then((resp) => {
    console.log("resp >>",resp)
})`

推荐答案

您可以添加部分查询,例如:

You can add parts of queries like this:

const getResults = (options) => {
    let query = knex('table');
    if (options.where) {
        query = query.where('franchisee_name', 'like', '%jackmarker%');
    }

    if (options.other) {
        query = query.where('company', 'like', '%marker%');
    }

    return query.select();
};

您还可以使用以下原始查询:

You can also use raw queries like this:

const getVehiclePolicies = (options) => {
    return P.try(() => {
        const queries = [
            `
                SELECT
                    vp.id AS vehicle_policy_id,
                    vp.policy_id,
                    vp.policy_number,
                    vp.status,
                    vp.service_count,
                    vp.manufacturer,
                    vp.model,
                    vp.variant,
                    vp.colour,
                    vp.year,
                    vp.policy_owner,
                    vp.policy_owner_email,
                    vp.policy_owner_phone,
                    vp.policy_owner_country_code,
                    vp.vin_number,
                    vp.engine_number,
                    vp.vehicle_type,
                    vp.start_date,
                    vp.end_date,
                    vp.expired,
                    vp.created_at,
                    vp.updated_at
                FROM vehicles AS v
                    INNER JOIN vehicle_policies AS vp ON v.registration_number = vp.vehicle_id
                    INNER JOIN company_policies AS cp ON cp.id = vp.policy_id
                WHERE
                    NOT v.deleted
                    AND NOT vp.deleted
                    AND NOT cp.deleted              
            `
        ];

        const bindings = [];

        if (options.vehicle_id) {
            queries.push('AND vp.vehicle_id = ?');
            bindings.push(options.vehicle_id);
        }

        if (options.policy_number) {
            queries.push('AND vp.policy_number = ?');
            bindings.push(options.policy_number);
        }

        if (options.vehicle_policy_id) {
            queries.push('AND vp.id = ?');
            bindings.push(options.vehicle_policy_id);
        }

        if (options.default_policy) {
            queries.push('AND cp.default_policy');
        }

        queries.push(`LIMIT ? OFFSET ?`);
        bindings.push(options.limit || 15);
        bindings.push(options.offset || 0);

        return db.raw(queries.join(' '), bindings);
    }).then((data) => {
        debug('GET VEHICLE POLICIES DATA');
        debug(data.rows);

        return data.rows;
    });
};

这篇关于knex之类的查询动态添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆