Skip to content
Merged
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
14 changes: 0 additions & 14 deletions graphql/env/__fixtures__/env.base.json

This file was deleted.

5 changes: 0 additions & 5 deletions graphql/env/__fixtures__/env.dedupe.json

This file was deleted.

54 changes: 0 additions & 54 deletions graphql/env/__fixtures__/env.keys.json

This file was deleted.

1 change: 1 addition & 0 deletions graphql/env/__tests__/__snapshots__/merge.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports[`getEnvOptions merges pgpm defaults, graphql defaults, config, env, and
"awsSecretKey": "minioadmin",
"bucketName": "test-bucket",
"minioEndpoint": "http://localhost:9000",
"provider": "minio",
},
"db": {
"connections": {
Expand Down
45 changes: 23 additions & 22 deletions graphql/env/__tests__/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@ import { getEnvOptions } from '../src/merge';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {
applyEnvFixture,
loadEnvFixture,
loadEnvKeys,
restoreEnv,
snapshotAndClearEnv
} from '../../../pgpm/env/__tests__/test-utils';

const writeConfig = (dir: string, config: Record<string, unknown>): void => {
fs.writeFileSync(path.join(dir, 'pgpm.json'), JSON.stringify(config, null, 2));
};

const fixturesDir = path.join(__dirname, '..', '__fixtures__');
const envKeys = loadEnvKeys(fixturesDir, 'env.keys.json');

describe('getEnvOptions', () => {
let envSnapshot: Record<string, string | undefined>;
let tempDir = '';

beforeEach(() => {
envSnapshot = snapshotAndClearEnv(envKeys);
});

afterEach(() => {
restoreEnv(envSnapshot);
if (tempDir) {
fs.rmSync(tempDir, { recursive: true, force: true });
tempDir = '';
Expand Down Expand Up @@ -56,8 +40,20 @@ describe('getEnvOptions', () => {
}
});

const fixtureEnv = loadEnvFixture(fixturesDir, 'env.base.json');
applyEnvFixture(fixtureEnv);
const testEnv: NodeJS.ProcessEnv = {
PGHOST: 'env-host',
PGUSER: 'env-user',
GRAPHILE_SCHEMA: 'env_schema_a,env_schema_b',
FEATURES_SIMPLE_INFLECTION: 'true',
FEATURES_POSTGIS: 'false',
API_ENABLE_META: 'true',
API_IS_PUBLIC: 'true',
API_EXPOSED_SCHEMAS: 'public,app',
API_META_SCHEMAS: 'env_meta1,env_meta2',
API_ANON_ROLE: 'env_anon',
API_ROLE_NAME: 'env_role',
API_DEFAULT_DATABASE_ID: 'env_db'
};

const result = getEnvOptions(
{
Expand All @@ -81,7 +77,8 @@ describe('getEnvOptions', () => {
defaultDatabaseId: 'override_db'
}
},
tempDir
tempDir,
testEnv
);

expect(result).toMatchSnapshot();
Expand All @@ -99,8 +96,11 @@ describe('getEnvOptions', () => {
}
});

const fixtureEnv = loadEnvFixture(fixturesDir, 'env.dedupe.json');
applyEnvFixture(fixtureEnv);
const testEnv: NodeJS.ProcessEnv = {
GRAPHILE_SCHEMA: 'shared_schema,env_schema',
API_EXPOSED_SCHEMAS: 'shared,env_schema',
API_META_SCHEMAS: 'meta_public,env_meta'
};

const result = getEnvOptions(
{
Expand All @@ -112,7 +112,8 @@ describe('getEnvOptions', () => {
metaSchemas: ['env_meta', 'override_meta']
}
},
tempDir
tempDir,
testEnv
);

expect(result.graphile?.schema).toEqual([
Expand Down
7 changes: 5 additions & 2 deletions graphql/env/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const parseEnvBoolean = (val?: string): boolean | undefined => {
return ['true', '1', 'yes'].includes(val.toLowerCase());
};

export const getGraphQLEnvVars = (): Partial<ConstructiveOptions> => {
/**
* @param env - Environment object to read from (defaults to process.env for backwards compatibility)
*/
export const getGraphQLEnvVars = (env: NodeJS.ProcessEnv = process.env): Partial<ConstructiveOptions> => {
const {
GRAPHILE_SCHEMA,

Expand All @@ -24,7 +27,7 @@ export const getGraphQLEnvVars = (): Partial<ConstructiveOptions> => {
API_ANON_ROLE,
API_ROLE_NAME,
API_DEFAULT_DATABASE_ID,
} = process.env;
} = env;

return {
graphile: {
Expand Down
11 changes: 8 additions & 3 deletions graphql/env/src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ import { getGraphQLEnvVars } from './env';
*
* This is the main entry point for Constructive packages that need
* both core PGPM options and GraphQL/Graphile options.
*
* @param overrides - Runtime overrides to apply last
* @param cwd - Working directory for config file resolution
* @param env - Environment object to read from (defaults to process.env for backwards compatibility)
*/
export const getEnvOptions = (
overrides: Partial<ConstructiveOptions> = {},
cwd: string = process.cwd()
cwd: string = process.cwd(),
env: NodeJS.ProcessEnv = process.env
): ConstructiveOptions => {
// Get core PGPM options (includes pgpmDefaults + config + core env vars)
const coreOptions = getPgpmEnvOptions({}, cwd);
const coreOptions = getPgpmEnvOptions({}, cwd, env);

// Get GraphQL-specific env vars
const graphqlEnvOptions = getGraphQLEnvVars();
const graphqlEnvOptions = getGraphQLEnvVars(env);

// Load config again to get any GraphQL-specific config
// Config files can contain Constructive options (graphile, features, api)
Expand Down
13 changes: 0 additions & 13 deletions pgpm/env/__fixtures__/env.base.json

This file was deleted.

4 changes: 0 additions & 4 deletions pgpm/env/__fixtures__/env.dedupe.json

This file was deleted.

43 changes: 0 additions & 43 deletions pgpm/env/__fixtures__/env.keys.json

This file was deleted.

42 changes: 20 additions & 22 deletions pgpm/env/__tests__/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@ import { pgpmDefaults, PgpmOptions } from '@pgpmjs/types';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {
applyEnvFixture,
loadEnvFixture,
loadEnvKeys,
restoreEnv,
snapshotAndClearEnv
} from './test-utils';

const writeConfig = (dir: string, config: Record<string, unknown>): void => {
fs.writeFileSync(path.join(dir, 'pgpm.json'), JSON.stringify(config, null, 2));
};

const fixturesDir = path.join(__dirname, '..', '__fixtures__');
const envKeys = loadEnvKeys(fixturesDir, 'env.keys.json');

describe('getConnEnvOptions', () => {
describe('roles resolution', () => {
it('should always return roles with default values when no overrides provided', () => {
Expand Down Expand Up @@ -120,16 +110,10 @@ describe('getConnEnvOptions', () => {
});

describe('getEnvOptions', () => {
let envSnapshot: Record<string, string | undefined>;
let tempDir = '';
type PgpmOptionsWithPackages = PgpmOptions & { packages?: string[] };

beforeEach(() => {
envSnapshot = snapshotAndClearEnv(envKeys);
});

afterEach(() => {
restoreEnv(envSnapshot);
if (tempDir) {
fs.rmSync(tempDir, { recursive: true, force: true });
tempDir = '';
Expand Down Expand Up @@ -159,8 +143,19 @@ describe('getEnvOptions', () => {
}
});

const fixtureEnv = loadEnvFixture(fixturesDir, 'env.base.json');
applyEnvFixture(fixtureEnv);
const testEnv: NodeJS.ProcessEnv = {
PGHOST: 'env-host',
PGPORT: '6543',
PGUSER: 'env-user',
PGPASSWORD: 'env-pass',
DB_PREFIX: 'env-',
DB_CONNECTIONS_APP_PASSWORD: 'env-app-pass',
DB_CONNECTIONS_ADMIN_USER: 'env-admin-user',
PORT: '7777',
DEPLOYMENT_FAST: 'false',
JOBS_SUPPORT_ANY: 'false',
JOBS_SUPPORTED: 'alpha,beta'
};

const result = getEnvOptions(
{
Expand All @@ -178,7 +173,8 @@ describe('getEnvOptions', () => {
cache: true
}
},
tempDir
tempDir,
testEnv
);

expect(result).toMatchSnapshot();
Expand All @@ -201,8 +197,10 @@ describe('getEnvOptions', () => {
packages: ['testing/*', 'packages/*']
});

const fixtureEnv = loadEnvFixture(fixturesDir, 'env.dedupe.json');
applyEnvFixture(fixtureEnv);
const testEnv: NodeJS.ProcessEnv = {
DB_EXTENSIONS: 'postgis,pgcrypto',
JOBS_SUPPORTED: 'beta,gamma,delta'
};

const overrides: PgpmOptionsWithPackages = {
db: {
Expand All @@ -219,7 +217,7 @@ describe('getEnvOptions', () => {
packages: ['testing/*', 'extensions/*']
};

const result = getEnvOptions(overrides, tempDir) as PgpmOptionsWithPackages;
const result = getEnvOptions(overrides, tempDir, testEnv) as PgpmOptionsWithPackages;

expect(result.db?.extensions).toEqual([
'uuid',
Expand Down
Loading