-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added getDialectByNameOrAlias and converted repo to use it.
Previous this repo has imported Dialect objects via dynamically generating a string to require. This does not work well with compilers like ncc. To solve this and make for a better development experience we add the function `getDialectByNameOrAlias` to safely import Dialects while making requirement paths static for better compiler and bundler experiences.
- Loading branch information
Showing
2 changed files
with
37 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters