Replace mysql2 with prisma
also I updated packages, and properly typed api input a lot of time was spent, I don't remeber what really I did x3 but everything was related to replacing mysql2 with prisma
This commit is contained in:
parent
be1e3909b6
commit
eebf25198d
39 changed files with 1081 additions and 1292 deletions
|
@ -1,7 +1,10 @@
|
|||
import { defineEventHandler, readBody } from "h3";
|
||||
import { defineEventHandler, readBody, setResponseStatus } from "h3";
|
||||
import { type Client } from "@prisma/client";
|
||||
|
||||
import { baaWrapper } from "./clients.get";
|
||||
import { type client } from "~/utils/types/database";
|
||||
import getRequestingUser from "../utils/getRequestingUser";
|
||||
import { database } from "../utils/database";
|
||||
import { prismaToWeb } from "~/server/utils/prismaToWeb";
|
||||
import Snowflake from "~/utils/snowflake";
|
||||
|
||||
import { createError } from "#imports";
|
||||
|
||||
|
@ -12,10 +15,10 @@ const clientKeys: Array<string> = [
|
|||
"email",
|
||||
];
|
||||
|
||||
export function checkIsClient(
|
||||
export function checkIsClient<Patch extends boolean = boolean>(
|
||||
value: any,
|
||||
required = false,
|
||||
): value is Partial<Omit<client, "id">> {
|
||||
patch: Patch,
|
||||
): value is Patch extends true ? Partial<Omit<Client, "id">> : Omit<Client, "id"> {
|
||||
const errors = new Map<string, string>();
|
||||
|
||||
if (typeof value !== "object") {
|
||||
|
@ -25,12 +28,12 @@ export function checkIsClient(
|
|||
});
|
||||
}
|
||||
|
||||
if (!(typeof value.name === "string" || value.name === null || (!required && value.name === undefined))) errors.set("name", "is not string or null");
|
||||
if (!(typeof value.address === "string" || value.address === null || (!required && value.address === undefined))) errors.set("address", "is not string or null");
|
||||
if (!(typeof value.phone === "string" || value.phone === null || (!required && value.phone === undefined))) errors.set("phone", "is not string or null");
|
||||
if (!(typeof value.email === "string" || value.email === null || (!required && value.email === undefined))) errors.set("email", "is not string or null");
|
||||
if (!(typeof value.name === "string" || value.name === null || (!patch && value.name === undefined))) errors.set("name", "is not string or null");
|
||||
if (!(typeof value.address === "string" || value.address === null || (!patch && value.address === undefined))) errors.set("address", "is not string or null");
|
||||
if (!(typeof value.phone === "string" || value.phone === null || (!patch && value.phone === undefined))) errors.set("phone", "is not string or null");
|
||||
if (!(typeof value.email === "string" || value.email === null || (!patch && value.email === undefined))) errors.set("email", "is not string or null");
|
||||
|
||||
for (const i in value as Partial<Omit<client, "id">>)
|
||||
for (const i in value as Partial<Omit<Client, "id">>)
|
||||
if (!clientKeys.includes(i)) errors.set(i, `excessive property`);
|
||||
|
||||
if (errors.size !== 0) {
|
||||
|
@ -50,6 +53,20 @@ export function checkIsClient(
|
|||
return true;
|
||||
}
|
||||
|
||||
export default defineEventHandler((e) => {
|
||||
return baaWrapper.RESTpost(e, clientKeys as Array<keyof Omit<client, "id">>, (o): o is Omit<client, "id"> => checkIsClient(o, true));
|
||||
export default defineEventHandler(async (e) => {
|
||||
const body = await readBody(e);
|
||||
const id = new Snowflake().state;
|
||||
const user = await getRequestingUser(e);
|
||||
|
||||
if (!checkIsClient(body, false)) throw createError({ message: "Invalid body", statusCode: 400 });
|
||||
|
||||
const rvalue = await database.client.create({
|
||||
data: {
|
||||
...body,
|
||||
id,
|
||||
},
|
||||
});
|
||||
|
||||
setResponseStatus(e, 201);
|
||||
return prismaToWeb(rvalue);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue