From 88cbeee56261b68a449e71c6cdeec85d39fceef6 Mon Sep 17 00:00:00 2001 From: Quad Date: Wed, 13 Apr 2022 17:14:13 +0200 Subject: [PATCH] SQLite boilerplate --- .gitignore | 3 +++ README.md | 2 +- schema.sql | 15 +++++++++++++++ src/modules/blog.nim | 2 +- src/modules/db.nim | 32 ++++++++++++++++++++++++++++++++ src/nimblog.nim | 12 +++++++++++- 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 schema.sql create mode 100644 src/modules/db.nim diff --git a/.gitignore b/.gitignore index eaa6293..06603ad 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ # Binary name built by nimble nimblog + +# Database +blog.db diff --git a/README.md b/README.md index 87060c8..d2fbf0d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # nimblog -Placeholder text +Basic blog written in nim, mostly for learning purposes. ## How to run diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..a5bcd72 --- /dev/null +++ b/schema.sql @@ -0,0 +1,15 @@ +CREATE TABLE users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + displayname TEXT NOT NULL, + username TEXT UNIQUE NOT NULL, + password TEXT NOT NULL +); + +CREATE TABLE posts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + author_id INTEGER NOT NULL, + created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + title TEXT NOT NULL, + body TEXT NOT NULL, + FOREIGN KEY (author_id) REFERENCES users (id) +); diff --git a/src/modules/blog.nim b/src/modules/blog.nim index 645b478..67699da 100644 --- a/src/modules/blog.nim +++ b/src/modules/blog.nim @@ -2,7 +2,7 @@ import prologue import prologue/middlewares/staticfile import views -proc runWebsite(settings: Table) = +proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string]) = let prologueSettings = newSettings( debug = settings["debug"] ) diff --git a/src/modules/db.nim b/src/modules/db.nim new file mode 100644 index 0000000..8b01886 --- /dev/null +++ b/src/modules/db.nim @@ -0,0 +1,32 @@ +import tables +import std/[os, db_sqlite, strutils, streams] + +proc createDb(dbSettings: Table): bool = + if not fileExists(dbSettings["path"]): + var userConsent: bool = false + while userConsent != true: + stdout.write "Did not find database. Create one now? (y/n) " + stdout.flushFile() + var userInput = readChar(stdin) + stdin.flushFile() + case userInput + of 'y', 'Y': + userConsent = true + of 'n', 'N': + return false + else: + discard + + let + schema = readFile(dbSettings["schema_path"]) + db = open(dbSettings["path"], "", "", "") + for command in schema.split(";"): + # Skip "command" if it's just a blank line + if command == "\c\n" or command == "\n": + continue + db.exec(sql(command.strip)) + echo "Ran command: " & command.strip + db.close() + return true + +export createDb diff --git a/src/nimblog.nim b/src/nimblog.nim index e3eb2f3..f1c6a67 100644 --- a/src/nimblog.nim +++ b/src/nimblog.nim @@ -2,14 +2,24 @@ when isMainModule: import tables import modules/blog import modules/env + import modules/db let settings = { "debug": boolEnvOrDefault("DEBUG", false), "logging": boolEnvOrDefault("NIMBLOG_LOG", true) }.toTable() + let dbSettings = { + "path": "blog.db", + "schema_path": "schema.sql" + }.toTable() + + if not createDb(dbSettings): + echo "Failed to prepare database" + quit(QuitFailure) + try: echo "Starting website" - runWebsite(settings) + runWebsite(settings=settings, dbSettings=dbSettings) except: echo "Could not run website"