Skip to content

Commit 5fdf995

Browse files
committed
Uniform env variable to match type orm and add options to overwrite other aspects of env.ts
1 parent c7902ae commit 5fdf995

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

.env.example

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,27 @@ AUTH_ROUTE=http://localhost:3333/tokeninfo
2323
#
2424
# DATABASE
2525
#
26-
DB_TYPE=mysql
27-
DB_HOST=localhost
28-
DB_PORT=3306
29-
DB_USERNAME=root
30-
DB_PASSWORD=
31-
DB_DATABASE=my_database
32-
DB_SYNCHRONIZE=false
33-
DB_LOGGING=false
26+
TYPEORM_CONNECTION=mysql
27+
TYPEORM_HOST=localhost
28+
TYPEORM_PORT=3306
29+
TYPEORM_USERNAME=root
30+
TYPEORM_PASSWORD=
31+
TYPEORM_DATABASE=my_database
32+
TYPEORM_SYNCHRONIZE=false
33+
TYPEORM_LOGGING=false
34+
35+
#
36+
# Additional settings (optional as defaults are in env.ts)
37+
#
38+
# TYPEORM_MIGRATIONS=
39+
# TYPEORM_MIGRATIONS_DIR=
40+
# TYPEORM_ENTITIES=
41+
# TYPEORM_SUBSCRIBERS=
42+
# CONTROLLERS=
43+
# MIDDLEWARES=
44+
# INTERCEPTORS=
45+
# QUERIES=
46+
# MUTATIONS=
3447

3548
#
3649
# GraphQL

src/env.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as dotenv from 'dotenv';
22
import * as path from 'path';
33

44
import * as pkg from '../package.json';
5-
import { getOsEnv, normalizePort, toBool, toNumber } from './lib/env';
5+
import { getOsEnv, getOsEnvArray, normalizePort, toBool, toNumber } from './lib/env';
66

77
/**
88
* Load .env file or for tests the .env.test file.
@@ -27,15 +27,15 @@ export const env = {
2727
port: normalizePort(process.env.PORT || getOsEnv('APP_PORT')),
2828
banner: toBool(getOsEnv('APP_BANNER')),
2929
dirs: {
30-
migrations: [path.relative(path.join(process.cwd()), path.join(__dirname, 'database/migrations/**/*.ts'))],
31-
migrationsDir: path.relative(path.join(process.cwd()), path.join(__dirname, 'database/migrations')),
32-
entities: [path.relative(path.join(process.cwd()), path.join(__dirname, 'api/models/**/*{.js,.ts}'))],
33-
subscribers: [path.join(__dirname, 'api/subscribers/**/*Subscriber{.js,.ts}')],
34-
controllers: [path.join(__dirname, 'api/controllers/**/*Controller{.js,.ts}')],
35-
middlewares: [path.join(__dirname, 'api/middlewares/**/*Middleware{.js,.ts}')],
36-
interceptors: [path.join(__dirname, 'api/interceptors/**/*Interceptor{.js,.ts}')],
37-
queries: [path.join(__dirname, 'api/queries/**/*Query{.js,.ts}')],
38-
mutations: [path.join(__dirname, 'api/mutations/**/*Mutation{.js,.ts}')],
30+
migrations: getOsEnvArray('TYPEORM_MIGRATIONS') || [path.relative(path.join(process.cwd()), path.join(__dirname, 'database/migrations/**/*.ts'))],
31+
migrationsDir: getOsEnv('TYPEORM_MIGRATIONS_DIR') || path.relative(path.join(process.cwd()), path.join(__dirname, 'database/migrations')),
32+
entities: getOsEnvArray('TYPEORM_ENTITIES') || [path.relative(path.join(process.cwd()), path.join(__dirname, 'api/models/**/*{.js,.ts}'))],
33+
subscribers: getOsEnvArray('TYPEORM_SUBSCRIBERS') || [path.join(__dirname, 'api/subscribers/**/*Subscriber{.js,.ts}')],
34+
controllers: getOsEnvArray('CONTROLLERS') || [path.join(__dirname, 'api/controllers/**/*Controller{.js,.ts}')],
35+
middlewares: getOsEnvArray('MIDDLEWARES') || [path.join(__dirname, 'api/middlewares/**/*Middleware{.js,.ts}')],
36+
interceptors: getOsEnvArray('INTERCEPTORS') || [path.join(__dirname, 'api/interceptors/**/*Interceptor{.js,.ts}')],
37+
queries: getOsEnvArray('QUERIES') || [path.join(__dirname, 'api/queries/**/*Query{.js,.ts}')],
38+
mutations: getOsEnvArray('MUTATIONS') || [path.join(__dirname, 'api/mutations/**/*Mutation{.js,.ts}')],
3939
},
4040
},
4141
log: {
@@ -47,14 +47,14 @@ export const env = {
4747
route: getOsEnv('AUTH_ROUTE'),
4848
},
4949
db: {
50-
type: getOsEnv('DB_TYPE'),
51-
host: getOsEnv('DB_HOST'),
52-
port: toNumber(getOsEnv('DB_PORT')),
53-
username: getOsEnv('DB_USERNAME'),
54-
password: getOsEnv('DB_PASSWORD'),
55-
database: getOsEnv('DB_DATABASE'),
56-
synchronize: toBool(getOsEnv('DB_SYNCHRONIZE')),
57-
logging: toBool(getOsEnv('DB_LOGGING')),
50+
type: getOsEnv('TYPEORM_CONNECTION'),
51+
host: getOsEnv('TYPEORM_HOST'),
52+
port: toNumber(getOsEnv('TYPEORM_PORT')),
53+
username: getOsEnv('TYPEORM_USERNAME'),
54+
password: getOsEnv('TYPEORM_PASSWORD'),
55+
database: getOsEnv('TYPEORM_DATABASE'),
56+
synchronize: toBool(getOsEnv('TYPEORM_SYNCHRONIZE')),
57+
logging: toBool(getOsEnv('TYPEORM_LOGGING')),
5858
},
5959
graphql: {
6060
enabled: toBool(getOsEnv('GRAPHQL_ENABLED')),

src/lib/env/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export function getOsEnv(key: string): string {
22
return process.env[key] as string;
33
}
44

5+
export function getOsEnvArray(value: string, delimiter: string = ','): string[] | boolean {
6+
return value && value.split(delimiter) || false;
7+
}
8+
59
export function toNumber(value: string): number {
610
return parseInt(value, 10);
711
}

0 commit comments

Comments
 (0)