Skip to content

Commit

Permalink
feat: better env variable(s) handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Mar 17, 2023
1 parent f498c4d commit fea6bf7
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/env/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2023-2023.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

export enum EnvironmentName {
DEVELOPMENT = 'development',
PRODUCTION = 'production',
TEST = 'test',
}
4 changes: 4 additions & 0 deletions src/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './constants';
export * from './module';
export * from './type';
export * from './utils';
49 changes: 49 additions & 0 deletions src/env/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { EnvironmentName } from './constants';
import type { Environment } from './type';
import { hasProcessEnv, readFromProcessEnv } from './utils';

let instance : Environment | undefined;

export function useEnv() : Environment;
export function useEnv<K extends keyof Environment>(key: K) : Environment[K];
export function useEnv(key?: string) : any {
if (typeof instance !== 'undefined') {
if (typeof key === 'string') {
return instance[key as keyof Environment];
}

return instance;
}

const seeds : string[] = [];
const seedKeys = ['DATABASE_SEEDER_SEEDS', 'TYPEORM_SEEDING_SEEDS'];
for (let i = 0; i < seedKeys.length; i++) {
if (!hasProcessEnv(seedKeys[i])) {
continue;
}

seeds.push(...readFromProcessEnv(seedKeys[i], '').split(','));
}

const factories : string[] = [];
const factoryKeys = ['DATABASE_SEEDER_FACTORIES', 'TYPEORM_SEEDING_FACTORIES'];
for (let i = 0; i < factoryKeys.length; i++) {
if (!hasProcessEnv(factoryKeys[i])) {
continue;
}

factories.push(...readFromProcessEnv(factoryKeys[i], '').split(','));
}

instance = {
env: readFromProcessEnv('NODE_ENV', 'development') as `${EnvironmentName}`,
seeds: seeds.filter((el) => el),
factories: factories.filter((el) => el),
};

if (typeof key === 'string') {
return instance[key as keyof Environment];
}

return instance;
}
7 changes: 7 additions & 0 deletions src/env/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { EnvironmentName } from './constants';

export interface Environment {
env: `${EnvironmentName}`,
seeds: string[],
factories: string[]
}
16 changes: 16 additions & 0 deletions src/env/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import process from 'node:process';
import { hasOwnProperty } from '../utils';

export function hasProcessEnv(key: string) : boolean {
return hasOwnProperty(process.env, key);
}

export function readFromProcessEnv(key: string) : string | undefined;
export function readFromProcessEnv<T>(key: string, alt: T) : T | string;
export function readFromProcessEnv<T>(key: string, alt?: T): any {
if (hasOwnProperty(process.env, key)) {
return process.env[key];
}

return alt;
}
37 changes: 24 additions & 13 deletions src/seeder/utils/options.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { hasOwnProperty } from '../../utils';
import type { DataSourceOptions } from 'typeorm';
import { useEnv } from '../../env';
import type { SeederOptions } from '../type';

export function setDefaultSeederOptions<T extends Partial<DataSourceOptions> & SeederOptions>(options: T) : T {
if (
!Array.isArray(options.factories) ||
options.factories.length === 0
) {
let factories = useEnv('factories');
if (factories.length === 0) {
factories = ['src/database/factories/**/*{.ts,.js}'];
}

export function setDefaultSeederOptions<T extends Record<string, any>>(options: T) : T {
if (!hasOwnProperty(options, 'factories')) {
Object.assign(options, {
factories: [
process.env.TYPEORM_SEEDING_FACTORIES ||
'src/database/factories/**/*{.ts,.js}',
],
factories,
});
}

if (!hasOwnProperty(options, 'seeds')) {
if (
!Array.isArray(options.seeds) ||
options.seeds.length === 0
) {
let seeds = useEnv('seeds');
if (seeds.length === 0) {
seeds = ['src/database/seeds/**/*{.ts,.js}'];
}

Object.assign(options, {
seeds: [
process.env.TYPEORM_SEEDS ||
process.env.TYPEORM_SEEDING_SEEDS ||
'src/database/seeds/**/*{.ts,.js}',
],
seeds,
});
}

Expand Down

0 comments on commit fea6bf7

Please sign in to comment.