Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template ENV vars in config #524

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions packages/cli/src/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { DBConfigArgs, getEnvDBConfig } from './config';

describe('getEnvDBConfig', () => {
const env = {
...process.env,
};

beforeEach(() => {
// Default PG
process.env.PGHOST = 'pg_host';
process.env.PGUSER = 'pg_user';
process.env.PGPASSWORD = 'pg_password';
process.env.PGDATABASE = 'pg_db_name';
process.env.PGPORT = '1111';
process.env.PGURI = 'pg_uri';
process.env.DATABASE_URL = 'pg_url';

// Custom
process.env.URI_ENV = 'host_from_env';
process.env.HOST_ENV = 'host_from_env';
process.env.USER_ENV = 'user_from_env';
process.env.PASSWORD_ENV = 'password_from_env';
process.env.DB_NAME_ENV = 'db_name_from_env';
process.env.PORT_ENV = '2222';
process.env.URI_ENV = 'uri_from_env';
});

afterEach(() => {
process.env = env;
});

test('Parses template ENV', () => {
const dbConfig: Partial<DBConfigArgs> = {
host: '{{HOST_ENV}}',
user: '{{USER_ENV}}',
password: '{{PASSWORD_ENV}}',
dbName: '{{DB_NAME_ENV}}',
port: '{{PORT_ENV}}',
uri: '{{URI_ENV}}',
};
const parsedConfig = getEnvDBConfig(dbConfig);

expect(parsedConfig).toEqual({
host: 'host_from_env',
user: 'user_from_env',
password: 'password_from_env',
dbName: 'db_name_from_env',
port: 2222,
uri: 'uri_from_env',
});
});

test('Parses default ENV', () => {
const dbConfig: Partial<DBConfigArgs> = {};
const parsedConfig = getEnvDBConfig(dbConfig);

expect(parsedConfig).toEqual({
host: 'pg_host',
user: 'pg_user',
password: 'pg_password',
dbName: 'pg_db_name',
port: 1111,
uri: 'pg_uri',
});
});

test('Parses default ENV port=DATABASE_URL', () => {
process.env.PGURI = undefined;

const dbConfig: Partial<DBConfigArgs> = {};
const parsedConfig = getEnvDBConfig(dbConfig);

expect(parsedConfig).toEqual({
host: 'pg_host',
user: 'pg_user',
password: 'pg_password',
dbName: 'pg_db_name',
port: 1111,
uri: 'pg_url',
});
});

test('Parses default ENV with invalid templates', () => {
// All invalid templates
const dbConfig: Partial<DBConfigArgs> = {
host: '{{{HOST_ENV}}',
user: '{{USER_ENV}}}',
password: 'invalid',
dbName: '',
port: '1234',
uri: '_',
};
const parsedConfig = getEnvDBConfig(dbConfig);

expect(parsedConfig).toEqual({
host: undefined,
user: undefined,
password: 'pg_password',
dbName: 'pg_db_name',
port: 1111,
uri: 'pg_uri',
});
});
});
60 changes: 49 additions & 11 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const configParser = t.type({
t.type({
host: t.union([t.string, t.undefined]),
password: t.union([t.string, t.undefined]),
port: t.union([t.number, t.undefined]),
port: t.union([t.number, t.string, t.undefined]),
user: t.union([t.string, t.undefined]),
dbName: t.union([t.string, t.undefined]),
ssl: t.union([t.UnknownRecord, t.boolean, t.undefined]),
Expand All @@ -67,8 +67,42 @@ const configParser = t.type({
]),
});

export type DBConfigArgs = {
host: string;
user: string;
password: string;
dbName: string;
port: number | string;
ssl: tls.ConnectionOptions | boolean;
uri: string;
};

export type IConfig = typeof configParser._O;

function parseEnvTemplate(input?: string): string | undefined {
const templateStringRegex = new RegExp('{{\\w+}}', 'g');
const result = input ? templateStringRegex.exec(input) : undefined;
return result?.input?.substring(2, result.input.length - 2);
}

export function getEnvDBConfig(dBConfig: Partial<DBConfigArgs>) {
const host = parseEnvTemplate(dBConfig.host) ?? 'PGHOST';
const user = parseEnvTemplate(dBConfig.user) ?? 'PGUSER';
const password = parseEnvTemplate(dBConfig.password) ?? 'PGPASSWORD';
const dbName = parseEnvTemplate(dBConfig.dbName) ?? 'PGDATABASE';
const port = parseEnvTemplate(dBConfig?.port?.toString()) ?? 'PGPORT';
const uri = parseEnvTemplate(dBConfig.uri) ?? 'PGURI';

return {
host: process.env[host],
user: process.env[user],
password: process.env[password],
dbName: process.env[dbName],
port: process.env[port] ? Number(process.env[port]) : undefined,
uri: process.env[uri] ?? process.env.DATABASE_URL,
};
}

export interface ParsedConfig {
db: {
host: string;
Expand Down Expand Up @@ -162,15 +196,6 @@ export function parseConfig(
port: 5432,
};

const envDBConfig = {
host: process.env.PGHOST,
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
dbName: process.env.PGDATABASE,
port: process.env.PGPORT ? Number(process.env.PGPORT) : undefined,
uri: process.env.PGURI ?? process.env.DATABASE_URL,
};

const {
db = defaultDBConfig,
dbUrl: configDbUri,
Expand All @@ -182,6 +207,8 @@ export function parseConfig(
typesOverrides,
} = configObject as IConfig;

const envDBConfig = getEnvDBConfig(db);

// CLI connectionUri flag takes precedence over the env and config one
const dbUri = argConnectionUri || envDBConfig.uri || configDbUri;

Expand All @@ -196,7 +223,18 @@ export function parseConfig(
);
}

const finalDBConfig = merge(defaultDBConfig, db, urlDBConfig, envDBConfig);
// The port may be a template string
const dbConfig = {
...db,
port: typeof db.port === 'string' ? Number(db.port) : db.port,
};

const finalDBConfig = merge(
defaultDBConfig,
dbConfig,
urlDBConfig,
envDBConfig,
);

const parsedTypesOverrides: Record<string, Partial<TypeDefinition>> = {};

Expand Down