Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knex like query dynamically add

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

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.

Like this query-

`knex('franchisee').where('franchisee_name', 'like','%jackmarker%').andWhere('company_name', 'like','%marker%')
.then((resp) => {
    console.log("resp >>",resp)
})`
like image 286
jack Avatar asked Dec 13 '22 19:12

jack


2 Answers

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;
    });
};
like image 101
Abhyudit Jain Avatar answered Dec 21 '22 11:12

Abhyudit Jain


You can use a shorthand Query Builder like

Const fname = () =>knex(tableName)
.where(builder=>
builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]))
.andWhere(()=> {
If(condition)this.where('id', '>', 10)
}))
like image 32
Snivio Avatar answered Dec 21 '22 10:12

Snivio