WorkshopTasker/server/api/firstRun.post.ts
Wroclaw 10ff342991
All checks were successful
Build dev / build (push) Successful in 1m2s
api/firstRun.post: properly initialize database using @prisma/migrate
instead of executing the command,
which was not available in the build
now we use the proper library to initialize the database.
2023-11-09 23:52:56 +01:00

52 lines
1.6 KiB
TypeScript

import url from "node:url";
import path from "node:path";
import { defineEventHandler, setResponseStatus, readBody } from "h3";
// @ts-expect-error
import { DbPush } from "@prisma/migrate";
import { database } from "../utils/database";
import { isFirstRun } from "./firstRun.get";
import { getPasswordHash } from "./login.post";
import Snowflake from "~/utils/snowflake";
import { createError } from "#imports";
export default defineEventHandler(async (e) => {
if (!isFirstRun()) {
setResponseStatus(e, 404);
return null;
}
const body = await readBody(e);
if (typeof body !== "object") throw createError({ message: "Invalid body", statusCode: 400 });
const username = body.username;
if (typeof username !== "string") throw createError({ message: "username is not string", statusCode: 400 });
const password = body.password;
if (typeof password !== "string") throw createError({ message: "password is not string", statusCode: 400 });
const email = body.email;
if (typeof email !== "string") throw createError({ message: "email is not string", statusCode: 400 });
const dbPushParam = [
"--force-reset",
"--skip-generate",
];
if (!import.meta.dev ?? true) {
const mainPath = path.dirname(url.fileURLToPath(import.meta.url));
dbPushParam.push(
"--schema",
`${mainPath}/node_modules/.prisma/client/schema.prisma`,
);
}
await DbPush.new().parse(dbPushParam);
await database.user.create({
data: {
id: new Snowflake().state,
username,
email,
password: getPasswordHash(password),
},
});
return null;
});