-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
deps_injection.ts
40 lines (31 loc) · 1.02 KB
/
deps_injection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import application, { dependency } from "../server/mod.ts";
import { DB } from "https://deno.land/x/sqlite@v3.3.0/mod.ts";
const app = application();
const db = new DB("test.db");
const deps = dependency();
deps.set("hello", () => "Hello world");
deps.set("db", db);
app.use(deps);
app.get("/", () => {
type FunctionType = () => string;
const fn = <FunctionType> app.getDeps("hello");
return new Response(fn());
});
app.post("/name", () => {
const db = <DB> app.getDeps("db");
db.query(`CREATE TABLE IF NOT EXISTS people (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT)`);
const names = ["Peter Parker", "Clark Kent", "Bruce Wayne"];
for (const name of names) {
db.query("INSERT INTO people (name) VALUES (?)", [name]);
}
return new Response(JSON.stringify(names));
});
app.get("/name", () => {
const db = <DB> app.getDeps("db");
const res = db.query("SELECT name FROM people");
return new Response(JSON.stringify(res));
});
console.log("Listening on: http://localhost:8000");
await app.serve();