Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getDialectByNameOrAlias and converted repo to use it. #5142

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/dialects/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { resolveClientNameWithAliases } = require('../util/helpers');

const dbNameToDialectLoader = Object.freeze({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why freeze it?

Copy link
Collaborator Author

@code-ape code-ape Apr 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just trying to follow repo standards. I saw Object.freeze(...) was used for other constants. Happy to remove if preferred or change to all caps :)

'better-sqlite3': () => require('./better-sqlite3'),
cockroachdb: () => require('./cockroachdb'),
mssql: () => require('./mssql'),
mysql: () => require('./mysql'),
mysql2: () => require('./mysql2'),
oracle: () => require('./oracle'),
oracledb: () => require('./oracledb'),
pgnative: () => require('./pgnative'),
postgres: () => require('./postgres'),
redshift: () => require('./redshift'),
sqlite3: () => require('./sqlite3'),
});

/**
* Gets the Dialect object with the given client name or throw an
* error if not found.
*
* NOTE: This is a replacement for prior practice of doing dynamic
* string construction for imports of Dialect objects.
*/
function getDialectByNameOrAlias(clientName) {
const resolvedClientName = resolveClientNameWithAliases(clientName);
const dialectLoader = dbNameToDialectLoader[resolvedClientName];
if (!dialectLoader) {
throw new Error(`Invalid clientName given: ${clientName}`);
}
return dialectLoader();
}

module.exports = {
getDialectByNameOrAlias,
};
5 changes: 2 additions & 3 deletions lib/knex-builder/internal/config-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Client = require('../../client');
const { SUPPORTED_CLIENTS } = require('../../constants');

const parseConnection = require('./parse-connection');
const { resolveClientNameWithAliases } = require('../../util/helpers');
const { getDialectByNameOrAlias } = require('../../dialects');

function resolveConfig(config) {
let Dialect;
Expand Down Expand Up @@ -34,8 +34,7 @@ function resolveConfig(config) {
);
}

const resolvedClientName = resolveClientNameWithAliases(clientName);
Dialect = require(`../../dialects/${resolvedClientName}/index.js`);
Dialect = getDialectByNameOrAlias(clientName);
}

// If config connection parameter is passed as string, try to parse it
Expand Down