pg-sql-helpers
pg-sql-helpers copied to clipboard
Make `getDefinedKeys` public?
I have an update function that takes 0 or more parameters, and updates a table:
updateRelm: async (client, {
relmName,
isPublic,
encryptedPassword,
defaultEntryway,
}) => {
const updates = {
is_public: isPublic,
encrypted_password: encryptedPassword,
default_entryway: defaultEntryway
}
const res = await client.query(sql`
${UPDATE('relms', updates)}
WHERE relm = ${relmName}
RETURNING *
`)
return mkRelm(res.rows[0])
}
When 0 parameters are passed in, I get a SQL syntax error:
error: syntax error at or near ")"
Digging in, I find that indeed, this SQL is invalid:
UPDATE "relms" SET () = ROW () WHERE relm = $1 RETURNING *
Is there a simple way to check that the updates object in my function has any non-undefined values? Maybe export getDefinedKeys from pg-sql-helpers and check that length > 0?
@canadaduane ah interesting, I'd be open to some sort of helper exported to do that. Maybe there's a specific isEmpty helper that we could add for this use case?
Sure! I think that would make sense. Checking for empty may be the only use case where it's needed.