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

@@ -1,13 +1,12 @@
import { Schema as S } from "@effect/schema"; import { Schema as S } from "@effect/schema";
import { DateTime, Duration, Either as E, Effect, Option as O, pipe } from "effect"; import { Login } from "common/api";
import { AccessLog } from "common/db";
import { DateTime, Duration, Either as E, Effect, Match, Option as O, pipe } from "effect";
import { Api } from "make-api";
import { brotliCompress } from "node:zlib";
import { RequestError } from "./RequestError"; import { RequestError } from "./RequestError";
import { Database } from "./services/database"; import { Database } from "./services/database";
import { Request } from "./services/request"; import { Request } from "./services/request";
import { Match } from "effect";
import { Api } from "make-api";
import { Login } from "common/api";
import { AccessLog } from "common/db";
import { brotliCompress } from "node:zlib";
const match = (api: Api.Api.Any) => Effect.gen(function* () { const match = (api: Api.Api.Any) => Effect.gen(function* () {

View File

@@ -4,6 +4,7 @@ import { SessionId, UserId } from "common";
import { User } from "common/db"; import { User } from "common/db";
import { Context, Effect, Layer, pipe } from "effect"; import { Context, Effect, Layer, pipe } from "effect";
import { NoSuchElementException } from "effect/Cause"; import { NoSuchElementException } from "effect/Cause";
import { ulid } from "ulid";
export function generateSessionId(byteLength: number = 12): SessionId { export function generateSessionId(byteLength: number = 12): SessionId {
const array = new Uint8Array(byteLength); const array = new Uint8Array(byteLength);
@@ -73,6 +74,15 @@ export const DatabaseLive = (filePath: string = "db.sqlite3") => Layer.effect(Da
mediaType TEXT NOT NULL, mediaType TEXT NOT NULL,
${systemInformation} ${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) => { Effect.map((db) => {
const getUserByUsername = db.prepare("SELECT userId, username, password, admin FROM User WHERE username = ?"); const getUserByUsername = db.prepare("SELECT userId, username, password, admin FROM User WHERE username = ?");