From 541460cb32d217b7d107d270415ae79269e4d3e2 Mon Sep 17 00:00:00 2001 From: FadTheChad Date: Thu, 25 May 2023 02:39:50 +0500 Subject: [PATCH 1/6] Yeeted old goofy ah files --- packages/database/src/factoryReset.ts | 43 ------------------------ packages/database/src/getData.ts | 47 --------------------------- 2 files changed, 90 deletions(-) delete mode 100644 packages/database/src/factoryReset.ts delete mode 100644 packages/database/src/getData.ts diff --git a/packages/database/src/factoryReset.ts b/packages/database/src/factoryReset.ts deleted file mode 100644 index b5efb50..0000000 --- a/packages/database/src/factoryReset.ts +++ /dev/null @@ -1,43 +0,0 @@ -import db, { FactoryReset } from "../index.js" - -export const deleteAllUserGuildData = async (guildId: string): Promise => { - await db.userGuildData.deleteMany({ - where: { - guildId, - }, - }) -} - -export const deleteAllGuildSettings = async (guildId: string): Promise => { - await db.guild.delete({ - where: { - id: guildId, - }, - }) - - await db.guild.create({ - data: { - id: guildId, - }, - }) -} - -export const markFactoryReset = async (guildId: string, executorId: string): Promise => { - const reset = await db.factoryReset.create({ - data: { - guildId, - executorId, - resetAt: new Date(), - }, - }) - return reset -} - -export const getFactoryResets = async (guildId: string): Promise => { - const resets = await db.factoryReset.findMany({ - where: { - guildId, - }, - }) - return resets -} diff --git a/packages/database/src/getData.ts b/packages/database/src/getData.ts deleted file mode 100644 index 5ae47c8..0000000 --- a/packages/database/src/getData.ts +++ /dev/null @@ -1,47 +0,0 @@ -import db, { Prisma, UserGuildData } from "../index.js" - -export const getGuildData = async (guildId: string, include: Prisma.GuildInclude = { premiumGuildSlots: true }) => { - if (!guildId) throw new Error("No guild ID provided") - const guildData = await db.guild.upsert({ - where: { - id: guildId, - }, - update: {}, - create: { - id: guildId, - }, - include, - }) - return guildData -} - -export const getUserData = async (userId: string, include: Prisma.UserInclude = { premiumGuildSlots: true }) => { - const userData = await db.user.upsert({ - where: { - id: userId, - }, - update: {}, - create: { - id: userId, - }, - include, - }) - return userData -} - -export const getUserGuildData = async (userId: string, guildId: string): Promise => { - const userGuildData = await db.userGuildData.upsert({ - where: { - userId_guildId: { - userId, - guildId, - }, - }, - update: {}, - create: { - userId, - guildId, - }, - }) - return userGuildData -} From 735fecaf01daccde17f024233c6ce32938cbce6c Mon Sep 17 00:00:00 2001 From: FadTheChad Date: Thu, 25 May 2023 02:40:04 +0500 Subject: [PATCH 2/6] Added everything in one db class --- packages/database/src/db.ts | 111 ++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 packages/database/src/db.ts diff --git a/packages/database/src/db.ts b/packages/database/src/db.ts new file mode 100644 index 0000000..eddb351 --- /dev/null +++ b/packages/database/src/db.ts @@ -0,0 +1,111 @@ +import { FactoryReset, Guild, PremiumGuildSlot, User, PrismaClient, Prisma, UserGuildData } from "../../../node_modules/.prisma/client" + +export default class Database { + constructor(public db: PrismaClient) {} + + public async deleteAllUserGuildData(guildId: string): Promise { + await this.db.userGuildData.deleteMany({ + where: { + guildId, + }, + }) + } + + public async deleteAllGuildSettings(guildId: string): Promise { + await this.db.guild.delete({ + where: { + id: guildId, + }, + }) + + await this.db.guild.create({ + data: { + id: guildId, + }, + }) + } + + public async markFactoryReset(guildId: string, executorId: string): Promise { + const reset = await this.db.factoryReset.create({ + data: { + guildId, + executorId, + resetAt: new Date(), + }, + }) + return reset + } + + public async getFactoryResets(guildId: string): Promise { + return await this.db.factoryReset.findMany({ + where: { + guildId, + }, + }) + } + + public async getGuildData( + guildId: string, + include: Prisma.GuildInclude = { premiumGuildSlots: true } + ): Promise< + Guild & { + userData?: UserGuildData[] | undefined + premiumGuildSlots?: PremiumGuildSlot[] | undefined + factoryResets?: FactoryReset[] | undefined + _count?: Prisma.GuildCountOutputType | undefined + } + > { + if (!guildId) throw new Error("No guild ID provided") + const guildData = await this.db.guild.upsert({ + where: { + id: guildId, + }, + update: {}, + create: { + id: guildId, + }, + include, + }) + return guildData + } + + public async getUserData( + userId: string, + include: Prisma.UserInclude = { premiumGuildSlots: true } + ): Promise< + User & { + premiumGuildSlots?: PremiumGuildSlot[] | undefined + guildData?: UserGuildData[] | undefined + _count?: Prisma.UserCountOutputType | undefined + } + > { + const userData = await this.db.user.upsert({ + where: { + id: userId, + }, + update: {}, + create: { + id: userId, + }, + include, + }) + return userData + } + + public async getUserGuildData(userId: string, guildId: string): Promise { + const userGuildData = await this.db.userGuildData.upsert({ + where: { + userId_guildId: { + userId, + guildId, + }, + }, + update: {}, + create: { + userId, + guildId, + }, + }) + return userGuildData + } +} From 6db2e7a578fe71cabc7144f589bf79ce2b4018f8 Mon Sep 17 00:00:00 2001 From: FadTheChad Date: Thu, 25 May 2023 02:40:21 +0500 Subject: [PATCH 3/6] fixed cringe imports --- packages/database/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/database/index.ts b/packages/database/index.ts index 40d2a7a..f714f6c 100644 --- a/packages/database/index.ts +++ b/packages/database/index.ts @@ -1,13 +1,13 @@ import { PrismaClient } from "@prisma/client" +import Database from "./src/db" -const database = new PrismaClient() +const prismaDb = new PrismaClient() -export default database +export default prismaDb export * from "@prisma/client" -export * from "./src/getData.js" -export * from "./src/factoryReset.js" +export const database = new Database(prismaDb) export enum ApiPermission { Levels = 1 << 0, From 8d5805e66535053d3e35b7e5065a119a9b3e0055 Mon Sep 17 00:00:00 2001 From: FadTheChad Date: Fri, 26 May 2023 20:22:42 +0500 Subject: [PATCH 4/6] Seperated code in files --- packages/database/src/modules/Guild.ts | 42 +++++++++++++++++++++++++ packages/database/src/modules/Member.ts | 27 ++++++++++++++++ packages/database/src/modules/User.ts | 26 +++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 packages/database/src/modules/Guild.ts create mode 100644 packages/database/src/modules/Member.ts create mode 100644 packages/database/src/modules/User.ts diff --git a/packages/database/src/modules/Guild.ts b/packages/database/src/modules/Guild.ts new file mode 100644 index 0000000..b1fa1b2 --- /dev/null +++ b/packages/database/src/modules/Guild.ts @@ -0,0 +1,42 @@ +import Database from "../db" +import { Prisma, UserGuildData, FactoryReset, PremiumGuildSlot, Guild } from "../../../../node_modules/.prisma/client" + +export async function getGuildData( + this: Database, + guildId: string, + include: Prisma.GuildInclude = { premiumGuildSlots: true } +): Promise< + Guild & { + userData?: UserGuildData[] | undefined + premiumGuildSlots?: PremiumGuildSlot[] | undefined + factoryResets?: FactoryReset[] | undefined + _count?: Prisma.GuildCountOutputType | undefined + } +> { + if (!guildId) throw new Error("No guild ID provided") + const guildData = await this.db.guild.upsert({ + where: { + id: guildId, + }, + update: {}, + create: { + id: guildId, + }, + include, + }) + return guildData +} + +export async function deleteAllGuildSettings(this: Database, guildId: string): Promise { + await this.db.guild.delete({ + where: { + id: guildId, + }, + }) + + await this.db.guild.create({ + data: { + id: guildId, + }, + }) +} diff --git a/packages/database/src/modules/Member.ts b/packages/database/src/modules/Member.ts new file mode 100644 index 0000000..623eb5f --- /dev/null +++ b/packages/database/src/modules/Member.ts @@ -0,0 +1,27 @@ +import Database from "../db" +import { UserGuildData } from "../../../../node_modules/.prisma/client" + +export async function deleteAllUserGuildData(this: Database, guildId: string): Promise { + await this.db.userGuildData.deleteMany({ + where: { + guildId, + }, + }) +} + +export async function getUserGuildData(this: Database, userId: string, guildId: string): Promise { + const userGuildData = await this.db.userGuildData.upsert({ + where: { + userId_guildId: { + userId, + guildId, + }, + }, + update: {}, + create: { + userId, + guildId, + }, + }) + return userGuildData +} diff --git a/packages/database/src/modules/User.ts b/packages/database/src/modules/User.ts new file mode 100644 index 0000000..5f93dbc --- /dev/null +++ b/packages/database/src/modules/User.ts @@ -0,0 +1,26 @@ +import { UserGuildData, PremiumGuildSlot, Prisma, User } from "../../../../node_modules/.prisma/client" +import Database from "../db" + +export async function getUserData( + this: Database, + userId: string, + include: Prisma.UserInclude = { premiumGuildSlots: true } +): Promise< + User & { + premiumGuildSlots?: PremiumGuildSlot[] | undefined + guildData?: UserGuildData[] | undefined + _count?: Prisma.UserCountOutputType | undefined + } +> { + const userData = await this.db.user.upsert({ + where: { + id: userId, + }, + update: {}, + create: { + id: userId, + }, + include, + }) + return userData +} From cb388983462ce670e645f8896f2a9a00d2b8b243 Mon Sep 17 00:00:00 2001 From: FadTheChad Date: Fri, 26 May 2023 20:22:55 +0500 Subject: [PATCH 5/6] Factory Resets have their own file --- packages/database/src/modules/FactoryReset.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/database/src/modules/FactoryReset.ts diff --git a/packages/database/src/modules/FactoryReset.ts b/packages/database/src/modules/FactoryReset.ts new file mode 100644 index 0000000..29ab311 --- /dev/null +++ b/packages/database/src/modules/FactoryReset.ts @@ -0,0 +1,21 @@ +import { FactoryReset } from "../../../../node_modules/.prisma/client" +import Database from "../db" + +export async function markFactoryReset(this: Database, guildId: string, executorId: string): Promise { + const reset = await this.db.factoryReset.create({ + data: { + guildId, + executorId, + resetAt: new Date(), + }, + }) + return reset +} + +export async function getFactoryResets(this: Database, guildId: string): Promise { + return await this.db.factoryReset.findMany({ + where: { + guildId, + }, + }) +} From d052d34782bdc480396be6568b6df2460d4b29be Mon Sep 17 00:00:00 2001 From: FadTheChad Date: Fri, 26 May 2023 20:23:03 +0500 Subject: [PATCH 6/6] C L E A N --- packages/database/src/db.ts | 120 +++++------------------------------- 1 file changed, 17 insertions(+), 103 deletions(-) diff --git a/packages/database/src/db.ts b/packages/database/src/db.ts index eddb351..069890f 100644 --- a/packages/database/src/db.ts +++ b/packages/database/src/db.ts @@ -1,111 +1,25 @@ -import { FactoryReset, Guild, PremiumGuildSlot, User, PrismaClient, Prisma, UserGuildData } from "../../../node_modules/.prisma/client" +import { PrismaClient } from "../../../node_modules/.prisma/client" + +import { getFactoryResets, markFactoryReset } from "./modules/FactoryReset" +import { deleteAllGuildSettings, getGuildData } from "./modules/Guild" +import { deleteAllUserGuildData, getUserGuildData } from "./modules/Member" +import { getUserData } from "./modules/User" export default class Database { constructor(public db: PrismaClient) {} - public async deleteAllUserGuildData(guildId: string): Promise { - await this.db.userGuildData.deleteMany({ - where: { - guildId, - }, - }) - } - - public async deleteAllGuildSettings(guildId: string): Promise { - await this.db.guild.delete({ - where: { - id: guildId, - }, - }) - - await this.db.guild.create({ - data: { - id: guildId, - }, - }) - } - - public async markFactoryReset(guildId: string, executorId: string): Promise { - const reset = await this.db.factoryReset.create({ - data: { - guildId, - executorId, - resetAt: new Date(), - }, - }) - return reset - } - - public async getFactoryResets(guildId: string): Promise { - return await this.db.factoryReset.findMany({ - where: { - guildId, - }, - }) - } + // MEMBER + public getUserGuildData = getUserGuildData + public deleteAllUserGuildData = deleteAllUserGuildData - public async getGuildData( - guildId: string, - include: Prisma.GuildInclude = { premiumGuildSlots: true } - ): Promise< - Guild & { - userData?: UserGuildData[] | undefined - premiumGuildSlots?: PremiumGuildSlot[] | undefined - factoryResets?: FactoryReset[] | undefined - _count?: Prisma.GuildCountOutputType | undefined - } - > { - if (!guildId) throw new Error("No guild ID provided") - const guildData = await this.db.guild.upsert({ - where: { - id: guildId, - }, - update: {}, - create: { - id: guildId, - }, - include, - }) - return guildData - } + // GUILD + public getGuildData = getGuildData + public deleteAllGuildSettings = deleteAllGuildSettings - public async getUserData( - userId: string, - include: Prisma.UserInclude = { premiumGuildSlots: true } - ): Promise< - User & { - premiumGuildSlots?: PremiumGuildSlot[] | undefined - guildData?: UserGuildData[] | undefined - _count?: Prisma.UserCountOutputType | undefined - } - > { - const userData = await this.db.user.upsert({ - where: { - id: userId, - }, - update: {}, - create: { - id: userId, - }, - include, - }) - return userData - } + // USER + public getUserData = getUserData - public async getUserGuildData(userId: string, guildId: string): Promise { - const userGuildData = await this.db.userGuildData.upsert({ - where: { - userId_guildId: { - userId, - guildId, - }, - }, - update: {}, - create: { - userId, - guildId, - }, - }) - return userGuildData - } + // FACTORY RESET + public getFactoryResets = getFactoryResets + public markFactoryReset = markFactoryReset }