-
-
Save surma/53cb57219eea5217d02083b7dd19b711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type * as sqlite3 from "sqlite3"; | |
export interface ParameterizedQuery { | |
sql: string; | |
params?: any; | |
} | |
export function sql( | |
parts: readonly string[], | |
...params: any[] | |
): ParameterizedQuery { | |
return { | |
sql: parts.join("?"), | |
params, | |
}; | |
} | |
async function run( | |
db: sqlite3.Database, | |
query: ParameterizedQuery | |
): Promise<sqlite3.RunResult> { | |
return new Promise((resolve, reject) => { | |
db.run(query.sql, query.params, function (err) { | |
if (err) return reject(Error(err.message)); | |
resolve(this); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as sqlite3 from "sqlite3"; | |
import {run} from "./db-helper.js"; | |
const db = new sqlite3.Database("./db.sqlite"); | |
const { lastID } = await run( | |
db, | |
sql` | |
INSERT INTO reminders | |
(author_id, iso8601, text) | |
VALUES | |
(${author_id}, ${iso8601}, ${text}); | |
`, | |
); |
@surma got it, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates Where is my head, I totally forget about tagged template, foundation of lit-html using ECMA tagged string template
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@surma "?" what's this delimiter for? "NNNth-parameter" and it will work with number between 1 to SQLLITE_MAX_VARIABLE_NUMBER