sqliteBuilder: Opinionated safe(r) query builder using tagged templates
import {
Statement }
from "https://esm.town/v/postpostscript/sqliteBuilder";
const unsafeId =
"1234 or TRUE"
console.
log(
Statement`
SELECT *
FROM table
WHERE id = ${unsafeId}
${Statement`AND otherCondition`}
`)
const results =
await Statement`SELECT ...`.
execute()
Or you can pass it directly to @std/sqlite.execute
:
import { sqlite }
from "https://esm.town/v/std/sqlite"
await sqlite.
execute(
Statement`Select ...`)
You can combine multiple statements using Statement.prototype.combineWith
:
Statement`...`.
combineWith(
Statement`...`,
" AND ")
[
Statement`fieldA`,
Statement`fieldB`,
Statement`fieldC`,
].
reduce(
(a, b) => a.
combineWith(b,
", "))
This is AWESOME! I hate having to do it with with the
?
s@postpostscript, would there be a way to make
Statement
type safe in a way where we could do something like this:Where it would type check if the write types were provided in replacements?
@nbbaier this seems to work!: @postpostscript/sqliteBuilderTyped. [Example](https://www.val.town/v/postpostscript/sqliteBuilderTypedExample
)
@postpostscript Awesome! Really cool, thanks for implementing this
@nbbaier thanks, and no problem!