'What is a set analogue from squel.js in knex.js?

How can I rewrite it using Knex.js?

type === 'insert' ?
        sql = squel.insert()
            .set('id', userId)
            .set('type', 'open')
            .set('server', server)
            .set('created_at', timestamp)
            .set('updated_at', timestamp)
            .into('saved_users') :
        sql = squel.update()
            .table('saved_users')
            .set('updated_at', timestamp)
            .where(`id = "${userId}"`)

    for (const param in player) {
        if (param === 'full_response') continue;
        const value = player[param];
        if (param === 'kill') {
            sql.set(`\`${param}\``, value ?? 0)
        } else {
            sql.set(param, value ?? 0)
        }
    }

I'm using .set(..) to append the insert or update query. How to do it using Knex?



Solution 1:[1]

Something like that =/ Maybe it can better

const data = {}

    const query = knex("saved_users")

    for (const param in player) {
        if (param === 'full_response') continue;
        const value = player[param];
        if (param === 'kill') {
            data[param] = value ?? 0
        } else {
            data[param] = value ?? 0
        }
    }
    if (type === "insert") {
        data["id"] = userId;
        data["type"] = "open";
        data["server"] = server;
        data["created_at"] = timestamp;
        data["updated_at"] = timestamp;
        await query.insert(data)
    } else {
        data["updated_at"] = timestamp
        await query.update(data).where({
            id: userId
        })
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Khabr