I'm running Google's Cloud SQL Proxy locally and it's working with locally served Firebase functions using a command like:
/cloud_sql_proxy -instances=my-project-12345:us-central1:my-instance=tcp:1433
However I don't really know how to have this work on deployed Firebase functions.
export const typeOrmConnectionOptions: ConnectionOptions = {
name: 'primary',
type: 'mssql',
host: '127.0.0.1',
port: 1433,
username: 'sqlserver',
password: 'my$trongPa$$word',
database: 'TestDB',
synchronize: true,
logging: true,
entities: ['lib/entity/**/*.js'],
...(prod && {
extra: {
socketPath:
'/cloudsql/my-project-12345:us-central1:my-instance=tcp',
credential_file: './admin-service-account-file-long-a1b2c3-hash.json'
}
})
};
I really am taking shots in the dark as far as passing the credential file as part of the extras object to TypeORM's connections object, however I feel like something like this must be necessary to link the service account I created following this step to database queries.
Another longshot idea I had was to use the environment variable to set the credentials using this JSON file:
process.env.GOOGLE_APPLICATION_CREDENTIALS = fs.readFileSync(
'./admin-service-account-file-long-a1b2c3-hash.json',
'utf8'
)
No joy.
I don't think the error message is much help since I am certain the way I'm attempting this is fundamentally incorrect, but for what it's worth, the above gets the error
"Failed to connect to 127.0.0.1:1433 - connect ECONNREFUSED 127.0.0.1:1433"
How can I use the Cloud SQL Proxy to connect to a GCP database from Firebase?
Edit
I am not having luck connecting with either the socketPath
property, or directly referencing the IP of the GCP RD instance with root username and password. I've seen various places that the cloud proxy is only needed in local development, and also that it is needed in production (that is where I got the idea about socketPath
).
Further I have tried a test using MySql as was linked in an answer below. Formerly I had used this as a guide for SQL Server, but since that is in beta still, I thought I would give MySQL a try. Still failure, however when using that and using the services IP instead of cloud proxy, I get a timeout error.
I have also begun initializing the app with service account credentials I created from the GCP dashboard.
import { serviceAccount } from './service-account';
const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG);
adminConfig.credential = admin.credential.cert(
serviceAccount as admin.ServiceAccount
);
admin.initializeApp(adminConfig);
FIREBASE_TOKEN
come from? – Priggish