From cc3a7a7b4313ce2851c037b1b387ab451d5e3893 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Thu, 7 Sep 2023 19:14:01 -0400 Subject: [PATCH] User data functions --- src/data/prisma.ts | 3 +++ src/data/user.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/data/user.ts diff --git a/src/data/prisma.ts b/src/data/prisma.ts index e69de29..901f3a0 100644 --- a/src/data/prisma.ts +++ b/src/data/prisma.ts @@ -0,0 +1,3 @@ +import { PrismaClient } from "@prisma/client"; + +export const prisma = new PrismaClient(); diff --git a/src/data/user.ts b/src/data/user.ts new file mode 100644 index 0000000..5da351b --- /dev/null +++ b/src/data/user.ts @@ -0,0 +1,52 @@ +import { User } from "@prisma/client"; +import { prisma } from "./prisma"; + +export interface UserFlags { + freeze_name: number; + "no chat rate limit": number; + chat_curse_1: number; + chat_curse_2: number; + override_id: string; +} + +export async function createUser( + _id: string, + name?: string, + color?: string, + flags?: UserFlags +) { + return await prisma.user.create({ + data: { id: _id, name, color, flags: JSON.stringify(flags) } + }); +} + +export async function deleteUser(_id: string) { + return await prisma.user.delete({ + where: { id: _id } + }); +} + +export async function readUser(_id: string) { + const data = await prisma.user.findUnique({ + where: { id: _id } + }); + + if (!data) return null; + + return { + _id: data.id, + name: data.name, + color: data.color, + flags: data.flags + }; +} + +export async function updateUser( + _id: string, + data: Omit & { _id: string } +) { + return await prisma.user.update({ + where: { id: _id }, + data + }); +}