Problem: Create an Environment-Specific Database Client Factory

Easy
15 min
Build a factory that returns different database clients based on the environment.

Problem statement

Your team needs a database client factory that adapts to the environment—without cluttering code with if statements. In development, you use a fake in-memory DB client. In production, a PostgreSQL-like client is needed.

You’ll aim to allow any part of the system to call createDbClient() and retrieve the correct implementation, without needing to know about the underlying class or using conditional logic.

Goal

Implement a createDbClient(env) factory function that returns either FakeDbClient or PostgresClient depending on the env string ('dev' or 'prod').

Constraints

  • Do not use conditionals (if, switch, ternaries) to select which class to instantiate.

  • Do not instantiate the client directly outside the factory.

  • Only the factory knows which class to instantiate.

Sample output

The examples below illustrate what the output should look like:

const devClient = createDbClient('dev');
console.log(devClient.query('SELECT * FROM users'));
/* Expected output:
Querying (in-memory): SELECT * FROM users */
const prodClient = createDbClient('prod');
console.log(prodClient.query('SELECT * FROM orders'));
/* Expected output:
Querying (Postgres): SELECT * FROM orders */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Create an Environment-Specific Database Client Factory

Easy
15 min
Build a factory that returns different database clients based on the environment.

Problem statement

Your team needs a database client factory that adapts to the environment—without cluttering code with if statements. In development, you use a fake in-memory DB client. In production, a PostgreSQL-like client is needed.

You’ll aim to allow any part of the system to call createDbClient() and retrieve the correct implementation, without needing to know about the underlying class or using conditional logic.

Goal

Implement a createDbClient(env) factory function that returns either FakeDbClient or PostgresClient depending on the env string ('dev' or 'prod').

Constraints

  • Do not use conditionals (if, switch, ternaries) to select which class to instantiate.

  • Do not instantiate the client directly outside the factory.

  • Only the factory knows which class to instantiate.

Sample output

The examples below illustrate what the output should look like:

const devClient = createDbClient('dev');
console.log(devClient.query('SELECT * FROM users'));
/* Expected output:
Querying (in-memory): SELECT * FROM users */
const prodClient = createDbClient('prod');
console.log(prodClient.query('SELECT * FROM orders'));
/* Expected output:
Querying (Postgres): SELECT * FROM orders */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
class FakeDbClient {
query(sql) {
return `Querying (in-memory): ${sql}`;
}
}
class PostgresClient {
query(sql) {
return `Querying (Postgres): ${sql}`;
}
}
// Implement this
function createDbClient(env) {
// return appropriate client instance
}
// Example usage
const devClient = createDbClient('dev');
console.log(devClient.query('SELECT * FROM users'));
/* Expected output:
Querying (in-memory): SELECT * FROM users */
const prodClient = createDbClient('prod');
console.log(prodClient.query('SELECT * FROM orders'));
/* Expected output:
Querying (Postgres): SELECT * FROM orders */