Skip to content

Commit 55a13eb

Browse files
authored
Merge pull request #406 from samchon/prisma-ts-engine
Adapt new Prisma TS engine
2 parents 253022f + 398c588 commit 55a13eb

4 files changed

Lines changed: 43 additions & 29 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"@nestjs/common": "^11.1.6",
8888
"@nestjs/core": "^11.1.6",
8989
"@nestjs/platform-express": "^11.1.6",
90+
"@prisma/adapter-pg": "^6.16.2",
9091
"@prisma/client": "^6.16.1",
9192
"dotenv": "^16.3.1",
9293
"dotenv-expand": "^10.0.0",

prisma/schema/main.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
generator client {
22
provider = "prisma-client-js"
3+
engineType = "client"
34
previewFeatures = ["postgresqlExtensions", "views"]
4-
binaryTargets = ["native"]
55
}
66

77
datasource db {

src/MyGlobal.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1+
import { PrismaPg } from "@prisma/adapter-pg";
12
import { PrismaClient } from "@prisma/client";
23
import dotenv from "dotenv";
34
import dotenvExpand from "dotenv-expand";
45
import { Singleton } from "tstl";
56
import typia from "typia";
67

8+
interface IEnvironments {
9+
MODE: "local" | "dev" | "real";
10+
API_PORT: `${number}`;
11+
SYSTEM_PASSWORD: string;
12+
13+
POSTGRES_URL: string;
14+
POSTGRES_HOST: string;
15+
POSTGRES_PORT: `${number}`;
16+
POSTGRES_DATABASE: string;
17+
POSTGRES_SCHEMA: string;
18+
POSTGRES_USERNAME: string;
19+
POSTGRES_USERNAME_READONLY: string;
20+
POSTGRES_PASSWORD: string;
21+
}
22+
const envSingleton = new Singleton(() => {
23+
const env = dotenv.config();
24+
dotenvExpand.expand(env);
25+
return typia.assert<IEnvironments>(process.env);
26+
});
27+
const prismaSingleton = new Singleton(
28+
() =>
29+
new PrismaClient({
30+
adapter: new PrismaPg(
31+
{ connectionString: envSingleton.get().POSTGRES_URL },
32+
{ schema: envSingleton.get().POSTGRES_SCHEMA },
33+
),
34+
}),
35+
);
36+
737
/**
838
* Global variables of the server.
939
*
@@ -12,10 +42,11 @@ import typia from "typia";
1242
export class MyGlobal {
1343
public static testing: boolean = false;
1444

15-
public static readonly prisma: PrismaClient = new PrismaClient();
16-
45+
public static get prisma(): PrismaClient {
46+
return prismaSingleton.get();
47+
}
1748
public static get env(): IEnvironments {
18-
return environments.get();
49+
return envSingleton.get();
1950
}
2051

2152
/**
@@ -26,7 +57,7 @@ export class MyGlobal {
2657
* - real: The server is for the real service.
2758
*/
2859
public static get mode(): "local" | "dev" | "real" {
29-
return (modeWrapper.value ??= environments.get().MODE);
60+
return (modeWrapper.value ??= envSingleton.get().MODE);
3061
}
3162

3263
/**
@@ -39,28 +70,8 @@ export class MyGlobal {
3970
modeWrapper.value = mode;
4071
}
4172
}
42-
interface IEnvironments {
43-
MODE: "local" | "dev" | "real";
44-
API_PORT: `${number}`;
45-
SYSTEM_PASSWORD: string;
46-
47-
POSTGRES_HOST: string;
48-
POSTGRES_PORT: `${number}`;
49-
POSTGRES_DATABASE: string;
50-
POSTGRES_SCHEMA: string;
51-
POSTGRES_USERNAME: string;
52-
POSTGRES_USERNAME_READONLY: string;
53-
POSTGRES_PASSWORD: string;
54-
}
5573

5674
interface IMode {
5775
value?: "local" | "dev" | "real";
5876
}
59-
6077
const modeWrapper: IMode = {};
61-
62-
const environments = new Singleton(() => {
63-
const env = dotenv.config();
64-
dotenvExpand.expand(env);
65-
return typia.assert<IEnvironments>(process.env);
66-
});

src/executable/schema.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PrismaPg } from "@prisma/adapter-pg";
12
import { PrismaClient } from "@prisma/client";
23

34
import { MyGlobal } from "../MyGlobal";
@@ -11,11 +12,12 @@ async function execute(
1112
): Promise<void> {
1213
try {
1314
const prisma = new PrismaClient({
14-
datasources: {
15-
db: {
16-
url: `postgresql://${username}:${password}@${MyGlobal.env.POSTGRES_HOST}:${MyGlobal.env.POSTGRES_PORT}/${database}`,
15+
adapter: new PrismaPg(
16+
{
17+
connectionString: `postgresql://${username}:${password}@${MyGlobal.env.POSTGRES_HOST}:${MyGlobal.env.POSTGRES_PORT}/${database}?schema=${MyGlobal.env.POSTGRES_SCHEMA}`,
1718
},
18-
},
19+
{ schema: MyGlobal.env.POSTGRES_SCHEMA },
20+
),
1921
});
2022
const queries: string[] = script
2123
.split("\n")

0 commit comments

Comments
 (0)