Skip to content

Commit

Permalink
Added getDialectByNameOrAlias and converted repo to use it. (#5142)
Browse files Browse the repository at this point in the history
  • Loading branch information
code-ape committed Apr 22, 2022
1 parent 2511e74 commit 4e6a547
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
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({
'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

0 comments on commit 4e6a547

Please sign in to comment.