Knexjs upsert with update

import Knex from "knex";

const knex = Knex({
  client: 'pg',
  connection: process.env.DATABASE_URL,
});

const records = [
  { id: 2, login: "admin" },
];

async function upsert() {
  const result = await knex.raw(
      `? ON CONFLICT (id)
              DO UPDATE SET
              login = EXCLUDED.login,
              updated_at = CURRENT_TIMESTAMP
            RETURNING *;`,
      [knex("users").insert(records)],
    );
}
27 марта 2024, 09:55

Knex join with subquery

knex(
  knex('A').where('A.id',1).as('t1')
).leftJoin(
  knex('B').where('B.id', 2).as('t2'), 
  't1.c', 
  't2.d'
)
15 января 2021, 10:33