-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Better templating for @vercel/postgres
#494
base: main
Are you sure you want to change the base?
feat: Better templating for @vercel/postgres
#494
Conversation
🦋 Changeset detectedLatest commit: 710f1c0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
import { VercelPostgresError } from './error'; | ||
|
||
export type Primitive = string | number | boolean | undefined | null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the type of values
is now unknown[]
instead of Primitive[]
, but this is backwards-compatible because all Primitive
values are assignable to unknown
.
values, | ||
set, | ||
unsafe, | ||
query: debug, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this is just me, but i expected debug
to behave differently. Here is how:
- I run into a bad query and get some neon db error i don't understand
- I go into the code and switch
sql
withdebug
- get the query logged or thrown
instead the return type of debug
is different to sql
, so TS is unhappy and also my runtime error changed because, for example i can't read .rows
on the result of debug
. Does it make sense to wrap this with a little function to enhance DX event more?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that might be a good call... log the query and the parameters (with a check on something like process.env.DEVELOPMENT
or something) and still send the query to the database...
TODO:
What problem does this fix?
Right now, SQL templating is very limited. This is cool:
... but it falls apart quickly:
It completely falls apart for any sort of dynamic query building, such as is often required for bulk
UPDATE
/INSERT
statements. This adds the following API surface area:debug
(strings: TemplateStringsArray, ...values: unknown[]) => [string, unknown[]]
fragment
(strings: TemplateStringsArray, ...values: unknown[]) => [string, unknown[]]
query
, but returns aTqlFragment
node which can be recursively nested within itself and included in a top-levelquery
.identifiers
(ids: string | string[]) => TqlIdentifiers
list
(parameters: unknown[]) => TqlList
[1, 2, 3]
would become($1, $2, $3)
with the original values stored in the parameters array.values
(entries: ValuesObject) => TqlValues
, whereValuesObject
is{ [columnName: string]: unknown }
or an array of that object type.set
(entry: SetObject) => TqlSet
, whereSetObject
is{ [columnName: string]: unknown }
.unsafe
(str: string) => TqlTemplateString
The API for
sql
remains unchanged.An example, using the above (obviously somewhat contrived to show how various API works):
Full docs for the TQL spec