question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Template tag for EdgeQL

See original GitHub issue

Proposal: 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:open
  • Created a year ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
haikyuucommented, Jul 14, 2022

That’s virtually impossible to implement with just template literals

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

1reaction
colinhackscommented, Jul 13, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found