import { tryLoadEnvs , ClientEngineType } from '@prisma/sdk' ; import type { EnvPaths } from '@prisma/sdk' ; import type { InternalDatasource } from '../../runtime/utils/printDatasources' ; type LoadedEnv = { parsed : { [ x ] : string | undefined ; } ; } ; see declareInlineEnv param clientEngineType param datasources param envPaths returns export function buildInlineEnv ( clientEngineType , datasources , envPaths ) { if ( clientEngineType === ClientEngineType . DataProxy ) { const envVarNames = getSelectedEnvVarNames ( datasources ) ; const loadedEnv = loadSelectedEnvVars ( envPaths , envVarNames ) ; return declareInlineEnv ( loadedEnv ) ; } return `` ; } param datasources returns function getSelectedEnvVarNames ( datasources ) { return datasources . reduce ( ( acc , datasource ) => { if ( datasource . url . fromEnvVar ) { return [ ... acc , datasource . url . fromEnvVar ] ; } return acc ; } , [ ] as string [ ] ) ; } param envPaths param envVarNames returns function loadSelectedEnvVars ( envPaths , envVarNames ) { const loadedEnv = tryLoadEnvs ( envPaths , { conflictCheck : 'warn' } ) ; const selectedEnv = { parsed : { } } ; for ( const envVarName of envVarNames ) { const loadedEnvVar = loadedEnv ?. parsed [ envVarName ] ; selectedEnv . parsed [ envVarName ] = loadedEnvVar ; } return selectedEnv ; } /**
 * Creates the necessary declarations to embed env vars directly inside of the
 * generated client. We abuse a custom `JSON.stringify` replacer to transform
 *  loadedEnv into a piece of code. The goal of this is to take that and
 * generate a new object which re-prioritizes the loading of the environment.
 *
 * By generating an output with `${key} || process.env.${key} || '${value}'` it
 * would yield something like `DB_URL || process.env.DB_URL || 'prisma://...'`:
 * - For Cloudflare Workers `DB_URL` will be the first to be found (runtime)
 *   - `process.env.DB_URL` will be undefined
 * - For Vercel `process.env.DB_URL` is replaced by webpack by a value
 *   - `DB_URL` will be undefined at anytime
 * - If none of them were provided, we fallback to the one of the .env file
 *   - If empty, the value will be `undefined`
 *
 * As per the current standard, env vars have priority over any .env files.
 *  param loadedEnv function declareInlineEnv ( loadedEnv ) { const inlineEnvDeclaration = JSON . stringify ( loadedEnv , ( key , value ) => { if ( key === '' ) return value ; if ( key === 'parsed' ) return value ; const cfwEnv = `typeof global !== 'undefined' && global['${ key }']` ; const vercelEnv = `process.env['${ key }']` ; const dotEnv = value ? `'${ value }'` : 'undefined' ; return `${ cfwEnv } || ${ vercelEnv } || ${ dotEnv }` ; } , 2 ) . replace ( /"/g , '' ) ; return `
config.inlineEnv = ${ inlineEnvDeclaration }` ; }