porsager / porsager/postgres

Dynamic columns in insert and update should handle the case of empty object/array

Open
#644 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

Consider this scenario:

const user = { name: 'Murray', age: 68 }
let columns = [];  // will be filled later 

// assume something went wrong and columns is still empty

await sql`
  insert into users ${
    sql(user, [])
  }
`

Or maybe this:


const user = {}; // will be filled later

// assume something went wrong and user is still empty

await sql`
  insert into users ${
    sql(user)
  }
`

We get this no-so-informative error: PostgresError: syntax error at or near "where".

Since dynamic columns can be, by definition, very dynamic, I think it would make sense to handle these special cases:

  • the object with data (first argument) is "empty"
  • the columns array (second argument) is empty

It would be a simple change in src/types.js, in the update and insert entries in the builders: https://github.com/porsager/postgres/blob/master/cjs/src/types.js#L164-L176

Before using the escapeIdentifier, it would be a matter of checking if the array is empty.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read src/types.js, especially the update and insert builders, and reproduce the two examples with an empty object or columns array. Verify behavior for both empty-input cases so neither produces the reported uninformative PostgreSQL syntax error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.