forked from Wroclaw/WorkshopTasker
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.
This commit is contained in:
parent
1d8220d92c
commit
10ff342991
3 changed files with 2694 additions and 293 deletions
2966
package-lock.json
generated
2966
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -24,6 +24,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "5.5.2",
|
"@prisma/client": "5.5.2",
|
||||||
|
"@prisma/migrate": "^5.5.2",
|
||||||
"mysql2": "^3.2.3"
|
"mysql2": "^3.2.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
import { execSync } from "node:child_process";
|
import url from "node:url";
|
||||||
|
import path from "node:path";
|
||||||
import { defineEventHandler, setResponseStatus, readBody } from "h3";
|
import { defineEventHandler, setResponseStatus, readBody } from "h3";
|
||||||
|
// @ts-expect-error
|
||||||
|
import { DbPush } from "@prisma/migrate";
|
||||||
|
|
||||||
import { database } from "../utils/database";
|
import { database } from "../utils/database";
|
||||||
import { isFirstRun } from "./firstRun.get";
|
import { isFirstRun } from "./firstRun.get";
|
||||||
|
@ -23,7 +26,20 @@ export default defineEventHandler(async (e) => {
|
||||||
const email = body.email;
|
const email = body.email;
|
||||||
if (typeof email !== "string") throw createError({ message: "email is not string", statusCode: 400 });
|
if (typeof email !== "string") throw createError({ message: "email is not string", statusCode: 400 });
|
||||||
|
|
||||||
execSync("npx prisma db push --force-reset");
|
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({
|
await database.user.create({
|
||||||
data: {
|
data: {
|
||||||
id: new Snowflake().state,
|
id: new Snowflake().state,
|
||||||
|
|
Loading…
Reference in a new issue