Skip to content

Instantly share code, notes, and snippets.

@allohamora
Forked from surma/db-helper.ts
Last active October 8, 2023 17:10
Show Gist options
  • Save allohamora/79082dc60059b599120a058fcfa10235 to your computer and use it in GitHub Desktop.
Save allohamora/79082dc60059b599120a058fcfa10235 to your computer and use it in GitHub Desktop.
import { Database } from "sqlite3";
class DatabaseHelper {
constructor(private database: Database) {}
public async query (
parts: string[],
...params: any[]
): Promise<sqlite3.RunResult> {
const sql = parts.join("?");
return new Promise((resolve, reject) => {
this.database.run(sql, params, function (err) {
if (err) return reject(err);
resolve(this);
});
});
}
}
const db = new DatabaseHelper(new Database("./db.sqlite"));
const { lastID } = await db.query`
INSERT INTO reminders
(author_id, iso8601, text)
VALUES
(${author_id}, ${iso8601}, ${text});
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment