porsager / porsager/postgres

Feature request, update SQL insert functionality to support sql fragments

Open
#1,071 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I can do:

await sql`insert into cats ${sql(cat)}`

But sometimes I want to take all the fields from the cat object and also generate a few fields from nested SQL expressions, it would be great if I could do this:

await sql`insert into cats ${sql({
   ...cat,
  serialNumber: sql`select serial_number from cat_tracking where name = ${cat.name}`,
)}`

Sometimes we use json schemas to validate our objects, so we know there's nothing harmful there. Without this functionality we have to update all our SQL statements to cater for new properties added to our schema. With this suggested change the SQL queries would automatically work with our schema updates without any changes needed.

The shorthand provided works great but currently falls over when one of the fields is generated from a nested SQL expression.

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

Start from the implementation of the object shorthand used by sql(cat) and trace how interpolated values are classified and rendered. Verify the nested sql fragment in the issue produces the expected INSERT expression while preserving ordinary object fields, then run the existing test suite and add coverage for this example if the relevant tests are located.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql
Domain
backend, databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.