Skip to content

New Database Class #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/database/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
25 changes: 25 additions & 0 deletions packages/database/src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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) {}

// MEMBER
public getUserGuildData = getUserGuildData
public deleteAllUserGuildData = deleteAllUserGuildData

// GUILD
public getGuildData = getGuildData
public deleteAllGuildSettings = deleteAllGuildSettings

// USER
public getUserData = getUserData

// FACTORY RESET
public getFactoryResets = getFactoryResets
public markFactoryReset = markFactoryReset
}
43 changes: 0 additions & 43 deletions packages/database/src/factoryReset.ts

This file was deleted.

47 changes: 0 additions & 47 deletions packages/database/src/getData.ts

This file was deleted.

21 changes: 21 additions & 0 deletions packages/database/src/modules/FactoryReset.ts
Original file line number Diff line number Diff line change
@@ -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<FactoryReset> {
const reset = await this.db.factoryReset.create({
data: {
guildId,
executorId,
resetAt: new Date(),
},
})
return reset
}

export async function getFactoryResets(this: Database, guildId: string): Promise<FactoryReset[]> {
return await this.db.factoryReset.findMany({
where: {
guildId,
},
})
}
42 changes: 42 additions & 0 deletions packages/database/src/modules/Guild.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await this.db.guild.delete({
where: {
id: guildId,
},
})

await this.db.guild.create({
data: {
id: guildId,
},
})
}
27 changes: 27 additions & 0 deletions packages/database/src/modules/Member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Database from "../db"
import { UserGuildData } from "../../../../node_modules/.prisma/client"

export async function deleteAllUserGuildData(this: Database, guildId: string): Promise<void> {
await this.db.userGuildData.deleteMany({
where: {
guildId,
},
})
}

export async function getUserGuildData(this: Database, userId: string, guildId: string): Promise<UserGuildData> {
const userGuildData = await this.db.userGuildData.upsert({
where: {
userId_guildId: {
userId,
guildId,
},
},
update: {},
create: {
userId,
guildId,
},
})
return userGuildData
}
26 changes: 26 additions & 0 deletions packages/database/src/modules/User.ts
Original file line number Diff line number Diff line change
@@ -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
}