GoogleCloudPlatform / cloud-sql-nodejs-connector

A JavaScript library for connecting securely to your Cloud SQL instances
Apache License 2.0
66 stars 8 forks source link

feat: add local Unix domain socket support #336

Closed jackwotherspoon closed 4 months ago

jackwotherspoon commented 4 months ago

Adds a new Connector.startLocalProxy method that starts a local proxy tunnel listening to the location defined at listenOptions enabling usage of the Connector by drivers and different libraries / frameworks that are not currently supported by the Connector directly but that can connect to databases via Unix Sockets.

Example usage:

import {Connector} from '@google-cloud/cloud-sql-connector';
import {PrismaClient} from '@prisma/client';

const connector = new Connector();
await connector.startLocalProxy({
  instanceConnectionName: 'my-project:us-east1:my-instance',
  listenOptions: { path: '.s.PGSQL.5432' },
});
const hostPath = process.cwd();

const datasourceUrl =
 `postgresql://my-user:password@localhost/dbName?host=${hostPath}`;
const prisma = new PrismaClient({ datasourceUrl });

connector.close();
await prisma.$disconnect();

Fixes: https://github.com/GoogleCloudPlatform/cloud-sql-nodejs-connector/issues/113

Co-authored-by: @ruyadorno

jackwotherspoon commented 4 months ago

Let's see if we can write the test coverage for this as an e2e test and remove the unit style test. WDYT?

Once we add all the samples they are like e2e tests...

jackwotherspoon commented 4 months ago

Let's see if we can write the test coverage for this as an e2e test and remove the unit style test. WDYT?

Removed the unit test but it seems the unit test coverage is now dropping. @enocom do you think we should leave the test for full coverage? I will take a look into improving the unit tests and mock infra in the future to make them more like Go and Python.