porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.04k stars 257 forks source link

Dynamic columns in updates #799

Open rbarszcz opened 5 months ago

rbarszcz commented 5 months ago

trying to make an update with dynamic columns and i'm getting a bunch of errors

object cliente: {

id: 3,
nome: 'Teste Um',
tipo: 'F',
cpf: '12345678910',
cnpj: null,
email: 'teste@black.com',
telefone: '46111111111',
status: 'A',
datacadastro: '2024-02-01'

}

query

const cols = ['nome', 'tipo', 'cpf', 'cnpj', 'email', 'telefone'];
const update = await sql`UPDATE clientes SET (${sql(cliente, cols)}) WHERE id=${sql(cliente.id)}`;

error: Cannot convert undefined or null to object

query 2 (removed the null fields)

const cols = ['nome', 'tipo', 'cpf', 'email', 'telefone'];
const update = await sql`UPDATE clientes SET (${sql(cliente, cols)}) WHERE id=${sql(cliente.id)}`;

error: syntax error at or near "$1"

query 3 (removed the () after SET)

const cols = ['nome', 'tipo', 'cpf', 'cnpj', 'email', 'telefone'];
const update = await sql`UPDATE clientes SET ${sql(cliente, cols)} WHERE id=${sql(cliente.id)}`;

error: syntax error at end of input

any thoughts?

lnlife commented 5 months ago

WHERE id=${sql(cliente.id)} to WHERE id=${cliente.id}

rbarszcz commented 5 months ago

thanks.. damn, i feel stupid.. didn't see i wrapped it up with the sql()..