chore: sets the connection type to default for postgres
Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
@@ -220,6 +220,41 @@ describe('postgres', () => {
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an error when the connection type is not supported', async () => {
|
||||
await expect(
|
||||
buildPgDatabaseConfig(
|
||||
new ConfigReader({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
type: 'not-supported',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).rejects.toThrow('Unknown connection type: not-supported');
|
||||
});
|
||||
|
||||
it('supports default as the default connection type', async () => {
|
||||
await expect(
|
||||
buildPgDatabaseConfig(
|
||||
new ConfigReader({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
type: 'default',
|
||||
port: '5432',
|
||||
database: 'other_db',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).resolves.toEqual({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
port: '5432',
|
||||
database: 'other_db',
|
||||
},
|
||||
useNullAsDefault: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPgConnectionConfig', () => {
|
||||
|
||||
@@ -69,6 +69,7 @@ export async function buildPgDatabaseConfig(
|
||||
overrides?: Knex.Config,
|
||||
) {
|
||||
const config = mergeDatabaseConfig(
|
||||
{ connection: { type: 'default' } },
|
||||
dbConfig.get(),
|
||||
{
|
||||
connection: getPgConnectionConfig(dbConfig, !!overrides),
|
||||
@@ -77,44 +78,48 @@ export async function buildPgDatabaseConfig(
|
||||
overrides,
|
||||
);
|
||||
|
||||
if (config.connection?.type === 'cloudsql') {
|
||||
if (config.client !== 'pg') {
|
||||
throw new Error('Cloud SQL only supports the pg client');
|
||||
let transformedConfig = config;
|
||||
|
||||
if (config.connection.type) {
|
||||
if (config.connection.type === 'cloudsql') {
|
||||
if (config.client !== 'pg') {
|
||||
throw new Error('Cloud SQL only supports the pg client');
|
||||
}
|
||||
|
||||
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');
|
||||
const connector = new CloudSqlConnector();
|
||||
const clientOpts = await connector.getOptions({
|
||||
instanceConnectionName: config.connection.instance,
|
||||
ipType: IpAddressTypes.PUBLIC,
|
||||
authType: AuthTypes.IAM,
|
||||
});
|
||||
|
||||
transformedConfig = {
|
||||
...config,
|
||||
client: 'pg',
|
||||
connection: {
|
||||
...config.connection,
|
||||
...clientOpts,
|
||||
},
|
||||
};
|
||||
} else if (config.connection.type !== 'default') {
|
||||
throw new Error(`Unknown connection type: ${config.connection.type}`);
|
||||
}
|
||||
|
||||
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');
|
||||
const connector = new CloudSqlConnector();
|
||||
const clientOpts = await connector.getOptions({
|
||||
instanceConnectionName: config.connection.instance,
|
||||
ipType: IpAddressTypes.PUBLIC,
|
||||
authType: AuthTypes.IAM,
|
||||
});
|
||||
|
||||
const cloudsqlConfig = {
|
||||
...config,
|
||||
client: 'pg',
|
||||
connection: {
|
||||
...config.connection,
|
||||
...clientOpts,
|
||||
},
|
||||
};
|
||||
|
||||
// Trim additional properties from the connection object passed to knex
|
||||
delete cloudsqlConfig.connection.type;
|
||||
delete cloudsqlConfig.connection.instance;
|
||||
|
||||
return cloudsqlConfig;
|
||||
}
|
||||
|
||||
return config;
|
||||
// Remove the connection type and instance from the config
|
||||
delete transformedConfig.connection.type;
|
||||
delete transformedConfig.connection.instance;
|
||||
|
||||
return transformedConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user