-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from smartcontractkit/features/merge-env-vars-…
…for-db merge TYPEORM_* env vars onto existing ormconfig.json
- Loading branch information
Showing
4 changed files
with
25 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,3 @@ lib-cov | |
*.pid | ||
assets/* | ||
build/ | ||
ormconfig.production.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"name": "default", | ||
"type": "postgres", | ||
"host": "localhost", | ||
"port": 5432, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
import "reflect-metadata" | ||
import { createConnection, ConnectionOptions, ConnectionOptionsReader } from "typeorm" | ||
import seed from "./seed" | ||
import server from "./server" | ||
import 'reflect-metadata' | ||
import { createConnection } from 'typeorm' | ||
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions' | ||
import seed from './seed' | ||
import server from './server' | ||
import options from '../ormconfig.json' | ||
|
||
const nodeEnv = process.env.NODE_ENV || 'development' | ||
const ormconfigPath = `ormconfig.${nodeEnv}.json` | ||
const overridableKeys = ['host', 'port', 'username', 'password', 'database'] | ||
|
||
const loadOptions = async (configName: string): Promise<ConnectionOptions> => { | ||
const reader = new ConnectionOptionsReader({configName}) | ||
return reader.get("default") | ||
// Loads the following ENV vars, giving them precedence. | ||
// i.e. TYPEORM_PORT will replace "port" in ormconfig.json. | ||
const loadOptions = (): PostgresConnectionOptions => { | ||
let envOptions: { [key: string]: string } = {} | ||
for (let v of overridableKeys) { | ||
const envVar = process.env[`TYPEORM_${v.toUpperCase()}`] | ||
if (envVar) { | ||
envOptions[v] = envVar | ||
} | ||
} | ||
return { | ||
...options, | ||
...envOptions | ||
} as PostgresConnectionOptions | ||
} | ||
|
||
loadOptions(ormconfigPath).then(options => { | ||
createConnection(options).then(async dbConnection => { | ||
createConnection(loadOptions()) | ||
.then(async dbConnection => { | ||
seed(dbConnection) | ||
server(dbConnection) | ||
}) | ||
}).catch(error => console.log(error)) | ||
.catch(error => console.log(error)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters