Build without the optional mysql2 and sqlite3 drivers - #580
Merged
Merged
Conversation
The drivers are optionalDependencies and are import()ed lazily, so a
missing one is supposed to degrade to a log message. The build was
stricter than the runtime: `import type { Database } from 'sqlite3'`
makes TypeScript resolve the package while type checking, and so does
`import('sqlite3')`, even though that specifier is only evaluated at
runtime. Both fail with TS2307 when the driver is absent.
That is not hypothetical - adapter-tests-sqlite (24.x, macos-latest)
failed exactly this way on #578: npm skipped 38 optional packages on
that runner (sqlite3, cpu-features, diskusage, unix-dgram and their
trees) with nothing but a warning, and `npm run build:ts` then died on
sqlite3-client.ts lines 5 and 16.
Measured, one driver removed at a time:
without sqlite3 2 errors (sqlite3-client.ts:5, :16)
without mysql2 3 errors (connection-options.ts:1, mysql-client.ts:5, :13)
without mssql ok
without pg ok
Only the two drivers that ship their types inside the package break;
`pg` and `mssql` are covered by @types/pg and @types/mssql, which are
regular devDependencies and therefore always installed.
src/lib/optional-drivers.ts now declares the sliver of mysql2 and
sqlite3 this adapter actually touches, and importDriver() routes the
specifier through a `string` so the type checker cannot resolve it
either. The emitted code is unchanged in substance - `import(name)`
instead of `import('sqlite3')` - and tsc now succeeds with all four
drivers absent. Verified at runtime too: opening an in-memory SQLite
database, querying it and closing it still works, and a removed driver
still reports `Node.js DB driver "sqlite3" could not be loaded`
instead of crashing.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The DB drivers are
optionalDependenciesand areimport()ed lazily, so a missing one is meant to degrade to a log message. The build was stricter than the runtime:import type { Database } from 'sqlite3'makes TypeScript resolve the package while type checking, and so doesimport('sqlite3')— even though that specifier is only ever evaluated at runtime. Both fail with TS2307 when the driver is absent.Not hypothetical:
adapter-tests-sqlite (24.x, macos-latest)failed exactly this way on #578. npm skipped 38 optional packages on that runner (sqlite3,cpu-features,diskusage,unix-dgramand their trees) with nothing but a warning — 631 packages installed instead of 669 — andnpm run build:tsthen died:What breaks, measured
Removing one driver at a time from
node_modulesand runningtsc:sqlite3sqlite3-client.ts:5,:16)mysql2connection-options.ts:1,mysql-client.ts:5,:13)mssqlpgOnly the two drivers that ship their types inside the package break.
pgandmssqlare covered by@types/pgand@types/mssql, which are regulardevDependenciesand therefore always installed.The change
src/lib/optional-drivers.tsdeclares the sliver ofmysql2andsqlite3this adapter actually touches —Database.close/.all, the twonew Database(...)overloads,Connection.end, and the connection options we set — plusimportDriver(), which routes the specifier through astringso the type checker cannot resolve it either.The emitted code is unchanged in substance:
Verification
tscsucceeds with all four drivers absent, and with each one absent individually.SELECT 42, sqlite_version()and closing it all still work.sqlite3removed,openConnectionreportsNode.js DB driver "sqlite3" could not be loaded: ERR_MODULE_NOT_FOUNDinstead of crashing.check:tsandlint(root andsrc-admin) clean, 195 unit tests passing.Verified in a checkout outside
C:\pWork, because a straypackage.json/node_modulesthere makes every driver resolve from an ancestor directory and hides exactly this class of problem.🤖 Generated with Claude Code