feat: optimize Prisma queries by replacing findFirst with findUnique where applicable (#21826)
* feat: optimize Prisma queries by replacing findFirst with findUnique where applicable - Replace findFirst/findFirstOrThrow with findUnique/findUniqueOrThrow for queries using unique constraints - Maintain existing functionality and error handling behavior - Focus on queries using primary keys and unique index fields from schema - Revert problematic changes that caused test failures to maintain stability Co-Authored-By: [email protected] <[email protected]> * revert: exclude API files from Prisma query optimizations per user request - Reverted all 55 API-related files to their original state - Kept all non-API Prisma query optimizations intact - API files include apps/api/v1, apps/api/v2, apps/web/app/api, and packages/app-store/*/api - Non-API optimizations remain for packages/lib, packages/features, apps/web (non-api), etc. Co-Authored-By: [email protected] <[email protected]> * feat: optimize membership query in attributeUtils to use findUnique with userId_teamId constraint Co-Authored-By: [email protected] <[email protected]> * revert: exclude test files from Prisma query optimizations per user request Co-Authored-By: [email protected] <[email protected]> * revert: revert attributeUtils.ts to use findFirst for test compatibility Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Anik Dhabal Babu <[email protected]>
This commit is contained in:
93 files changed
+205
-172
No files matched your search
@@ -186,7 +186,7 @@ export default class EventManager {
|
||||
// Fallback to cal video if no location is set
|
||||
if (!evt.location) {
|
||||
// See if cal video is enabled & has keys
|
||||
const calVideo = await prisma.app.findFirst({
|
||||
const calVideo = await prisma.app.findUnique({
|
||||
where: {
|
||||
slug: "daily-video",
|
||||
},
|
||||
@@ -461,7 +461,7 @@ export default class EventManager {
|
||||
}
|
||||
|
||||
// Get details of existing booking.
|
||||
const booking = await prisma.booking.findFirst({
|
||||
const booking = await prisma.booking.findUnique({
|
||||
where: {
|
||||
uid: rescheduleUid,
|
||||
},
|
||||
|
||||
@@ -14,7 +14,7 @@ export const bulkUpdateTeamEventsToDefaultLocation = async ({
|
||||
teamId: number;
|
||||
prisma: PrismaClient;
|
||||
}) => {
|
||||
const team = await prisma.team.findFirst({
|
||||
const team = await prisma.team.findUnique({
|
||||
where: { id: teamId },
|
||||
select: { metadata: true },
|
||||
});
|
||||
|
||||
@@ -45,7 +45,7 @@ function getResponsesFromOldBooking(
|
||||
}
|
||||
|
||||
async function getBooking(prisma: PrismaClient, uid: string) {
|
||||
const rawBooking = await prisma.booking.findFirst({
|
||||
const rawBooking = await prisma.booking.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
},
|
||||
|
||||
@@ -15,7 +15,7 @@ export async function getTeamIdFromEventType({
|
||||
|
||||
// If it's a managed event we need to find the teamId for it from the parent
|
||||
if (eventType?.parentId) {
|
||||
const managedEvent = await prisma.eventType.findFirst({
|
||||
const managedEvent = await prisma.eventType.findUnique({
|
||||
where: {
|
||||
id: eventType.parentId,
|
||||
},
|
||||
|
||||
@@ -6,7 +6,7 @@ export const getBrand = async (orgId: number | null) => {
|
||||
if (!orgId) {
|
||||
return null;
|
||||
}
|
||||
const org = await prisma.team.findFirst({
|
||||
const org = await prisma.team.findUnique({
|
||||
where: {
|
||||
id: orgId,
|
||||
},
|
||||
|
||||
@@ -59,7 +59,7 @@ async function getAttributeRoutingConfig(
|
||||
}
|
||||
const { routingFormResponseId, eventTypeId } = data;
|
||||
|
||||
const routingFormResponseQuery = await prisma.app_RoutingForms_FormResponse.findFirst({
|
||||
const routingFormResponseQuery = await prisma.app_RoutingForms_FormResponse.findUnique({
|
||||
where: {
|
||||
id: routingFormResponseId,
|
||||
},
|
||||
|
||||
@@ -26,10 +26,12 @@ export async function isOrganisationOwner(userId: number, orgId: number) {
|
||||
}
|
||||
|
||||
export async function isOrganisationMember(userId: number, orgId: number) {
|
||||
return !!(await prisma.membership.findFirst({
|
||||
return !!(await prisma.membership.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
teamId: orgId,
|
||||
userId_teamId: {
|
||||
userId,
|
||||
teamId: orgId,
|
||||
},
|
||||
},
|
||||
}));
|
||||
}
|
||||
@@ -123,7 +123,7 @@ export class BookingRepository {
|
||||
|
||||
/** Determines if the user is the organizer, team admin, or org admin that the booking was created under */
|
||||
static async doesUserIdHaveAccessToBooking({ userId, bookingId }: { userId: number; bookingId: number }) {
|
||||
const booking = await prisma.booking.findFirst({
|
||||
const booking = await prisma.booking.findUnique({
|
||||
where: {
|
||||
id: bookingId,
|
||||
},
|
||||
@@ -165,7 +165,7 @@ export class BookingRepository {
|
||||
}
|
||||
|
||||
static async findReschedulerByUid({ uid }: { uid: string }) {
|
||||
return await prisma.booking.findFirst({
|
||||
return await prisma.booking.findUnique({
|
||||
where: {
|
||||
uid,
|
||||
},
|
||||
|
||||
@@ -45,7 +45,7 @@ export class CredentialRepository {
|
||||
* Doesn't retrieve key field as that has credentials
|
||||
*/
|
||||
static async findFirstByIdWithUser({ id }: { id: number }) {
|
||||
const credential = await prisma.credential.findFirst({ where: { id }, select: safeCredentialSelect });
|
||||
const credential = await prisma.credential.findUnique({ where: { id }, select: safeCredentialSelect });
|
||||
return buildNonDelegationCredential(credential);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ export class CredentialRepository {
|
||||
* Includes 'key' field which is sensitive data.
|
||||
*/
|
||||
static async findFirstByIdWithKeyAndUser({ id }: { id: number }) {
|
||||
const credential = await prisma.credential.findFirst({
|
||||
const credential = await prisma.credential.findUnique({
|
||||
where: { id },
|
||||
select: { ...safeCredentialSelect, key: true },
|
||||
});
|
||||
|
||||
@@ -233,10 +233,12 @@ export class MembershipRepository {
|
||||
}
|
||||
|
||||
static async findFirstByUserIdAndTeamId({ userId, teamId }: { userId: number; teamId: number }) {
|
||||
return await prisma.membership.findFirst({
|
||||
return await prisma.membership.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
teamId,
|
||||
userId_teamId: {
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -247,11 +247,11 @@ export class OrganizationRepository {
|
||||
}
|
||||
|
||||
static async findCurrentOrg({ userId, orgId }: { userId: number; orgId: number }) {
|
||||
const membership = await prisma.membership.findFirst({
|
||||
const membership = await prisma.membership.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
team: {
|
||||
id: orgId,
|
||||
userId_teamId: {
|
||||
userId,
|
||||
teamId: orgId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
|
||||
@@ -364,10 +364,12 @@ export class ProfileRepository {
|
||||
if (!organizationId) {
|
||||
return null;
|
||||
}
|
||||
const profile = await prisma.profile.findFirst({
|
||||
const profile = await prisma.profile.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
organizationId,
|
||||
userId_organizationId: {
|
||||
userId,
|
||||
organizationId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
organization: {
|
||||
@@ -401,10 +403,12 @@ export class ProfileRepository {
|
||||
organizationId: number;
|
||||
username: string;
|
||||
}) {
|
||||
const profile = await prisma.profile.findFirst({
|
||||
const profile = await prisma.profile.findUnique({
|
||||
where: {
|
||||
username,
|
||||
organizationId,
|
||||
username_organizationId: {
|
||||
username,
|
||||
organizationId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
organization: {
|
||||
|
||||
@@ -636,7 +636,7 @@ export class UserRepository {
|
||||
return user;
|
||||
}
|
||||
static async getUserAdminTeams(userId: number) {
|
||||
return prisma.user.findFirst({
|
||||
return prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
@@ -713,10 +713,12 @@ export class UserRepository {
|
||||
return !!teams.length;
|
||||
}
|
||||
static async isAdminOrOwnerOfTeam({ userId, teamId }: { userId: number; teamId: number }) {
|
||||
const isAdminOrOwnerOfTeam = await prisma.membership.findFirst({
|
||||
const isAdminOrOwnerOfTeam = await prisma.membership.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
teamId,
|
||||
userId_teamId: {
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
role: { in: [MembershipRole.ADMIN, MembershipRole.OWNER] },
|
||||
accepted: true,
|
||||
},
|
||||
@@ -876,7 +878,7 @@ export class UserRepository {
|
||||
}
|
||||
|
||||
static async getUserStats({ userId }: { userId: number }) {
|
||||
const user = await prisma.user.findFirst({
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
|
||||
@@ -6,7 +6,7 @@ import { EventTypeMetaDataSchema, EventTypeAppMetadataSchema } from "@calcom/pri
|
||||
|
||||
export class EventTypeService {
|
||||
static async getEventTypeAppDataFromId(eventTypeId: number, appSlug: keyof typeof appDataSchemas) {
|
||||
const eventType = await prisma.eventType.findFirst({
|
||||
const eventType = await prisma.eventType.findUnique({
|
||||
where: {
|
||||
id: eventTypeId,
|
||||
},
|
||||
|
||||
@@ -5,7 +5,7 @@ import { WorkflowRepository } from "../repository/workflow";
|
||||
// TODO (Sean): Move most of the logic migrated in 16861 to this service
|
||||
export class WorkflowService {
|
||||
static async deleteWorkflowRemindersOfRemovedTeam(teamId: number) {
|
||||
const team = await prisma.team.findFirst({
|
||||
const team = await prisma.team.findUnique({
|
||||
where: {
|
||||
id: teamId,
|
||||
},
|
||||
|
||||
@@ -85,7 +85,7 @@ export const validateAndGetCorrectedUsernameInTeam = async (
|
||||
isSignup: boolean
|
||||
) => {
|
||||
try {
|
||||
const team = await prisma.team.findFirst({
|
||||
const team = await prisma.team.findUnique({
|
||||
where: {
|
||||
id: teamId,
|
||||
},
|
||||
|
||||
@@ -92,7 +92,7 @@ const createMeeting = async (credential: CredentialPayload, calEvent: CalendarEv
|
||||
};
|
||||
try {
|
||||
// Check to see if video app is enabled
|
||||
const enabledApp = await prisma.app.findFirst({
|
||||
const enabledApp = await prisma.app.findUnique({
|
||||
where: {
|
||||
slug: credential.appId,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user