Template tag for EdgeQL
See original GitHub issueProposal: provide a template tag for writing sanitized EdgeQL queries.
User input
import {edgeql} from "edgedb";
const query = edgeql`insert User { title := ${req.body.title}}`
// => { query: 'insert User { title := <str>$var_0 }', parameters: {var_0: "Some title"}}
We’d overload the query methods to support an object-based query format: {query: string; parameters: {[k:string]: any}}
await client.query(query)
The cast type will be inferred from the input data according to the following rules:
Array.isArray(data) === true
-> check that contents are homogenous primitives ->array<element-type>
object
->json
string
->str
number
->float64
bigint
->bigint
boolean
->bool
Implicit/assignment casting makes this workable. e.g. Assigning a float64
to a int16
value will work without an explicit cast.
Identifiers
There should be a separate API for specifying identifiers: edgeql.ident()
. This would verify that the input is a valid identifier with a regex.
import {edgeql, ident} from "edgedb";
const field1 = 'User'
const field2 = 'email'
edgeql`insert ${ident(field1)} { ${ident(field2)} };`
For convenience this function can be variadic. The inputs will be validated and joined with ", "
.
edgeql`select User { ${ident('name', 'email')} }`
// select User { name, email }
### Prior art:
https://github.com/felixfbecker/node-sql-template-strings
https://github.com/blakeembrey/sql-template-tag
https://www.npmjs.com/package/pg-format
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Select — EdgeQL | EdgeDB Docs
Let's start by selecting all Villain objects in the database. In this example, there are only three. Remember, Villain is a reference to...
Read more >Pull requests · edgedb/edgedb-js - GitHub
The official TypeScript/JS client library and query builder for EdgeDB - Pull requests · edgedb/edgedb-js.
Read more >My experience with EdgeDB - divan's blog
After EdgeQL, I don't want to return to SQL ever again. ... about querying data really well (humans don't think in JOINs, for...
Read more >How to use query result in if statement tag in Django template
You cannot compare asset_class with string, but you can compare name attribute which is string. So: {% for ETF in ETFs %} <td>{{...
Read more >EdgeDB: EdgeQL - Hacker News
EdgeQL, the query language of EdgeDB, is a very interesting piece of ... but then if I edit the code in an example...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Indeed, but it would be possible with a VSCode extension. It would introspect each query at dev-time and tell typescript what type that is
No, this template tag will not be schema-aware nor can it infer types. That’s virtually impossible to implement with just template literals - you should use the query builder if you want inferred types and typechecking.