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
knex case? #473
Comments
Yes, it's correct that this construct doesn't work with esbuild. It looks like this code is only used in a fallback for when the // If user provided Client constructor as a parameter, use it
else if (
typeof config.client === 'function' &&
config.client.prototype instanceof Client
) {
Dialect = config.client;
}
// If neither applies, let's assume user specified name of a client or dialect as a string
else {
const clientName = config.client || config.dialect;
if (!SUPPORTED_CLIENTS.includes(clientName)) {
throw new Error(
`knex: Unknown configuration option 'client' value ${clientName}. Note that it is case-sensitive, check documentation for supported values.`
);
}
const resolvedClientName = resolveClientNameWithAliases(clientName);
Dialect = require(`./dialects/${resolvedClientName}/index.js`);
} So would it be possible to just have the caller provide the client and avoid triggering this code path? |
You're right:
makes my ts-node based test case run as well.j Using esbuild I then run into fatal error 'pg-query-stream' after adding I fail with 'pg-native' not found. pg should have JS implementation and the native should be an option. Installing that as well makes it work. So while it's not perfect it does the job for me right now. Thank you. Esbuild is a game changer it drops the time to run a test case from 2.8 to 0.6 seconds. |
For typescript, that would be: import Knex from "knex"
import KnexPostgres from "knex/lib/dialects/postgres" // for esbuild
const knex = Knex({
client: KnexPostgres,
connection: "...",
}) and declare module "knex/lib/dialects/postgres" {
import { Knex } from "knex"
const client: typeof Knex.Client
export default client
} |
I don't know if I'm running a different config for TS or something, but I had to slightly modify the above import KnexPostgres = require("knex/lib/dialects/postgres"); // for esbuild and declare module "knex/lib/dialects/postgres" {
import { Knex } from "knex";
const client: typeof Knex.Client;
export = client;
} Probably to do with the build target (I didn't investigate further after it started working) |
Hmm... this doesn't work for me? I'm using it in a serverless project (with serverless-esbuild), and when I try to spin up the offline server I'm getting a slew of errors around these requires despite passing only the client I require. |
@aardvarkk I marked |
Yes, I did the same. Thanks @Noor0! |
I did this but I have already the problem of others connectors. This is my esbuild config in serverless : {
external : [
'better-sqlite3',
'tedious',
'mysql',
'oracledb',
],
bundle : true,
minify : true,
target : 'node18',
packager : 'npm',
sourcemap : true,
sourcesContent : false,
} This is the errors :
|
My issue was CDK Amazon Lambda Node.js Library was bundling using esbuild under the hood and causing the above errors. Fixed using |
Knex contains this code:
where dialects is mysql/postgresql whatever. esbuild warns that dynamic imports are not supported.
So which is the best way to fix this?
The text was updated successfully, but these errors were encountered: