I want to write two different connections in my code: one is for production and the other is for development. I also want to choose between these connections with a .env file. I tried several ways, and I found this solution:
// Modules
import dotenv from "dotenv";
import knex from "knex";
// Environment variables
dotenv.config();
const { PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DB, IS_APP_IN_PRODUCTION } = process.env;
// Connections
const connectionsList = [
knex({
client: "pg",
connection: {
host: PG_HOST,
port: (PG_PORT as any),
user: PG_USER,
password: PG_PASSWORD,
database: PG_DB
}
}),
knex({
client: "sqlite3",
connection: {
filename: "./development/dev.sqlite3",
database: "dev-db"
}
})
];
// Setting the connection
const connection = connectionsList[IS_APP_IN_PRODUCTION == "true" ? 0 : 1];
// Export
export default connection;
This works and solves my problem, but in my point of view, this isn't the best solution. I don't like the idea of using an array to organize my connections; because of that, my first try was to use object literals to organize, in this way:
// Modules
import dotenv from "dotenv";
import knex from "knex";
// Environment variables
dotenv.config();
const { PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DB, APP_MODE } = process.env;
// Connections
const connectionsList = {
production: knex({
client: "pg",
connection: {
host: PG_HOST,
port: (PG_PORT as any),
user: PG_USER,
password: PG_PASSWORD,
database: PG_DB
}
}),
development: knex({
client: "sqlite3",
connection: {
filename: "./development/dev.sqlite3",
database: "dev-db"
}
})
};
// Setting the connection
const connection = connectionsList[APP_MODE as string]; // APP_MODE is a string that can be "production" or "development"
// Export
export default connection;
But doing this way gives me this error:
The element implicitly has a type 'any' because the expression of type 'string' cannot be used for the index type '{ production: Knex<any, unknown[]>; development: Knex<any, unknown[]>; }'. No index signature with a parameter of type 'string' was found in type '{ production: Knex<any, unknown[]>; development: Knex<any, unknown[]>; }'.
Is there a way to solve this problem? If yes, how? And if no, how should I write my code?