feat: databaseUrl customPrisma

pull/9078/head
Agusti Fernandez Pardo 2022-06-21 23:02:43 +02:00
parent 7558a73ab7
commit 6e78bf15f0
2 changed files with 17 additions and 13 deletions

View File

@ -3,7 +3,7 @@ import cache from "memory-cache";
import { NextMiddleware } from "next-api-middleware"; import { NextMiddleware } from "next-api-middleware";
import { PRISMA_CLIENT_CACHING_TIME } from "@calcom/api/lib/constants"; import { PRISMA_CLIENT_CACHING_TIME } from "@calcom/api/lib/constants";
import prismaAdmin from "@calcom/console/modules/common/utils/prisma"; // import prismaAdmin from "@calcom/console/modules/common/utils/prisma";
import { asStringOrUndefined } from "@calcom/lib/asStringOrNull"; import { asStringOrUndefined } from "@calcom/lib/asStringOrNull";
import { prisma, customPrisma } from "@calcom/prisma"; import { prisma, customPrisma } from "@calcom/prisma";
@ -20,26 +20,31 @@ export const customPrismaClient: NextMiddleware = async (req, res, next) => {
const id = asStringOrUndefined(key); const id = asStringOrUndefined(key);
// If we have a key, we check if it is valid. // If we have a key, we check if it is valid.
const deployment = await prismaAdmin.deployment.findUnique({ // const deployment = await prismaAdmin.deployment.findUnique({
where: { key }, // where: { key },
}); // });
if (!deployment) { const databaseUrl = await fetch(`https://console.cal.com/api/deployments/database?key=${id}`)
res.status(400).json({ error: "Invalid custom credentials id" }); .then((res) => res.json())
return; .then((res) => res.databaseUrl);
}
const credentials = deployment.databaseUrl; console.log(databaseUrl);
if (!credentials) { // if (!databaseUrl) {
// res.status(400).json({ error: "Invalid custom credentials id" });
// return;
// }
// const credentials = deployment.databaseUrl;
if (!databaseUrl) {
res.status(400).json({ error: "no databaseUrl set up at your instance yet" }); res.status(400).json({ error: "no databaseUrl set up at your instance yet" });
return; return;
} }
const hashedUrl = await hash(credentials, 12); const hashedUrl = await hash(databaseUrl, 12);
const cachedPrisma = cache.get(hashedUrl); const cachedPrisma = cache.get(hashedUrl);
if (!cachedPrisma) { if (!cachedPrisma) {
cache.put( cache.put(
hashedUrl, hashedUrl,
customPrisma({ datasources: { db: { url: credentials } } }), customPrisma({ datasources: { db: { url: databaseUrl } } }),
PRISMA_CLIENT_CACHING_TIME // Cache the prisma client for 24 hours PRISMA_CLIENT_CACHING_TIME // Cache the prisma client for 24 hours
); );
} }

View File

@ -11,7 +11,6 @@ const withTM = require("next-transpile-modules")([
"@calcom/emails", "@calcom/emails",
"@calcom/embed-core", "@calcom/embed-core",
"@calcom/embed-snippet", "@calcom/embed-snippet",
"@calcom/console",
]); ]);
module.exports = withTM({ module.exports = withTM({