Skip to content

Instantly share code, notes, and snippets.

@tgranqvist
Last active April 15, 2024 17:13
Show Gist options
  • Select an option

  • Save tgranqvist/834feb2bbb571bda3173429491cc14cd to your computer and use it in GitHub Desktop.

Select an option

Save tgranqvist/834feb2bbb571bda3173429491cc14cd to your computer and use it in GitHub Desktop.
Extract Knexfile params from environment, use defaults if unset

Knexfile from environment

Run the below as follows

SQLite with default memory db

TEST_DB_TYPE=sqlite3 node index.js

SQLite with specified path

TEST_DB_TYPE=sqlite3 TEST_DB_FILENAME=/tmp/test.db node index.js

Postgresql with parameters

TEST_DB_TYPE=postgres TEST_DB_HOST=mydb TEST_DB_USER=admin TEST_DB_PASSWORD=1234 TEST_DB_DBNAME=testing node index.js
const conf = {
client: process.env.TEST_DB_TYPE ?? "sqlite3",
connection: (process.env.TEST_DB_TYPE === "sqlite3"
? { filename: process.env.TEST_DB_FILENAME ?? ":memory:" }
: { host: process.env.TEST_DB_HOST, user: process.env.TEST_DB_USER,
password: process.env.TEST_DB_PASSWORD, database: process.env.TEST_DB_DBNAME
}
)
}
console.log(conf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment