first
This commit is contained in:
commit
8538ab969a
434 changed files with 44051 additions and 0 deletions
88
packages/database/drizzle/queries/ai-chats.ts
Normal file
88
packages/database/drizzle/queries/ai-chats.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../client";
|
||||
import { aiChat } from "../schema/postgres";
|
||||
|
||||
export async function getAiChatsByUserId({
|
||||
limit,
|
||||
offset,
|
||||
userId,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
userId: string;
|
||||
}) {
|
||||
return await db.query.aiChat.findMany({
|
||||
where: (aiChat, { eq }) => eq(aiChat.userId, userId),
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAiChatsByOrganizationId({
|
||||
limit,
|
||||
offset,
|
||||
organizationId,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
organizationId: string;
|
||||
}) {
|
||||
return await db.query.aiChat.findMany({
|
||||
where: (aiChat, { eq }) => eq(aiChat.organizationId, organizationId),
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAiChatById(id: string) {
|
||||
return db.query.aiChat.findFirst({
|
||||
where: (aiChat, { eq }) => eq(aiChat.id, id),
|
||||
});
|
||||
}
|
||||
|
||||
export async function createAiChat({
|
||||
organizationId,
|
||||
userId,
|
||||
title,
|
||||
}: {
|
||||
organizationId?: string;
|
||||
userId: string;
|
||||
title?: string;
|
||||
}) {
|
||||
const [{ id }] = await db
|
||||
.insert(aiChat)
|
||||
.values({
|
||||
organizationId,
|
||||
userId,
|
||||
title,
|
||||
})
|
||||
.returning({ id: aiChat.id });
|
||||
|
||||
const createdChat = await getAiChatById(id);
|
||||
|
||||
if (!createdChat) {
|
||||
throw new Error("Failed to create chat");
|
||||
}
|
||||
|
||||
return createdChat;
|
||||
}
|
||||
|
||||
export async function updateAiChat({
|
||||
id,
|
||||
title,
|
||||
messages,
|
||||
}: {
|
||||
id: string;
|
||||
title?: string;
|
||||
messages?: typeof aiChat.$inferInsert.messages;
|
||||
}) {
|
||||
return await db
|
||||
.update(aiChat)
|
||||
.set({ title, messages })
|
||||
.where(eq(aiChat.id, id))
|
||||
.returning();
|
||||
}
|
||||
|
||||
export async function deleteAiChat(id: string) {
|
||||
return await db.delete(aiChat).where(eq(aiChat.id, id));
|
||||
}
|
||||
4
packages/database/drizzle/queries/index.ts
Normal file
4
packages/database/drizzle/queries/index.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export * from "./organizations";
|
||||
export * from "./users";
|
||||
export * from "./ai-chats";
|
||||
export * from "./purchases";
|
||||
105
packages/database/drizzle/queries/organizations.ts
Normal file
105
packages/database/drizzle/queries/organizations.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import { and, eq } from "drizzle-orm";
|
||||
import type { z } from "zod";
|
||||
import { db } from "../client";
|
||||
import { member, organization } from "../schema/postgres";
|
||||
import type { OrganizationUpdateSchema } from "../zod";
|
||||
|
||||
export async function getOrganizations({
|
||||
limit,
|
||||
offset,
|
||||
query,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
query?: string;
|
||||
}) {
|
||||
return db.query.organization.findMany({
|
||||
where: query
|
||||
? (org, { like }) => like(org.name, `%${query}%`)
|
||||
: undefined,
|
||||
limit,
|
||||
offset,
|
||||
extras: {
|
||||
membersCount: db
|
||||
.$count(member, eq(member.organizationId, organization.id))
|
||||
.as("membersCount"),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function countAllOrganizations() {
|
||||
return db.$count(organization);
|
||||
}
|
||||
|
||||
export async function getOrganizationById(id: string) {
|
||||
return db.query.organization.findFirst({
|
||||
where: (org, { eq }) => eq(org.id, id),
|
||||
with: {
|
||||
members: true,
|
||||
invitations: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getInvitationById(id: string) {
|
||||
return db.query.invitation.findFirst({
|
||||
where: (invitation, { eq }) => eq(invitation.id, id),
|
||||
with: {
|
||||
organization: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOrganizationBySlug(slug: string) {
|
||||
return db.query.organization.findFirst({
|
||||
where: (org, { eq }) => eq(org.slug, slug),
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOrganizationMembership(
|
||||
organizationId: string,
|
||||
userId: string,
|
||||
) {
|
||||
return db.query.member.findFirst({
|
||||
where: (member, { eq }) =>
|
||||
and(
|
||||
eq(member.organizationId, organizationId),
|
||||
eq(member.userId, userId),
|
||||
),
|
||||
with: {
|
||||
organization: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getOrganizationWithPurchasesAndMembersCount(
|
||||
organizationId: string,
|
||||
) {
|
||||
return db.query.organization.findFirst({
|
||||
where: (org, { eq }) => eq(org.id, organizationId),
|
||||
with: {
|
||||
purchases: true,
|
||||
},
|
||||
extras: {
|
||||
membersCount: db
|
||||
.$count(member, eq(member.organizationId, organization.id))
|
||||
.as("membersCount"),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPendingInvitationByEmail(email: string) {
|
||||
return db.query.invitation.findFirst({
|
||||
where: (invitation, { eq }) =>
|
||||
and(eq(invitation.email, email), eq(invitation.status, "pending")),
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateOrganization(
|
||||
updatedOrganization: z.infer<typeof OrganizationUpdateSchema>,
|
||||
) {
|
||||
return db
|
||||
.update(organization)
|
||||
.set(updatedOrganization)
|
||||
.where(eq(organization.id, updatedOrganization.id));
|
||||
}
|
||||
59
packages/database/drizzle/queries/purchases.ts
Normal file
59
packages/database/drizzle/queries/purchases.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { eq } from "drizzle-orm";
|
||||
import type { z } from "zod";
|
||||
import { db } from "../client";
|
||||
import { purchase } from "../schema/postgres";
|
||||
import type { PurchaseInsertSchema, PurchaseUpdateSchema } from "../zod";
|
||||
|
||||
export async function getPurchasesByOrganizationId(organizationId: string) {
|
||||
return db.query.purchase.findMany({
|
||||
where: (purchase, { eq }) =>
|
||||
eq(purchase.organizationId, organizationId),
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPurchasesByUserId(userId: string) {
|
||||
return db.query.purchase.findMany({
|
||||
where: (purchase, { eq }) => eq(purchase.userId, userId),
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPurchaseById(id: string) {
|
||||
return db.query.purchase.findFirst({
|
||||
where: (purchase, { eq }) => eq(purchase.id, id),
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPurchaseBySubscriptionId(subscriptionId: string) {
|
||||
return db.query.purchase.findFirst({
|
||||
where: (purchase, { eq }) =>
|
||||
eq(purchase.subscriptionId, subscriptionId),
|
||||
});
|
||||
}
|
||||
|
||||
export async function createPurchase(
|
||||
insertedPurchase: z.infer<typeof PurchaseInsertSchema>,
|
||||
) {
|
||||
const [{ id }] = await db
|
||||
.insert(purchase)
|
||||
.values(insertedPurchase)
|
||||
.returning({ id: purchase.id });
|
||||
|
||||
return getPurchaseById(id);
|
||||
}
|
||||
|
||||
export async function updatePurchase(
|
||||
updatedPurchase: z.infer<typeof PurchaseUpdateSchema>,
|
||||
) {
|
||||
const [{ id }] = await db
|
||||
.update(purchase)
|
||||
.set(updatedPurchase)
|
||||
.returning({ id: purchase.id });
|
||||
|
||||
return getPurchaseById(id);
|
||||
}
|
||||
|
||||
export async function deletePurchaseBySubscriptionId(subscriptionId: string) {
|
||||
await db
|
||||
.delete(purchase)
|
||||
.where(eq(purchase.subscriptionId, subscriptionId));
|
||||
}
|
||||
112
packages/database/drizzle/queries/users.ts
Normal file
112
packages/database/drizzle/queries/users.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
import { eq } from "drizzle-orm";
|
||||
import type { z } from "zod";
|
||||
import { db } from "../client";
|
||||
import { account, user } from "../schema/postgres";
|
||||
import type { UserUpdateSchema } from "../zod";
|
||||
|
||||
export async function getUsers({
|
||||
limit,
|
||||
offset,
|
||||
query,
|
||||
}: {
|
||||
limit: number;
|
||||
offset: number;
|
||||
query?: string;
|
||||
}) {
|
||||
return await db.query.user.findMany({
|
||||
where: (user, { like }) => like(user.name, `%${query}%`),
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
}
|
||||
|
||||
export async function countAllUsers() {
|
||||
return db.$count(user);
|
||||
}
|
||||
|
||||
export async function getUserById(id: string) {
|
||||
return await db.query.user.findFirst({
|
||||
where: (user, { eq }) => eq(user.id, id),
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserByEmail(email: string) {
|
||||
return await db.query.user.findFirst({
|
||||
where: (user, { eq }) => eq(user.email, email),
|
||||
});
|
||||
}
|
||||
|
||||
export async function createUser({
|
||||
email,
|
||||
name,
|
||||
role,
|
||||
emailVerified,
|
||||
onboardingComplete,
|
||||
}: {
|
||||
email: string;
|
||||
name: string;
|
||||
role: "admin" | "user";
|
||||
emailVerified: boolean;
|
||||
onboardingComplete: boolean;
|
||||
}) {
|
||||
const [{ id }] = await db
|
||||
.insert(user)
|
||||
.values({
|
||||
email,
|
||||
name,
|
||||
role,
|
||||
emailVerified,
|
||||
onboardingComplete,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.returning({
|
||||
id: user.id,
|
||||
});
|
||||
|
||||
const newUser = await getUserById(id);
|
||||
|
||||
return newUser;
|
||||
}
|
||||
|
||||
export async function getAccountById(id: string) {
|
||||
return await db.query.account.findFirst({
|
||||
where: (account, { eq }) => eq(account.id, id),
|
||||
});
|
||||
}
|
||||
|
||||
export async function createUserAccount({
|
||||
userId,
|
||||
providerId,
|
||||
accountId,
|
||||
hashedPassword,
|
||||
}: {
|
||||
userId: string;
|
||||
providerId: string;
|
||||
accountId: string;
|
||||
hashedPassword?: string;
|
||||
}) {
|
||||
const [{ id }] = await db
|
||||
.insert(account)
|
||||
.values({
|
||||
userId,
|
||||
accountId,
|
||||
providerId,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
password: hashedPassword,
|
||||
})
|
||||
.returning({
|
||||
id: account.id,
|
||||
});
|
||||
|
||||
const newAccount = await getAccountById(id);
|
||||
|
||||
return newAccount;
|
||||
}
|
||||
|
||||
export async function updateUser(
|
||||
updatedUser: z.infer<typeof UserUpdateSchema>,
|
||||
) {
|
||||
return db.update(user).set(updatedUser).where(eq(user.id, updatedUser.id));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue