feat: databaseUrl customPrisma
parent
7558a73ab7
commit
6e78bf15f0
|
@ -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
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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({
|
||||||
|
|
Loading…
Reference in New Issue