-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprisma.config.ts
More file actions
60 lines (47 loc) · 1.52 KB
/
Copy pathprisma.config.ts
File metadata and controls
60 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { existsSync, readFileSync } from "node:fs";
import path from "node:path";
import { defineConfig } from "prisma/config";
loadLocalEnvFiles([".env.local", ".env"]);
const migrationDatabaseUrl =
nonEmptyEnv("DATABASE_URL_TEST") ?? nonEmptyEnv("POSTGRES_URL_NON_POOLING") ?? nonEmptyEnv("POSTGRES_PRISMA_URL");
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: migrationDatabaseUrl
? {
url: migrationDatabaseUrl,
}
: undefined,
});
function loadLocalEnvFiles(fileNames: string[]): void {
for (const fileName of fileNames) {
const envPath = path.join(process.cwd(), fileName);
if (!existsSync(envPath)) {
continue;
}
const source = readFileSync(envPath, "utf8");
for (const line of source.split(/\r?\n/)) {
const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line.trim());
if (!match || nonEmptyEnv(match[1])) {
continue;
}
const [, key, rawValue] = match;
process.env[key] = unquoteEnvValue(rawValue.trim());
}
}
}
function unquoteEnvValue(value: string): string {
if (value.length >= 2 && value.startsWith('"') && value.endsWith('"')) {
return value.slice(1, -1).replaceAll("\\n", "\n");
}
if (value.length >= 2 && value.startsWith("'") && value.endsWith("'")) {
return value.slice(1, -1);
}
return value;
}
function nonEmptyEnv(key: string): string | undefined {
const value = process.env[key]?.trim();
return value ? value : undefined;
}