postgres
postgres copied to clipboard
Document array of fragments?
Is an array of fragments missing in the README?
sql`${[sql`select 1`, sql`as result`]}`
I first saw this in use in this example:
https://github.com/porsager/postgres/issues/217#issuecomment-1156958623
${
Object.keys(rows[0]).map((x, i) =>
sql`${ i ? sql`,` : sql``}${ sql(x) } = excluded.${ sql(x) }`
)
}
For me it wasn't super obvious that you could do that, but it's useful! Maybe add it to https://github.com/porsager/postgres#quick-primer-on-interpolation, something like
| Interpolation syntax | Usage | Example |
|---|---|---|
${ sql`` } |
for keywords or sql fragments | await sql`SELECT * FROM users ${sql`order by age desc` }` |
${ sql(string) } |
for identifiers | await sql`SELECT * FROM ${sql('table_name')` |
${ sql([] or {}, ...) } |
for helpers | await sql`INSERT INTO users ${sql({ name: 'Peter'})}` |
${ 'somevalue' } |
for values | await sql`SELECT * FROM users WHERE age = ${42}` |
${ [sql``, sql``, ...] } |
for sql fragments | some short but still useful example? |
Or maybe add it to the first example?
| Interpolation syntax | Usage | Example |
|---|---|---|
${ sql`` or [sql``, ...] } |
for keywords or sql fragments | await sql`SELECT * FROM users ${sql`order by age desc` }` |
${ sql(string) } |
for identifiers | await sql`SELECT * FROM ${sql('table_name')` |
${ sql([] or {}, ...) } |
for helpers | await sql`INSERT INTO users ${sql({ name: 'Peter'})}` |
${ 'somevalue' } |
for values | await sql`SELECT * FROM users WHERE age = ${42}` |