first
This commit is contained in:
commit
8538ab969a
434 changed files with 44051 additions and 0 deletions
18
packages/database/prisma/client.ts
Normal file
18
packages/database/prisma/client.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const prismaClientSingleton = () => {
|
||||
return new PrismaClient();
|
||||
};
|
||||
|
||||
declare global {
|
||||
var prisma: undefined | ReturnType<typeof prismaClientSingleton>;
|
||||
}
|
||||
|
||||
// biome-ignore lint/suspicious/noRedeclare: <explanation>
|
||||
const prisma = globalThis.prisma ?? prismaClientSingleton();
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
globalThis.prisma = prisma;
|
||||
}
|
||||
|
||||
export { prisma as db };
|
||||
7
packages/database/prisma/index.ts
Normal file
7
packages/database/prisma/index.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
export * from "./client";
|
||||
export * from "./zod";
|
||||
export * from "./queries";
|
||||
|
||||
declare global {
|
||||
namespace PrismaJson {}
|
||||
}
|
||||
92
packages/database/prisma/queries/ai-chats.ts
Normal file
92
packages/database/prisma/queries/ai-chats.ts
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
import { db } from "../client";
|
||||
import type { AiChat } from "../zod";
|
||||
|
||||
export async function getAiChatsByUserId({
|
||||
limit,
|
||||
offset,
|
||||
userId,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
userId: string;
|
||||
}) {
|
||||
return await db.aiChat.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
take: limit,
|
||||
skip: offset,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAiChatsByOrganizationId({
|
||||
limit,
|
||||
offset,
|
||||
organizationId,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
organizationId: string;
|
||||
}) {
|
||||
return await db.aiChat.findMany({
|
||||
where: {
|
||||
organizationId,
|
||||
},
|
||||
take: limit,
|
||||
skip: offset,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAiChatById(id: string) {
|
||||
return await db.aiChat.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createAiChat({
|
||||
organizationId,
|
||||
userId,
|
||||
title,
|
||||
}: {
|
||||
organizationId?: string;
|
||||
userId: string;
|
||||
title?: string;
|
||||
}) {
|
||||
return await db.aiChat.create({
|
||||
data: {
|
||||
organizationId,
|
||||
userId,
|
||||
title,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateAiChat({
|
||||
id,
|
||||
title,
|
||||
messages,
|
||||
}: {
|
||||
id: string;
|
||||
title?: string;
|
||||
messages?: AiChat["messages"];
|
||||
}) {
|
||||
return await db.aiChat.update({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
title,
|
||||
messages,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteAiChat(id: string) {
|
||||
return await db.aiChat.delete({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
4
packages/database/prisma/queries/index.ts
Normal file
4
packages/database/prisma/queries/index.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export * from "./ai-chats";
|
||||
export * from "./organizations";
|
||||
export * from "./purchases";
|
||||
export * from "./users";
|
||||
128
packages/database/prisma/queries/organizations.ts
Normal file
128
packages/database/prisma/queries/organizations.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
import type { z } from "zod";
|
||||
import { db } from "../client";
|
||||
import type { OrganizationUpdateInputSchema } from "../zod";
|
||||
|
||||
export async function getOrganizations({
|
||||
limit,
|
||||
offset,
|
||||
query,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
query?: string;
|
||||
}) {
|
||||
return db.organization
|
||||
.findMany({
|
||||
where: {
|
||||
name: { contains: query, mode: "insensitive" },
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
members: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
take: limit,
|
||||
skip: offset,
|
||||
})
|
||||
.then((res) =>
|
||||
res.map((org) => ({
|
||||
...org,
|
||||
membersCount: org._count.members,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
export async function countAllOrganizations() {
|
||||
return db.organization.count();
|
||||
}
|
||||
|
||||
export async function getOrganizationById(id: string) {
|
||||
return db.organization.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
members: true,
|
||||
invitations: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getInvitationById(id: string) {
|
||||
return db.invitation.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
organization: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOrganizationBySlug(slug: string) {
|
||||
return db.organization.findUnique({
|
||||
where: { slug },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOrganizationMembership(
|
||||
organizationId: string,
|
||||
userId: string,
|
||||
) {
|
||||
return db.member.findUnique({
|
||||
where: {
|
||||
organizationId_userId: {
|
||||
organizationId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
organization: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOrganizationWithPurchasesAndMembersCount(
|
||||
organizationId: string,
|
||||
) {
|
||||
const organization = await db.organization.findUnique({
|
||||
where: {
|
||||
id: organizationId,
|
||||
},
|
||||
include: {
|
||||
purchases: true,
|
||||
_count: {
|
||||
select: {
|
||||
members: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return organization
|
||||
? {
|
||||
...organization,
|
||||
membersCount: organization._count.members,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
export async function getPendingInvitationByEmail(email: string) {
|
||||
return db.invitation.findFirst({
|
||||
where: {
|
||||
email,
|
||||
status: "pending",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateOrganization(
|
||||
organization: z.infer<typeof OrganizationUpdateInputSchema> & {
|
||||
id: string;
|
||||
},
|
||||
) {
|
||||
return db.organization.update({
|
||||
where: {
|
||||
id: organization.id,
|
||||
},
|
||||
data: organization,
|
||||
});
|
||||
}
|
||||
69
packages/database/prisma/queries/purchases.ts
Normal file
69
packages/database/prisma/queries/purchases.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import type { z } from "zod";
|
||||
import { db } from "../client";
|
||||
import type {
|
||||
PurchaseUncheckedCreateInputSchema,
|
||||
PurchaseUncheckedUpdateInputSchema,
|
||||
} from "../zod";
|
||||
|
||||
export async function getPurchaseById(id: string) {
|
||||
return db.purchase.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPurchasesByOrganizationId(organizationId: string) {
|
||||
return db.purchase.findMany({
|
||||
where: {
|
||||
organizationId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPurchasesByUserId(userId: string) {
|
||||
return db.purchase.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPurchaseBySubscriptionId(subscriptionId: string) {
|
||||
return db.purchase.findFirst({
|
||||
where: {
|
||||
subscriptionId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createPurchase(
|
||||
purchase: z.infer<typeof PurchaseUncheckedCreateInputSchema>,
|
||||
) {
|
||||
const created = await db.purchase.create({
|
||||
data: purchase,
|
||||
});
|
||||
|
||||
return getPurchaseById(created.id);
|
||||
}
|
||||
|
||||
export async function updatePurchase(
|
||||
purchase: z.infer<typeof PurchaseUncheckedUpdateInputSchema> & {
|
||||
id: string;
|
||||
},
|
||||
) {
|
||||
const updated = await db.purchase.update({
|
||||
where: {
|
||||
id: purchase.id,
|
||||
},
|
||||
data: purchase,
|
||||
});
|
||||
|
||||
return getPurchaseById(updated.id);
|
||||
}
|
||||
|
||||
export async function deletePurchaseBySubscriptionId(subscriptionId: string) {
|
||||
await db.purchase.delete({
|
||||
where: {
|
||||
subscriptionId,
|
||||
},
|
||||
});
|
||||
}
|
||||
113
packages/database/prisma/queries/users.ts
Normal file
113
packages/database/prisma/queries/users.ts
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import type { z } from "zod";
|
||||
import { db } from "../client";
|
||||
import type { UserUncheckedUpdateInputSchema } from "../zod";
|
||||
|
||||
export async function getUsers({
|
||||
limit,
|
||||
offset,
|
||||
query,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
query?: string;
|
||||
}) {
|
||||
return await db.user.findMany({
|
||||
where: query
|
||||
? {
|
||||
name: {
|
||||
contains: query,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
take: limit,
|
||||
skip: offset,
|
||||
});
|
||||
}
|
||||
|
||||
export async function countAllUsers() {
|
||||
return await db.user.count();
|
||||
}
|
||||
|
||||
export async function getUserById(id: string) {
|
||||
return await db.user.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserByEmail(email: string) {
|
||||
return await db.user.findUnique({
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createUser({
|
||||
email,
|
||||
name,
|
||||
role,
|
||||
emailVerified,
|
||||
onboardingComplete,
|
||||
}: {
|
||||
email: string;
|
||||
name: string;
|
||||
role: "admin" | "user";
|
||||
emailVerified: boolean;
|
||||
onboardingComplete: boolean;
|
||||
}) {
|
||||
return await db.user.create({
|
||||
data: {
|
||||
email,
|
||||
name,
|
||||
role,
|
||||
emailVerified,
|
||||
onboardingComplete,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAccountById(id: string) {
|
||||
return await db.account.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createUserAccount({
|
||||
userId,
|
||||
providerId,
|
||||
accountId,
|
||||
hashedPassword,
|
||||
}: {
|
||||
userId: string;
|
||||
providerId: string;
|
||||
accountId: string;
|
||||
hashedPassword?: string;
|
||||
}) {
|
||||
return await db.account.create({
|
||||
data: {
|
||||
userId,
|
||||
accountId,
|
||||
providerId,
|
||||
password: hashedPassword,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateUser(
|
||||
user: z.infer<typeof UserUncheckedUpdateInputSchema> & { id: string },
|
||||
) {
|
||||
return await db.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
data: user,
|
||||
});
|
||||
}
|
||||
212
packages/database/prisma/schema.prisma
Normal file
212
packages/database/prisma/schema.prisma
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
generator zod {
|
||||
provider = "zod-prisma-types"
|
||||
output = "./zod"
|
||||
createInputTypes = true
|
||||
addIncludeType = false
|
||||
addSelectType = false
|
||||
}
|
||||
|
||||
generator json {
|
||||
provider = "prisma-json-types-generator"
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
email String
|
||||
emailVerified Boolean
|
||||
image String?
|
||||
createdAt DateTime
|
||||
updatedAt DateTime
|
||||
username String?
|
||||
role String?
|
||||
banned Boolean?
|
||||
banReason String?
|
||||
banExpires DateTime?
|
||||
onboardingComplete Boolean @default(false)
|
||||
paymentsCustomerId String?
|
||||
locale String?
|
||||
twoFactorEnabled Boolean?
|
||||
sessions Session[]
|
||||
accounts Account[]
|
||||
passkeys Passkey[]
|
||||
invitations Invitation[]
|
||||
purchases Purchase[]
|
||||
members Member[]
|
||||
twofactors TwoFactor[]
|
||||
aiChats AiChat[]
|
||||
|
||||
@@unique([email])
|
||||
@@unique([username])
|
||||
@@map("user")
|
||||
}
|
||||
|
||||
model Session {
|
||||
id String @id @default(cuid())
|
||||
expiresAt DateTime
|
||||
ipAddress String?
|
||||
userAgent String?
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
impersonatedBy String?
|
||||
|
||||
activeOrganizationId String?
|
||||
|
||||
token String
|
||||
createdAt DateTime
|
||||
updatedAt DateTime
|
||||
|
||||
@@unique([token])
|
||||
@@map("session")
|
||||
}
|
||||
|
||||
model Account {
|
||||
id String @id @default(cuid())
|
||||
accountId String
|
||||
providerId String
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
accessToken String? @db.Text
|
||||
refreshToken String? @db.Text
|
||||
idToken String? @db.Text
|
||||
expiresAt DateTime?
|
||||
password String?
|
||||
|
||||
accessTokenExpiresAt DateTime?
|
||||
refreshTokenExpiresAt DateTime?
|
||||
scope String?
|
||||
createdAt DateTime
|
||||
updatedAt DateTime
|
||||
|
||||
@@map("account")
|
||||
}
|
||||
|
||||
model Verification {
|
||||
id String @id @default(cuid())
|
||||
identifier String
|
||||
value String @db.Text
|
||||
expiresAt DateTime
|
||||
|
||||
createdAt DateTime?
|
||||
updatedAt DateTime?
|
||||
|
||||
@@map("verification")
|
||||
}
|
||||
|
||||
model Passkey {
|
||||
id String @id @default(cuid())
|
||||
name String?
|
||||
publicKey String
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
credentialID String
|
||||
counter Int
|
||||
deviceType String
|
||||
backedUp Boolean
|
||||
transports String?
|
||||
createdAt DateTime?
|
||||
|
||||
@@map("passkey")
|
||||
}
|
||||
|
||||
model TwoFactor {
|
||||
id String @id @default(cuid())
|
||||
secret String
|
||||
backupCodes String
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@map("twoFactor")
|
||||
}
|
||||
|
||||
model Organization {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
slug String?
|
||||
logo String?
|
||||
createdAt DateTime
|
||||
metadata String?
|
||||
paymentsCustomerId String?
|
||||
members Member[]
|
||||
invitations Invitation[]
|
||||
purchases Purchase[]
|
||||
aiChats AiChat[]
|
||||
|
||||
@@unique([slug])
|
||||
@@map("organization")
|
||||
}
|
||||
|
||||
model Member {
|
||||
id String @id @default(cuid())
|
||||
organizationId String
|
||||
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
role String
|
||||
createdAt DateTime
|
||||
|
||||
@@unique([organizationId, userId])
|
||||
@@map("member")
|
||||
}
|
||||
|
||||
model Invitation {
|
||||
id String @id @default(cuid())
|
||||
organizationId String
|
||||
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||
email String
|
||||
role String?
|
||||
status String
|
||||
expiresAt DateTime
|
||||
inviterId String
|
||||
user User @relation(fields: [inviterId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@map("invitation")
|
||||
}
|
||||
|
||||
enum PurchaseType {
|
||||
SUBSCRIPTION
|
||||
ONE_TIME
|
||||
}
|
||||
|
||||
model Purchase {
|
||||
id String @id @default(cuid())
|
||||
organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||
organizationId String?
|
||||
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
userId String?
|
||||
type PurchaseType
|
||||
customerId String
|
||||
subscriptionId String? @unique
|
||||
productId String
|
||||
status String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([subscriptionId])
|
||||
@@map("purchase")
|
||||
}
|
||||
|
||||
model AiChat {
|
||||
id String @id @default(cuid())
|
||||
organizationId String?
|
||||
organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
|
||||
userId String?
|
||||
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
title String?
|
||||
/// [Array<{role: "user" | "assistant"; content: string;}>]
|
||||
messages Json @default("[]") /// @zod.custom.use(z.array(z.object({ role: z.enum(['user', 'assistant']), content: z.string() })))
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@map("ai_chat")
|
||||
}
|
||||
7633
packages/database/prisma/zod/index.ts
Normal file
7633
packages/database/prisma/zod/index.ts
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue