knex像查询动态添加

时间:2017-12-27 06:02:54

标签: node.js express knex.js

`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查询。

喜欢这个查询 -

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

2 个答案:

答案 0 :(得分:2)

您可以添加以下部分查询:

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();
};

你也可以使用这样的原始查询:

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;
    });
};

答案 1 :(得分:2)

您可以使用速记查询生成器之类的

Const fname = () =>knex(tableName)
.where(builder=>
builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]))
.andWhere(()=> {
If(condition)this.where('id', '>', 10)
}))