perf: move to disable prisma client extension inference (#23692)
* perf: move to disable prisma client extension inference * Prisma doesn't like it when you pass Record<string, unknown> * API v1 type fixes * Missed one * Fix unit test fail due to faulty expect * Assigning to prisma InputJsonValue/Array must be done as object, not interface * Fix @calcom/web ts error, teams not defined * Run eslint formatter * fixed the routingFormHelpers file causing a failing app store e2e test
This commit is contained in:
1 parent
38bc5fb71e
commit
aa51218428
24 files changed
+295
-253
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import type z from "zod";
|
||||
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
|
||||
import type { OAuth2UniversalSchemaWithCalcomBackwardCompatibility } from "./universalSchema";
|
||||
|
||||
@@ -23,7 +23,7 @@ export const updateTokenObject = async ({
|
||||
id: credentialId,
|
||||
},
|
||||
data: {
|
||||
key: tokenObject,
|
||||
key: tokenObject as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ import { z } from "zod";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import type { CalendarEvent } from "@calcom/types/Calendar";
|
||||
import type { CredentialPayload } from "@calcom/types/Credential";
|
||||
import type { PartialReference } from "@calcom/types/EventManager";
|
||||
@@ -102,7 +103,7 @@ const NextcloudTalkVideoApiAdapter = (credential: CredentialPayload): VideoApiAd
|
||||
id: credential.id,
|
||||
},
|
||||
data: {
|
||||
key: newTokenObject,
|
||||
key: newTokenObject as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
@@ -10,7 +10,8 @@ import {
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { getPiiFreeCalendarEvent } from "@calcom/lib/piiFreeData";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { Frequency } from "@calcom/prisma/zod-utils";
|
||||
import type { CalendarEvent } from "@calcom/types/Calendar";
|
||||
import type { CredentialPayload } from "@calcom/types/Credential";
|
||||
@@ -316,7 +317,8 @@ const ZoomVideoApiAdapter = (credential: CredentialPayload): VideoApiAdapter =>
|
||||
id: credential.id,
|
||||
},
|
||||
data: {
|
||||
key: newTokenObject,
|
||||
// z.passthrough() is not allowed in Prisma, but we know this is trusted.
|
||||
key: newTokenObject as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
@@ -36,7 +36,6 @@ export class FeaturesRepository implements IFeaturesRepository {
|
||||
|
||||
const features = await this.prismaClient.feature.findMany({
|
||||
orderBy: { slug: "asc" },
|
||||
cacheStrategy: { swr: 300, ttl: 300 },
|
||||
});
|
||||
|
||||
FeaturesRepository.featuresCache = {
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
} from "@calcom/features/insights/server/raw-data.schema";
|
||||
import { getInsightsBookingService } from "@calcom/lib/di/containers/InsightsBooking";
|
||||
import { getInsightsRoutingService } from "@calcom/lib/di/containers/InsightsRouting";
|
||||
import type { readonlyPrisma } from "@calcom/prisma";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { BookingStatus } from "@calcom/prisma/enums";
|
||||
import authedProcedure from "@calcom/trpc/server/procedures/authedProcedure";
|
||||
import { router } from "@calcom/trpc/server/trpc";
|
||||
@@ -31,7 +31,7 @@ const UserBelongsToTeamInput = z.object({
|
||||
type BuildBaseWhereConditionCtxType = {
|
||||
userIsOwnerAdminOfParentTeam: boolean;
|
||||
userOrganizationId: number | null;
|
||||
insightsDb: typeof readonlyPrisma;
|
||||
insightsDb: PrismaClient;
|
||||
};
|
||||
|
||||
interface BuildBaseWhereConditionType {
|
||||
@@ -341,7 +341,7 @@ function createInsightsBookingService(
|
||||
}
|
||||
|
||||
function createInsightsRoutingService(
|
||||
ctx: { insightsDb: typeof readonlyPrisma; user: { id: number; organizationId: number | null } },
|
||||
ctx: { insightsDb: PrismaClient; user: { id: number; organizationId: number | null } },
|
||||
input: z.infer<typeof routingRepositoryBaseInputSchema>
|
||||
) {
|
||||
return getInsightsRoutingService({
|
||||
@@ -1037,7 +1037,7 @@ export async function getEventTypeList({
|
||||
isAll,
|
||||
user,
|
||||
}: {
|
||||
prisma: typeof readonlyPrisma;
|
||||
prisma: PrismaClient;
|
||||
teamId: number | null | undefined;
|
||||
userId: number | null | undefined;
|
||||
isAll: boolean | undefined;
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
|
||||
import type { FormResponse } from "@calcom/app-store/routing-forms/types/types";
|
||||
import { withReporting } from "@calcom/lib/sentryWrapper";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { bookingMinimalSelect } from "@calcom/prisma";
|
||||
import type { Booking } from "@calcom/prisma/client";
|
||||
import { RRTimestampBasis } from "@calcom/prisma/enums";
|
||||
import { BookingStatus } from "@calcom/prisma/enums";
|
||||
import { RRTimestampBasis, BookingStatus } from "@calcom/prisma/enums";
|
||||
import { bookingMinimalSelect } from "@calcom/prisma/selects/booking";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
|
||||
import { UserRepository } from "./user";
|
||||
|
||||
export type FormResponse = Record<
|
||||
// Field ID
|
||||
string,
|
||||
{
|
||||
value: number | string | string[];
|
||||
label: string;
|
||||
identifier?: string;
|
||||
}
|
||||
>;
|
||||
|
||||
type TeamBookingsParamsBase = {
|
||||
user: { id: number; email: string };
|
||||
teamId: number;
|
||||
@@ -540,8 +548,10 @@ export class BookingRepository {
|
||||
},
|
||||
data: {
|
||||
location,
|
||||
metadata,
|
||||
...(responses && { responses }),
|
||||
// FIXME: metadata is untyped
|
||||
metadata: metadata as unknown as Prisma.InputJsonValue,
|
||||
// FIXME: responses is untyped
|
||||
...(responses && { responses: responses as unknown as Prisma.InputJsonValue }),
|
||||
...(iCalSequence !== undefined && { iCalSequence }),
|
||||
references: {
|
||||
create: referencesToCreate,
|
||||
@@ -681,9 +691,13 @@ export class BookingRepository {
|
||||
},
|
||||
},
|
||||
},
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
destinationCalendar: true,
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
payment: true,
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
references: true,
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
workflowReminders: true,
|
||||
},
|
||||
});
|
||||
@@ -825,9 +839,13 @@ export class BookingRepository {
|
||||
status: filterForUnconfirmed ? BookingStatus.PENDING : BookingStatus.ACCEPTED,
|
||||
},
|
||||
include: {
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
attendees: true,
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
references: true,
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
user: true,
|
||||
// eslint-disable-next-line @calcom/eslint/no-prisma-include-true
|
||||
payment: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -97,7 +97,8 @@ export class DelegationCredentialRepository {
|
||||
id: data.organizationId,
|
||||
},
|
||||
},
|
||||
serviceAccountKey: encryptedKey,
|
||||
// z.passthrough() is not allowed in Prisma, but we know this is trusted.
|
||||
serviceAccountKey: encryptedKey as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
select: delegationCredentialSafeSelect,
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { availabilityUserSelect, prisma, type PrismaTransaction, type PrismaClient } from "@calcom/prisma";
|
||||
import { availabilityUserSelect, prisma, type PrismaTransaction } from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import type { Prisma, Membership } from "@calcom/prisma/client";
|
||||
import type { Prisma, Membership, PrismaClient } from "@calcom/prisma/client";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
|
||||
import logger from "../../logger";
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { Prisma } from "@calcom/prisma/client";
|
||||
import type { TServiceAccountKeySchema } from "@calcom/prisma/zod-utils";
|
||||
import { serviceAccountKeySchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
@@ -42,7 +41,7 @@ export class WorkspacePlatformRepository {
|
||||
: _defaultServiceAccountKey;
|
||||
return await prisma.workspacePlatform.create({
|
||||
data: {
|
||||
defaultServiceAccountKey,
|
||||
defaultServiceAccountKey: defaultServiceAccountKey as unknown as Prisma.InputJsonValue,
|
||||
...rest,
|
||||
},
|
||||
select: safeWorkspacePlatformSelect,
|
||||
@@ -95,7 +94,10 @@ export class WorkspacePlatformRepository {
|
||||
}) {
|
||||
return await prisma.workspacePlatform.update({
|
||||
where: { id },
|
||||
data,
|
||||
data: {
|
||||
...data,
|
||||
defaultServiceAccountKey: data.defaultServiceAccountKey as unknown as Prisma.InputJsonValue,
|
||||
},
|
||||
select: safeWorkspacePlatformSelect,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
isNumberFilterValue,
|
||||
} from "@calcom/features/data-table/lib/utils";
|
||||
import type { DateRange } from "@calcom/features/insights/server/insightsDateUtils";
|
||||
import type { readonlyPrisma } from "@calcom/prisma";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
import { MembershipRepository } from "../repository/membership";
|
||||
@@ -142,7 +142,7 @@ const NOTHING_CONDITION = Prisma.sql`1=0`;
|
||||
const bookingDataKeys = new Set(Object.keys(bookingDataSchema.shape));
|
||||
|
||||
export class InsightsBookingBaseService {
|
||||
private prisma: typeof readonlyPrisma;
|
||||
private prisma: PrismaClient;
|
||||
private options: InsightsBookingServiceOptions | null;
|
||||
private filters: InsightsBookingServiceFilterOptions | null;
|
||||
private cachedAuthConditions?: Prisma.Sql;
|
||||
@@ -153,7 +153,7 @@ export class InsightsBookingBaseService {
|
||||
options,
|
||||
filters,
|
||||
}: {
|
||||
prisma: typeof readonlyPrisma;
|
||||
prisma: PrismaClient;
|
||||
options: InsightsBookingServicePublicOptions;
|
||||
filters?: InsightsBookingServiceFilterOptions;
|
||||
}) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
isSingleSelectFilterValue,
|
||||
} from "@calcom/features/data-table/lib/utils";
|
||||
import type { DateRange } from "@calcom/features/insights/server/insightsDateUtils";
|
||||
import type { readonlyPrisma } from "@calcom/prisma";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { BookingStatus } from "@calcom/prisma/enums";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
@@ -136,7 +136,7 @@ type GetConditionsOptions = {
|
||||
};
|
||||
|
||||
export class InsightsRoutingBaseService {
|
||||
private prisma: typeof readonlyPrisma;
|
||||
private prisma: PrismaClient;
|
||||
private options: InsightsRoutingServiceOptions | null;
|
||||
private filters: InsightsRoutingServiceFilterOptions;
|
||||
|
||||
@@ -145,7 +145,7 @@ export class InsightsRoutingBaseService {
|
||||
options,
|
||||
filters,
|
||||
}: {
|
||||
prisma: typeof readonlyPrisma;
|
||||
prisma: PrismaClient;
|
||||
options: InsightsRoutingServicePublicOptions;
|
||||
filters: InsightsRoutingServiceFilterOptions;
|
||||
}) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { readonlyPrisma } from "@calcom/prisma";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
import {
|
||||
InsightsRoutingBaseService,
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
} from "./InsightsRoutingBaseService";
|
||||
|
||||
export interface IInsightsRoutingService {
|
||||
prisma: typeof readonlyPrisma;
|
||||
prisma: PrismaClient;
|
||||
}
|
||||
|
||||
export class InsightsRoutingService {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import type { PrismaClient as PrismaClientWithoutExtensions } from "@prisma/client";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
import { waitUntil } from "@vercel/functions";
|
||||
|
||||
import { UsageEvent, LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { DeploymentRepository } from "@calcom/lib/server/repository/deployment";
|
||||
|
||||
async function incrementUsage(prismaClient: PrismaClientWithoutExtensions, event?: UsageEvent) {
|
||||
async function incrementUsage(prismaClient: PrismaClient, event?: UsageEvent) {
|
||||
const deploymentRepo = new DeploymentRepository(prismaClient);
|
||||
try {
|
||||
const licenseKeyService = await LicenseKeySingleton.getInstance(deploymentRepo);
|
||||
@@ -15,7 +15,7 @@ async function incrementUsage(prismaClient: PrismaClientWithoutExtensions, event
|
||||
}
|
||||
}
|
||||
|
||||
export function usageTrackingExtention(prismaClient: PrismaClientWithoutExtensions) {
|
||||
export function usageTrackingExtention(prismaClient: PrismaClient) {
|
||||
return Prisma.defineExtension({
|
||||
query: {
|
||||
booking: {
|
||||
|
||||
+20
-25
@@ -1,6 +1,5 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import { PrismaClient as PrismaClientWithoutExtension } from "@prisma/client";
|
||||
import { withAccelerate } from "@prisma/extension-accelerate";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
import { bookingIdempotencyKeyExtension } from "./extensions/booking-idempotency-key";
|
||||
import { disallowUndefinedDeleteUpdateManyExtension } from "./extensions/disallow-undefined-delete-update-many";
|
||||
@@ -12,8 +11,7 @@ import { bookingReferenceMiddleware } from "./middleware";
|
||||
const prismaOptions: Prisma.PrismaClientOptions = {};
|
||||
|
||||
const globalForPrisma = global as unknown as {
|
||||
prismaWithoutClientExtensions: PrismaClientWithoutExtension;
|
||||
prismaWithClientExtensions: PrismaClientWithExtensions;
|
||||
baseClient: PrismaClient;
|
||||
};
|
||||
|
||||
const loggerLevel = parseInt(process.env.NEXT_PUBLIC_LOGGER_LEVEL ?? "", 10);
|
||||
@@ -37,34 +35,32 @@ if (!isNaN(loggerLevel)) {
|
||||
}
|
||||
}
|
||||
|
||||
// Prevents flooding with idle connections
|
||||
const prismaWithoutClientExtensions =
|
||||
globalForPrisma.prismaWithoutClientExtensions || new PrismaClientWithoutExtension(prismaOptions);
|
||||
const baseClient =
|
||||
globalForPrisma.baseClient || new PrismaClient(prismaOptions);
|
||||
|
||||
export const customPrisma = (options?: Prisma.PrismaClientOptions) =>
|
||||
new PrismaClientWithoutExtension({ ...prismaOptions, ...options })
|
||||
.$extends(usageTrackingExtention(prismaWithoutClientExtensions))
|
||||
new PrismaClient({ ...prismaOptions, ...options })
|
||||
.$extends(usageTrackingExtention(baseClient))
|
||||
.$extends(excludeLockedUsersExtension())
|
||||
.$extends(excludePendingPaymentsExtension())
|
||||
.$extends(bookingIdempotencyKeyExtension())
|
||||
.$extends(disallowUndefinedDeleteUpdateManyExtension())
|
||||
.$extends(withAccelerate());
|
||||
.$extends(disallowUndefinedDeleteUpdateManyExtension()) as unknown as PrismaClient;
|
||||
|
||||
// If any changed on middleware server restart is required
|
||||
// TODO: Migrate it to $extends
|
||||
bookingReferenceMiddleware(prismaWithoutClientExtensions);
|
||||
bookingReferenceMiddleware(baseClient);
|
||||
|
||||
// FIXME: Due to some reason, there are types failing in certain places due to the $extends. Fix it and then enable it
|
||||
// Specifically we get errors like `Type 'string | Date | null | undefined' is not assignable to type 'Exact<string | Date | null | undefined, string | Date | null | undefined>'`
|
||||
const prismaWithClientExtensions = prismaWithoutClientExtensions
|
||||
.$extends(usageTrackingExtention(prismaWithoutClientExtensions))
|
||||
|
||||
// Explanation why we cast as PrismaClient. When we leave Prisma to its devices it tries to infer logic based on the extensions, but this is not a simple extends.
|
||||
// this makes the PrismaClient export type-hint impossible and it also is a massive hit on Prisma type hinting performance.
|
||||
export const prisma: PrismaClient = baseClient
|
||||
.$extends(usageTrackingExtention(baseClient))
|
||||
.$extends(excludeLockedUsersExtension())
|
||||
.$extends(excludePendingPaymentsExtension())
|
||||
.$extends(bookingIdempotencyKeyExtension())
|
||||
.$extends(disallowUndefinedDeleteUpdateManyExtension())
|
||||
.$extends(withAccelerate());
|
||||
|
||||
export const prisma = globalForPrisma.prismaWithClientExtensions || prismaWithClientExtensions;
|
||||
.$extends(disallowUndefinedDeleteUpdateManyExtension()) as unknown as PrismaClient;
|
||||
|
||||
// This prisma instance is meant to be used only for READ operations.
|
||||
// If self hosting, feel free to leave INSIGHTS_DATABASE_URL as empty and `readonlyPrisma` will default to `prisma`.
|
||||
@@ -75,24 +71,23 @@ export const readonlyPrisma = process.env.INSIGHTS_DATABASE_URL
|
||||
: prisma;
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
globalForPrisma.prismaWithoutClientExtensions = prismaWithoutClientExtensions;
|
||||
globalForPrisma.prismaWithClientExtensions = prisma;
|
||||
globalForPrisma.baseClient = baseClient;
|
||||
}
|
||||
|
||||
type PrismaClientWithExtensions = typeof prismaWithClientExtensions;
|
||||
export type PrismaClient = PrismaClientWithExtensions;
|
||||
|
||||
type OmitPrismaClient = Omit<
|
||||
PrismaClient,
|
||||
"$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends"
|
||||
>;
|
||||
|
||||
// we cant pass tx to functions as types miss match since we have a custom prisma client https://github.com/prisma/prisma/discussions/20924#discussioncomment-10077649
|
||||
export type PrismaTransaction = OmitPrismaClient;
|
||||
export type {
|
||||
OmitPrismaClient as PrismaTransaction,
|
||||
// we re-export the native PrismaClient type for backwards-compatibility.
|
||||
PrismaClient
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated Use named export `prisma` instead
|
||||
*/
|
||||
export default prisma;
|
||||
|
||||
export * from "./selects";
|
||||
Reference in new issue
Block a user