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 { 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 { prisma, customPrisma } from "@calcom/prisma";
@ -20,26 +20,31 @@ export const customPrismaClient: NextMiddleware = async (req, res, next) => {
const id = asStringOrUndefined(key);
// If we have a key, we check if it is valid.
const deployment = await prismaAdmin.deployment.findUnique({
where: { key },
});
if (!deployment) {
res.status(400).json({ error: "Invalid custom credentials id" });
return;
}
const credentials = deployment.databaseUrl;
if (!credentials) {
// const deployment = await prismaAdmin.deployment.findUnique({
// where: { key },
// });
const databaseUrl = await fetch(`https://console.cal.com/api/deployments/database?key=${id}`)
.then((res) => res.json())
.then((res) => res.databaseUrl);
console.log(databaseUrl);
// 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" });
return;
}
const hashedUrl = await hash(credentials, 12);
const hashedUrl = await hash(databaseUrl, 12);
const cachedPrisma = cache.get(hashedUrl);
if (!cachedPrisma) {
cache.put(
hashedUrl,
customPrisma({ datasources: { db: { url: credentials } } }),
customPrisma({ datasources: { db: { url: databaseUrl } } }),
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/embed-core",
"@calcom/embed-snippet",
"@calcom/console",
]);
module.exports = withTM({