bppapsdf
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { GameModel, PlayerModel, PoemModel } from "generated/prisma/models";
|
||||
import { prisma } from "../../lib/prisma";
|
||||
import { ObjName } from "./types";
|
||||
|
||||
export async function create<InputType, OutputType>(
|
||||
objName: ObjName,
|
||||
data: InputType,
|
||||
) {
|
||||
try {
|
||||
let newObj;
|
||||
switch (objName) {
|
||||
case "game":
|
||||
if (Array.isArray(data)) {
|
||||
newObj = await prisma.game.createMany({
|
||||
data: data as GameModel[],
|
||||
});
|
||||
} else {
|
||||
newObj = await prisma.game.create({
|
||||
data: data as GameModel,
|
||||
include: {
|
||||
players: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
break;
|
||||
case "player":
|
||||
if (Array.isArray(data)) {
|
||||
newObj = await prisma.player.createMany({
|
||||
data: data as PlayerModel[],
|
||||
});
|
||||
} else {
|
||||
newObj = await prisma.player.create({
|
||||
data: data as PlayerModel,
|
||||
});
|
||||
}
|
||||
break;
|
||||
case "poem":
|
||||
if (Array.isArray(data)) {
|
||||
newObj = await prisma.poem.createMany({
|
||||
data: (data as PoemModel[]).map((d) => ({
|
||||
...d,
|
||||
lines: d.lines!,
|
||||
})),
|
||||
});
|
||||
} else {
|
||||
newObj = await prisma.poem.create({
|
||||
data: { ...(data as PoemModel), lines: (data as PoemModel).lines! },
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
return newObj as OutputType;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user