-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
README.md finished, API finished for 1.0
- Loading branch information
Showing
20 changed files
with
673 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Container } from "nwire" | ||
import { TasksCreator } from "./TasksCreator" | ||
import { createDatabase } from "./createDatabase" | ||
import { SQLiteTaskStore } from "./SQLiteTaskStore" | ||
import { Injected } from "nwire" | ||
|
||
export type AppContext = { | ||
db: Awaited<ReturnType<typeof createDatabase>> | ||
tasksCreator: TasksCreator | ||
tasks: SQLiteTaskStore | ||
} | ||
|
||
export async function createContext() { | ||
const database = await createDatabase() | ||
|
||
const context = Container.build<AppContext>() | ||
.register("db", () => database) | ||
.instance("tasksCreator", TasksCreator) | ||
.instance("tasks", SQLiteTaskStore) | ||
.context() | ||
|
||
return context | ||
} | ||
|
||
export class Service extends Injected<AppContext> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,27 @@ | ||
import { Container } from "nwire" | ||
import { TaskStore } from "./TaskStore" | ||
import * as sqlite from "sqlite" | ||
import { Task } from "./Task" | ||
import * as sqlite3 from "sqlite3" | ||
|
||
export class SQLiteTaskStore implements TaskStore { | ||
db!: sqlite.Database | ||
|
||
constructor() { | ||
;(async () => { | ||
this.db = await sqlite.open({ | ||
filename: ":memory:", | ||
driver: sqlite3.Database, | ||
}) | ||
|
||
this.db.run( | ||
`CREATE TABLE IF NOT EXISTS tasks ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL, | ||
completed INTEGER NOT NULL DEFAULT 0 | ||
)` | ||
) | ||
|
||
this.db.run( | ||
`CREATE TABLE IF NOT EXISTS users ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT NOT NULL | ||
)` | ||
) | ||
|
||
this.db.run( | ||
`CREATE TABLE IF NOT EXISTS users_tasks ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
user_id INTEGER NOT NULL, | ||
task_id INTEGER NOT NULL, | ||
FOREIGN KEY(user_id) REFERENCES users(id), | ||
FOREIGN KEY(task_id) REFERENCES tasks(id) | ||
)` | ||
) | ||
})() | ||
} | ||
import { Service } from "./AppContext" | ||
|
||
export class SQLiteTaskStore extends Service implements TaskStore { | ||
async get(id: number): Promise<Task | null> { | ||
return (await this.db.get(`SELECT * FROM tasks WHERE id = ?`, [id])) ?? null | ||
return ( | ||
(await this.context.db.get(`SELECT * FROM tasks WHERE id = ?`, [id])) ?? | ||
null | ||
) | ||
} | ||
|
||
async save(title: string): Promise<Task> { | ||
const insert = await this.db.run(`INSERT INTO tasks (title) VALUES (?);`, [ | ||
title, | ||
]) | ||
const insert = await this.context.db.run( | ||
`INSERT INTO tasks (title) VALUES (?);`, | ||
[title] | ||
) | ||
|
||
if (!insert.lastID) throw new Error("unable to save task") | ||
|
||
return (await this.get(insert.lastID))! | ||
} | ||
|
||
async delete(id: number): Promise<void> { | ||
await this.db.run(`DELETE FROM tasks WHERE id = ?`, [id]) | ||
await this.context.db.run(`DELETE FROM tasks WHERE id = ?`, [id]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as sqlite from "sqlite" | ||
import * as sqlite3 from "sqlite3" | ||
|
||
export async function createDatabase() { | ||
const db = await sqlite.open({ | ||
filename: ":memory:", | ||
driver: sqlite3.Database, | ||
}) | ||
|
||
await db.run( | ||
`CREATE TABLE IF NOT EXISTS tasks ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL, | ||
completed INTEGER NOT NULL DEFAULT 0 | ||
)` | ||
) | ||
|
||
await db.run( | ||
`CREATE TABLE IF NOT EXISTS users ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
name TEXT NOT NULL | ||
)` | ||
) | ||
|
||
await db.run( | ||
`CREATE TABLE IF NOT EXISTS users_tasks ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
user_id INTEGER NOT NULL, | ||
task_id INTEGER NOT NULL, | ||
FOREIGN KEY(user_id) REFERENCES users(id), | ||
FOREIGN KEY(task_id) REFERENCES tasks(id) | ||
)` | ||
) | ||
|
||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.