Skip to content
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

Better documentation for tagged template and sql() usage #4

Closed
porsager opened this issue Jan 1, 2020 · 0 comments
Closed

Better documentation for tagged template and sql() usage #4

porsager opened this issue Jan 1, 2020 · 0 comments
Assignees
Labels

Comments

@porsager
Copy link
Owner

@porsager porsager commented Jan 1, 2020

I thought I had finished the docs for sql() usage and template strings, but I see they're incomplete.

I think at the very start it would be great with a completely basic description of how the tagged template sql fn are to be used.

Any raw value inside a template string will be replaced with $n and sent as a parameter to postgresql inferring the correct type. If a value needs to be passed directly in the query sql(value) is to be used which ensures escaping happens properly. It also has overloads to expand the most common scenarios in the correct context.

Eg: sql({ a: 'first', b: new Date() })

  • in insert context it will expand to (a, b) values ($1, $2)
  • in update / where context it will expand to a = $1, b = $2
  • in select context it will expand to $1 as a, $2 as b
    and $1 and $2 will be sent as parameters with correct types (string, timestamp) to postgres

As extra help you can supply an array of objects to insert multiple rows in insert context, and also extra arguments to only pluck specific columns from the object. (encouraged for safety).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.