Adapt new Prisma TS engine - #406
Conversation
There was a problem hiding this comment.
Pull Request Overview
This pull request updates the application to use Prisma's new @prisma/adapter-pg adapter for PostgreSQL connections, replacing the previous direct datasource configuration approach. The changes also introduce singleton patterns for better resource management and centralize environment variable handling.
- Adopts Prisma's new PostgreSQL adapter with enhanced configuration options
- Refactors to use singleton patterns for Prisma client and environment variables
- Updates Prisma generator configuration to use the new engine type
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| package.json | Adds the new @prisma/adapter-pg dependency |
| prisma/schema/main.prisma | Updates generator to use "client" engine type and removes binary targets |
| src/MyGlobal.ts | Implements singleton patterns for environment and Prisma client management |
| src/executable/schema.ts | Updates PrismaClient instantiation to use the new adapter |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| MODE: "local" | "dev" | "real"; | ||
| API_PORT: `${number}`; | ||
| SYSTEM_PASSWORD: string; | ||
|
|
There was a problem hiding this comment.
The POSTGRES_URL environment variable is referenced in the code but not documented in the interface. Consider adding a comment explaining its purpose and expected format, especially since it's used alongside the individual connection components.
| /** | |
| * PostgreSQL connection string used by Prisma. | |
| * Expected format: "postgresql://USER:PASSWORD@HOST:PORT/DATABASE" | |
| * If provided, this will be used to connect to the database instead of the individual connection components below. | |
| */ |
| url: `postgresql://${username}:${password}@${MyGlobal.env.POSTGRES_HOST}:${MyGlobal.env.POSTGRES_PORT}/${database}`, | ||
| adapter: new PrismaPg( | ||
| { | ||
| connectionString: `postgresql://${username}:${password}@${MyGlobal.env.POSTGRES_HOST}:${MyGlobal.env.POSTGRES_PORT}/${database}?schema=${MyGlobal.env.POSTGRES_SCHEMA}`, |
There was a problem hiding this comment.
The connection string is manually constructed here while MyGlobal.env.POSTGRES_URL is available and used elsewhere. Consider using the existing POSTGRES_URL environment variable or a helper function to maintain consistency across the codebase.
This pull request updates the way the application connects to PostgreSQL by adopting Prisma's new
@prisma/adapter-pgadapter, enhancing configuration flexibility and aligning with Prisma best practices. The changes also refactor environment variable handling and Prisma client instantiation to use singletons for improved consistency and maintainability.Prisma Adapter Integration and Database Configuration:
Adopted
@prisma/adapter-pgfor PostgreSQL connections: Added the@prisma/adapter-pgdependency and updated Prisma client instantiations in bothsrc/MyGlobal.tsandsrc/executable/schema.tsto use the new adapter, allowing for advanced configuration such as specifying the schema directly. [1] [2] [3] [4]Updated Prisma generator configuration: Modified the Prisma schema generator to use
engineType = "client"and removed thebinaryTargetsfield, aligning with recent Prisma recommendations.Environment Variable Management and Singleton Refactoring:
Centralized environment variable parsing and validation: Introduced an
IEnvironmentsinterface and a singleton to parse, expand, and validate environment variables usingtypia. All environment access now goes through this singleton. [1] [2] [3]Singleton pattern for Prisma client: Replaced direct instantiation of
PrismaClientwith a singleton getter, ensuring a single, consistently configured instance throughout the application. [1] [2]Removed legacy environment and mode wrappers: Cleaned up old environment and mode wrapper code, consolidating logic to use the new singleton-based approach.