Initialize DB with admin user

This commit is contained in:
2024-08-10 23:40:07 +02:00
parent a5b16cbdf9
commit a0a6681ae0
2 changed files with 15 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import { SessionId, UserId } from "common";
import { User } from "common/db";
import { Context, Effect, Layer, pipe } from "effect";
import { NoSuchElementException } from "effect/Cause";
import { ulid } from "ulid";
export function generateSessionId(byteLength: number = 12): SessionId {
const array = new Uint8Array(byteLength);
@@ -73,6 +74,15 @@ export const DatabaseLive = (filePath: string = "db.sqlite3") => Layer.effect(Da
mediaType TEXT NOT NULL,
${systemInformation}
)`);
const userCount = db.query("SELECT COUNT(*) FROM User").values()[0][0] as number;
if (userCount <= 0) {
const userId = UserId.make(ulid());
const username = "admin";
const password = Bun.password.hashSync("admin");
const admin = true;
db.exec("INSERT INTO User (userId, username, password, admin) VALUES (?, ?, ?, ?)", userId, username, password, admin);
}
})),
Effect.map((db) => {
const getUserByUsername = db.prepare("SELECT userId, username, password, admin FROM User WHERE username = ?");