Add create file table, insert first user, fully port to tailwind

This commit is contained in:
2024-11-19 21:45:10 +01:00
parent 63de1a3b02
commit 5f0b8a69d3
10 changed files with 114 additions and 147 deletions

View File

@@ -1,7 +1,7 @@
import { AttachmentId, PieceId, RequestId, SessionId, Sha256, UserId } from "common";
import { ColumnType, CompiledQuery, CreateTableBuilder, Insertable, Kysely, Selectable, sql, Updateable } from "kysely";
import { BunSqliteDialect } from "kysely-bun-sqlite";
import { Database as BunSqliteDatabase } from "bun:sqlite";
import { AttachmentId, PieceId, RequestId, SessionId, Sha256, UserId } from "common";
import { ColumnType, CompiledQuery, CreateTableBuilder, Kysely } from "kysely";
import { BunSqliteDialect } from "kysely-bun-sqlite";
export function generateSessionId(byteLength: number = 32): SessionId {
const array = new Uint8Array(byteLength);
@@ -112,6 +112,13 @@ export async function initDatabase(filename: string = "db.sqlite3"): Promise<Kys
.column("timestamp")
.execute();
await db.schema
.createTable("File")
.ifNotExists()
.addColumn("sha256", "blob", (c) => c.notNull().primaryKey())
.addColumn("data", "blob", (c) => c.notNull())
.execute();
await db.schema
.createTable("User")
.ifNotExists()
@@ -152,11 +159,28 @@ export async function initDatabase(filename: string = "db.sqlite3"): Promise<Kys
.ifNotExists()
.addColumn("attachmentId", "text", (c) => c.notNull().primaryKey())
.addColumn("pieceId", "text", (c) => c.notNull().references("Piece.pieceId").onDelete("cascade").onUpdate("cascade"))
.addColumn("sha256", "blob", (c) => c.notNull())
.addColumn("sha256", "blob", (c) => c.notNull().references("File.sha256").onDelete("restrict").onUpdate("restrict"))
.addColumn("filename", "text", (c) => c.notNull())
.addColumn("mediaType", "text", (c) => c.notNull())
.$call(systemInformation)
.execute();
const { count } = await db
.selectFrom("User")
.select((eb) => eb.fn.countAll().as("count"))
.executeTakeFirstOrThrow();
if (BigInt(count) === 0n) {
const userId = UserId(Bun.randomUUIDv7());
const username = "admin";
const password = await Bun.password.hash("admin");
const admin = 1;
await db
.insertInto("User")
.values({ userId, username, password, admin })
.execute();
}
return db;
}