Skip to content

Instantly share code, notes, and snippets.

@surma
Last active December 21, 2023 02:18
Show Gist options
  • Save surma/53cb57219eea5217d02083b7dd19b711 to your computer and use it in GitHub Desktop.
Save surma/53cb57219eea5217d02083b7dd19b711 to your computer and use it in GitHub Desktop.
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);
});
});
}
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});
`,
);
@64BitAsura
Copy link

64BitAsura commented Oct 16, 2023

@surma "?" what's this delimiter for? "NNNth-parameter" and it will work with number between 1 to SQLLITE_MAX_VARIABLE_NUMBER

@64BitAsura
Copy link

@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