+23
-3
@@ -377,7 +377,7 @@ export interface Config {
|
||||
/** Database connection configuration, select base database type using the `client` field */
|
||||
database: {
|
||||
/** Default database client to use */
|
||||
client: 'better-sqlite3' | 'sqlite3' | 'pg';
|
||||
client: 'better-sqlite3' | 'sqlite3' | 'pg' | 'pg+google-cloudsql';
|
||||
/**
|
||||
* Base database connection string, or object with individual connection properties
|
||||
* @visibility secret
|
||||
@@ -390,6 +390,10 @@ export interface Config {
|
||||
* @visibility secret
|
||||
*/
|
||||
password?: string;
|
||||
/**
|
||||
* The instance connection name to use for google cloudsql connector. Should be format of `project:region:instance`
|
||||
*/
|
||||
instance?: string;
|
||||
/**
|
||||
* Other connection settings
|
||||
*/
|
||||
@@ -436,12 +440,28 @@ export interface Config {
|
||||
plugin?: {
|
||||
[pluginId: string]: {
|
||||
/** Database client override */
|
||||
client?: 'better-sqlite3' | 'sqlite3' | 'pg';
|
||||
client?: 'better-sqlite3' | 'sqlite3' | 'pg' | 'pg+google-cloudsql';
|
||||
/**
|
||||
* Database connection string or Knex object override
|
||||
* @visibility secret
|
||||
*/
|
||||
connection?: string | object;
|
||||
connection?:
|
||||
| string
|
||||
| {
|
||||
/**
|
||||
* Password that belongs to the client User
|
||||
* @visibility secret
|
||||
*/
|
||||
password?: string;
|
||||
/**
|
||||
* The instance connection name to use for google cloudsql connector. Should be format of `project:region:instance`
|
||||
*/
|
||||
instance?: string;
|
||||
/**
|
||||
* Other connection settings
|
||||
*/
|
||||
[key: string]: unknown;
|
||||
};
|
||||
/**
|
||||
* Whether to ensure the given database exists by creating it if it does not.
|
||||
* Defaults to base config if unspecified.
|
||||
|
||||
@@ -139,26 +139,26 @@ describe('postgres', () => {
|
||||
});
|
||||
|
||||
it('uses the correct config when using pg+google-cloudsql', async () => {
|
||||
const mockConnectionString = createMockConnectionString();
|
||||
const mockConnection = createMockConnection();
|
||||
|
||||
expect(
|
||||
await buildPgDatabaseConfig(
|
||||
new ConfigReader({
|
||||
client: 'pg+google-cloudsql',
|
||||
connection: mockConnectionString,
|
||||
instanceConnectionName: 'project:region:instance',
|
||||
connection: {
|
||||
user: 'ben@gke.com',
|
||||
instance: 'project:region:instance',
|
||||
port: 5423,
|
||||
},
|
||||
}),
|
||||
{ connection: { database: 'other_db' } },
|
||||
),
|
||||
).toEqual({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
...mockConnection,
|
||||
port: '5432',
|
||||
user: 'ben@gke.com',
|
||||
instance: 'project:region:instance',
|
||||
port: 5423,
|
||||
database: 'other_db',
|
||||
},
|
||||
instanceConnectionName: 'project:region:instance',
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
});
|
||||
@@ -171,27 +171,27 @@ describe('postgres', () => {
|
||||
const mockStream = (): any => {};
|
||||
Connector.prototype.getOptions.mockResolvedValue({ stream: mockStream });
|
||||
|
||||
const mockConnectionString = createMockConnectionString();
|
||||
const mockConnection = createMockConnection();
|
||||
|
||||
expect(
|
||||
await buildPgDatabaseConfig(
|
||||
new ConfigReader({
|
||||
client: 'pg+google-cloudsql',
|
||||
connection: mockConnectionString,
|
||||
instanceConnectionName: 'project:region:instance',
|
||||
connection: {
|
||||
user: 'ben@gke.com',
|
||||
instance: 'project:region:instance',
|
||||
port: 5423,
|
||||
},
|
||||
}),
|
||||
{ connection: { database: 'other_db' } },
|
||||
),
|
||||
).toEqual({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
...mockConnection,
|
||||
port: '5432',
|
||||
database: 'other_db',
|
||||
user: 'ben@gke.com',
|
||||
instance: 'project:region:instance',
|
||||
port: 5423,
|
||||
stream: mockStream,
|
||||
database: 'other_db',
|
||||
},
|
||||
instanceConnectionName: 'project:region:instance',
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -78,23 +78,25 @@ export async function buildPgDatabaseConfig(
|
||||
);
|
||||
|
||||
if (config.client === 'pg+google-cloudsql') {
|
||||
if (!config.connection.instance) {
|
||||
throw new Error('Missing instance connection name for Cloud SQL');
|
||||
}
|
||||
|
||||
const {
|
||||
Connector: CloudSqlConnector,
|
||||
IpAddressTypes,
|
||||
AuthTypes,
|
||||
} = await import('@google-cloud/cloud-sql-connector');
|
||||
// override the config to be pg for backwards compat with other code
|
||||
config.client = 'pg';
|
||||
|
||||
const connector = new CloudSqlConnector();
|
||||
const clientOpts = await connector.getOptions({
|
||||
instanceConnectionName: dbConfig.getString('instanceConnectionName'),
|
||||
instanceConnectionName: config.connection.instance,
|
||||
ipType: IpAddressTypes.PUBLIC,
|
||||
authType: AuthTypes.IAM,
|
||||
});
|
||||
|
||||
return {
|
||||
...config,
|
||||
client: 'pg',
|
||||
connection: {
|
||||
...config.connection,
|
||||
...clientOpts,
|
||||
|
||||
Reference in New Issue
Block a user