pg-sql-helpers icon indicating copy to clipboard operation
pg-sql-helpers copied to clipboard

Make `getDefinedKeys` public?

Open canadaduane opened this issue 5 years ago • 2 comments

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 avatar Jul 09 '20 16:07 canadaduane

@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?

ianstormtaylor avatar Nov 22 '20 17:11 ianstormtaylor

Sure! I think that would make sense. Checking for empty may be the only use case where it's needed.

canadaduane avatar Nov 22 '20 21:11 canadaduane