Alexandru Circus Alexandru Circus - 1 year ago 97
TypeScript Question

Overwrite static member by using another file

I'm converting a js project to typeScript and I want to overwrite some constants static members by using a local/untracked_by_git file.

My original constants file:

export default class Constants{
static MYSQL_HOST = 'some_host';
static MYSQL_PORT = 0000;
static MYSQL_DATABASE = 'prod_database_name';
static MYSQL_USER = 'user';
static MYSQL_PWD = 'password';

I want a local file (untracked by versioning system) that is able to overwrite any of this fields (in order to connect to some particular db, different than my collegues)

Answer Source

following the advice from @mario-alexandro-santini

export default class Constants{
    static MYSQL_HOST = process.env.MYSQL_HOST || 'some_host';
    static MYSQL_PORT = process.env.MYSQL_PORT 0000;
    static MYSQL_DATABASE = process.env.MYSQL_DATABASE || 'prod_database_name';
    static MYSQL_USER = process.env.MYSQL_USER || 'user';
    static MYSQL_PWD = process.env.MYSQL_PWD || 'password';

assuming you are running in a UNIX based environment you can have a bash file lets call it add that file name to the .gitignore file and it should contain something like this.

MYSQL_HOST="Other values"

when you run the app just do something like ./ && <the command you did before>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download