chore: rework solution

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-11-28 13:49:53 +01:00
parent 4bdfcf05ba
commit 35d6263803
3 changed files with 36 additions and 17 deletions
+23 -10
View File
@@ -377,23 +377,29 @@ 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' | 'pg+google-cloudsql';
client: 'better-sqlite3' | 'sqlite3' | 'pg';
/**
* Base database connection string, or object with individual connection properties
* @visibility secret
*/
connection:
| string
| {
/**
* The specific config for cloudsql connections
*/
type: 'cloudsql';
/**
* The instance connection name for the cloudsql instance, e.g. `project:region:instance`
*/
instance: 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
*/
@@ -440,28 +446,35 @@ export interface Config {
plugin?: {
[pluginId: string]: {
/** Database client override */
client?: 'better-sqlite3' | 'sqlite3' | 'pg' | 'pg+google-cloudsql';
client?: 'better-sqlite3' | 'sqlite3' | 'pg';
/**
* Database connection string or Knex object override
* @visibility secret
*/
connection?:
| string
| {
/**
* The specific config for cloudsql connections
*/
type: 'cloudsql';
/**
* The instance connection name for the cloudsql instance, e.g. `project:region:instance`
*/
instance: 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.
@@ -138,12 +138,13 @@ describe('postgres', () => {
});
});
it('uses the correct config when using pg+google-cloudsql', async () => {
it('uses the correct config when using cloudsql', async () => {
expect(
await buildPgDatabaseConfig(
new ConfigReader({
client: 'pg+google-cloudsql',
client: 'pg',
connection: {
type: 'cloudsql',
user: 'ben@gke.com',
instance: 'project:region:instance',
port: 5423,
@@ -155,7 +156,6 @@ describe('postgres', () => {
client: 'pg',
connection: {
user: 'ben@gke.com',
instance: 'project:region:instance',
port: 5423,
database: 'other_db',
},
@@ -174,8 +174,9 @@ describe('postgres', () => {
expect(
await buildPgDatabaseConfig(
new ConfigReader({
client: 'pg+google-cloudsql',
client: 'pg',
connection: {
type: 'cloudsql',
user: 'ben@gke.com',
instance: 'project:region:instance',
port: 5423,
@@ -187,7 +188,6 @@ describe('postgres', () => {
client: 'pg',
connection: {
user: 'ben@gke.com',
instance: 'project:region:instance',
port: 5423,
stream: mockStream,
database: 'other_db',
@@ -77,7 +77,7 @@ export async function buildPgDatabaseConfig(
overrides,
);
if (config.client === 'pg+google-cloudsql') {
if (config.connection?.type === 'cloudsql') {
if (!config.connection.instance) {
throw new Error('Missing instance connection name for Cloud SQL');
}
@@ -94,7 +94,7 @@ export async function buildPgDatabaseConfig(
authType: AuthTypes.IAM,
});
return {
const cloudsqlConfig = {
...config,
client: 'pg',
connection: {
@@ -102,6 +102,12 @@ export async function buildPgDatabaseConfig(
...clientOpts,
},
};
// Trim additional properties from the connection object passed to knex
delete cloudsqlConfig.connection.type;
delete cloudsqlConfig.connection.instance;
return cloudsqlConfig;
}
return config;