Skip to content

Instantly share code, notes, and snippets.

@aminroosta
Created May 20, 2020 06:20
Show Gist options
  • Save aminroosta/6239f103c8bdb80f435998fd77df1f86 to your computer and use it in GitHub Desktop.
Save aminroosta/6239f103c8bdb80f435998fd77df1f86 to your computer and use it in GitHub Desktop.
deno sqlite example
import { open, save, DB } from "https://deno.land/x/sqlite/mod.ts";
class Database {
private constructor(private db: DB) { }
static async connect(path : string) {
return new Database(await open(path));
}
execute(query: string, values : object = {}) {
this.db.query(query, values).done();
}
query(query: string, values: object = { }) {
return this.db.query(query, values);
}
select<T>(query: string, values: object = {}) {
}
}
Database.connect("test.db");
const db = await open("test.db");
db.query(`
CREATE TABLE IF NOT EXISTS people (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
)`, {});
db.query("delete from people", {});
const names = ["Peter Parker", "Clark Kent", "Bruce Wayne"];
// Run a simple query
for (const name of names)
db.query("INSERT INTO people (name) VALUES (:name)", {name});
let res = db.query('select name,id from people', {});
let r = res.columns();
res.done();
console.warn(r);
// Print out data in table
for (const [name] of db.query("SELECT name FROM people", {}))
console.log(name);
// Save and close connection
await save(db);
db.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment