Feature request, update SQL insert functionality to support sql fragments
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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