mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-12 22:13:05 +00:00
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
/**
|
|
* Shared local-DB guard for CLI scripts (seeds, pnpm dev guard, etc.).
|
|
* Host allowlist + ALLOW_REMOTE_DB escape hatch.
|
|
*/
|
|
|
|
import {
|
|
databaseHostname,
|
|
dieWithLabel,
|
|
isLocalDatabaseUrl,
|
|
isRemoteDbOverrideSet,
|
|
ALLOW_REMOTE_DB_FLAG as OVERRIDE_FLAG,
|
|
} from './lib/cli.mjs'
|
|
|
|
/**
|
|
* @param {string} label Log/error prefix, e.g. "seed:tse"
|
|
* @param {string} [usageHint] Extra lines shown when refusing a remote host
|
|
*/
|
|
export const assertLocalDatabase = (label, usageHint = '') => {
|
|
const die = dieWithLabel(label)
|
|
|
|
const databaseUrl = process.env.DATABASE_URL
|
|
if (!databaseUrl) die('DATABASE_URL is not set. Refusing to continue.')
|
|
|
|
if (isRemoteDbOverrideSet()) {
|
|
console.warn(
|
|
`\n[${label}] ${OVERRIDE_FLAG} is set — connecting to a remote database on purpose. Be careful.\n`,
|
|
)
|
|
return
|
|
}
|
|
|
|
const host = databaseHostname(databaseUrl)
|
|
if (host === null) die('DATABASE_URL is not a valid connection string.')
|
|
|
|
if (!isLocalDatabaseUrl(databaseUrl)) {
|
|
die(
|
|
`DATABASE_URL points at a non-local host ("${host}").\n` +
|
|
(usageHint ? `${usageHint}\n\n` : '') +
|
|
`If you REALLY mean to use a remote database, re-run with:\n ${OVERRIDE_FLAG}=true …`,
|
|
)
|
|
}
|
|
|
|
console.log(`[${label}] OK — using local database host "${host}".`)
|
|
}
|