fix: ajustes dashboards tv e titulos

This commit is contained in:
Esdras Renan 2025-11-06 11:21:40 -03:00
parent 80abd92e78
commit 1b32638eb5
9 changed files with 609 additions and 232 deletions

View file

@ -1,5 +1,4 @@
import path from "node:path"
import { fileURLToPath, pathToFileURL } from "node:url"
import { PrismaClient } from "@prisma/client"
@ -9,10 +8,7 @@ declare global {
// Resolve a robust DATABASE_URL for all runtimes (prod/dev)
const PROJECT_ROOT = process.cwd()
const PROJECT_ROOT_URL = (() => {
const baseHref = pathToFileURL(PROJECT_ROOT).href
return new URL(baseHref.endsWith("/") ? baseHref : `${baseHref}/`)
})()
const PRISMA_DIR = path.join(PROJECT_ROOT, "prisma")
function resolveFileUrl(url: string) {
if (!url.startsWith("file:")) {
@ -20,31 +16,44 @@ function resolveFileUrl(url: string) {
}
const filePath = url.slice("file:".length)
if (filePath.startsWith("./") || filePath.startsWith("../")) {
const normalized = path.normalize(filePath)
const targetUrl = new URL(normalized, PROJECT_ROOT_URL)
const absolutePath = fileURLToPath(targetUrl)
if (!absolutePath.startsWith(PROJECT_ROOT)) {
throw new Error(`DATABASE_URL path escapes project directory: ${filePath}`)
}
return `file:${absolutePath}`
if (filePath.startsWith("//")) {
return url
}
if (!filePath.startsWith("/")) {
return resolveFileUrl(`file:./${filePath}`)
if (path.isAbsolute(filePath)) {
return `file:${path.normalize(filePath)}`
}
return url
const normalized = path.normalize(filePath)
const prismaPrefix = `prisma${path.sep}`
const relativeToPrisma = normalized.startsWith(prismaPrefix)
? normalized.slice(prismaPrefix.length)
: normalized
const absolutePath = path.resolve(PRISMA_DIR, relativeToPrisma)
if (!absolutePath.startsWith(PROJECT_ROOT)) {
throw new Error(`DATABASE_URL path escapes project directory: ${filePath}`)
}
return `file:${absolutePath}`
}
const resolvedDatabaseUrl = (() => {
const envUrl = process.env.DATABASE_URL?.trim()
if (envUrl && envUrl.length > 0) return resolveFileUrl(envUrl)
// Fallbacks by environment to ensure correctness in containers
function normalizeDatasourceUrl(envUrl?: string | null) {
const trimmed = envUrl?.trim()
if (trimmed) {
return resolveFileUrl(trimmed)
}
if (process.env.NODE_ENV === "production") {
return "file:/app/data/db.sqlite"
}
// In development, prefer a dedicated dev DB file
return resolveFileUrl("file:./prisma/db.dev.sqlite")
})()
return resolveFileUrl("file:./db.dev.sqlite")
}
const resolvedDatabaseUrl = normalizeDatasourceUrl(process.env.DATABASE_URL)
export const prisma =
global.prisma ?? new PrismaClient({ datasources: { db: { url: resolvedDatabaseUrl } } })