Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Prisma examples more production friendly #416

Open
jackwotherspoon opened this issue Feb 6, 2025 · 0 comments
Open

Make Prisma examples more production friendly #416

jackwotherspoon opened this issue Feb 6, 2025 · 0 comments
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: docs Improvement to the documentation for an API.

Comments

@jackwotherspoon
Copy link
Collaborator

Today our Prisma examples have a single connect() function that creates a Connector,
starts a local proxy listener, and then creates a PrismaClient.

It returns the PrismaClient, and a function to close the client and Connector for the user
to call when done with database connections.

While this example works, it is not production friendly.

Ideally all connections to a single Cloud SQL instance should share a Connector and the
local proxy, not create a new one for each database user.

export async function connect({instanceConnectionName, user, database}) {
const path = resolve('.s.PGSQL.5432'); // postgres-required socket filename
const connector = new Connector();
await connector.startLocalProxy({
instanceConnectionName,
ipType: IpAddressTypes.PUBLIC,
authType: AuthTypes.IAM,
listenOptions: {path},
});
// note that the host parameter needs to point to the parent folder of
// the socket provided in the `path` Connector option, in this example
// that is going to be the current working directory
const datasourceUrl = `postgresql://${user}@localhost/${database}?host=${process.cwd()}`;
const prisma = new PrismaClient({datasourceUrl});
// Return PrismaClient and close() function. Call close() when you are
// done using the PrismaClient to ensure client gracefully disconnects and
// local Unix socket file created by the Connector is deleted.
return {
prisma,
async close() {
await prisma.$disconnect();
connector.close();

Solution is to move the Connector initialization and connector.startLocalProxy call out of connect(), so that the Connector can be shared across multiple connect invocations and across PrismaClients for different users.

Related to #345

@jackwotherspoon jackwotherspoon added priority: p2 Moderately-important priority. Fix may not be included in next release. type: docs Improvement to the documentation for an API. labels Feb 6, 2025
@jackwotherspoon jackwotherspoon self-assigned this Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: docs Improvement to the documentation for an API.
Projects
None yet
Development

No branches or pull requests

1 participant