dyedgreen / deno-sqlite

Deno SQLite module
https://deno.land/x/sqlite
MIT License
409 stars 36 forks source link

Simplify Example #48

Closed jcuenod closed 4 years ago

jcuenod commented 4 years ago

I was recently talking about deno to someone who said it wasn't accessible to newcomers. I thought about this when I saw this in the example:

const name = ["Peter Parker", "Clark Kent", "Bruce Wayne"][Math.floor(Math.random() * 3)]

I realise that this makes the demo more interesting but I think it may detract from communicating clearly how the library works.

Forgive the backstory and obviously it's up to you what to do but I think "hard" hard-coding name would make the example more clear.

dyedgreen commented 4 years ago

Yeah, agreed. Any comments on the below version?

import { open, save } from "https://deno.land/x/sqlite/mod.ts";

// Open a database
const db = await open("test.db");
db.query("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");

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]);

// 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();
jcuenod commented 4 years ago

Ja, this is great!