feat: add explicit bun sqlite connector for Bun runtime deployments#3741
feat: add explicit bun sqlite connector for Bun runtime deployments#3741
bun sqlite connector for Bun runtime deployments#3741Conversation
…n support When building on Node.js but deploying to Bun runtime (e.g. Vercel with bun1.x), `process.versions.bun` is absent at build time, causing the module to fall back to `better-sqlite3` which fails at runtime in Bun. - Add `'bun'` to `SQLiteConnector` type union - Handle explicit `sqliteConnector: 'bun'` in `findBestSqliteAdapter()` - Auto-detect Bun runtime in Vercel preset via nitro config - Remove `better-sqlite3` type import from `database.server.ts` - Reorder auto-detection: explicit user choice always wins over runtime sniffing - Add build-time warning when `bun` connector is set on non-Bun build
|
@0x77dev is attempting to deploy a commit to the Nuxt Team on Vercel. A member of the Team first needs to authorize it. |
commit: |
📝 WalkthroughWalkthroughThis PR adds a new 'bun' SQLite connector option and updates docs with Bun-specific usage and Vercel/Nitro deployment guidance. Types are extended to include 'bun'. The Vercel preset setup now accepts a second Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/utils/database.ts (1)
174-176: Silent fallthrough whennativeSQLite is unavailable.When
sqliteConnector: 'native'is explicitly set butisNodeSqliteAvailable()returnsfalse, the function silently falls through to subsequent checks rather than warning the user. This differs from the'bun'case which warns when the runtime doesn't match.Consider adding a warning for consistency:
Proposed fix
if (opts.sqliteConnector === 'native' && isNodeSqliteAvailable()) { return opts.resolver ? opts.resolver.resolve('./runtime/internal/connectors/node-sqlite') : 'db0/connectors/node-sqlite' } + + if (opts.sqliteConnector === 'native') { + console.warn('[nuxt/content] `sqliteConnector: \'native\'` is set but Node.js native SQLite is not available (requires Node.js >= 22.5.0). Falling back to auto-detection.') + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/database.ts` around lines 174 - 176, The branch that returns the native node-sqlite connector when opts.sqliteConnector === 'native' currently assumes isNodeSqliteAvailable() is true and silently falls through if it is false; add a warning when the user explicitly requested 'native' but isNodeSqliteAvailable() is false (mirroring the existing 'bun' runtime warning). Concretely, inside the same conditional that checks opts.sqliteConnector === 'native' and calls isNodeSqliteAvailable(), detect the false case and emit a warning (using the same logging mechanism used for the 'bun' warning—e.g., processLogger.warn or console.warn) explaining that native sqlite is unavailable and that the resolver will fall back to the default; then continue with the existing fallback behavior (do not throw). Reference symbols: opts.sqliteConnector, isNodeSqliteAvailable(), and the resolver.resolve('./runtime/internal/connectors/node-sqlite') return path.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/utils/database.ts`:
- Around line 174-176: The branch that returns the native node-sqlite connector
when opts.sqliteConnector === 'native' currently assumes isNodeSqliteAvailable()
is true and silently falls through if it is false; add a warning when the user
explicitly requested 'native' but isNodeSqliteAvailable() is false (mirroring
the existing 'bun' runtime warning). Concretely, inside the same conditional
that checks opts.sqliteConnector === 'native' and calls isNodeSqliteAvailable(),
detect the false case and emit a warning (using the same logging mechanism used
for the 'bun' warning—e.g., processLogger.warn or console.warn) explaining that
native sqlite is unavailable and that the resolver will fall back to the
default; then continue with the existing fallback behavior (do not throw).
Reference symbols: opts.sqliteConnector, isNodeSqliteAvailable(), and the
resolver.resolve('./runtime/internal/connectors/node-sqlite') return path.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
docs/content/docs/1.getting-started/3.configuration.mdsrc/presets/vercel.tssrc/runtime/internal/database.server.tssrc/types/module.tssrc/utils/database.ts
…e when targeting Bun When `sqliteConnector: 'bun'` is set but the build runs on Node.js (e.g. Vercel CI), the local build-time database and `#content/local-adapter` must not use `bun:sqlite` — Node.js cannot resolve the `bun:` protocol. Only the runtime adapter (`#content/adapter`) now uses the bun connector. The build-time paths fall back to the default Node.js-compatible connector.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/module.ts (1)
194-201: Consider centralizing Bun-on-Node connector normalization.The same fallback rule is duplicated in two places. A small helper would reduce drift risk.
♻️ Suggested refactor
+function normalizeBuildSqliteConnector(connector?: ModuleOptions['experimental']['sqliteConnector']) { + return (connector === 'bun' && !process.versions.bun) ? undefined : connector +} + -const localSqliteConnector = (sqliteConnector === 'bun' && !process.versions.bun) ? undefined : sqliteConnector +const localSqliteConnector = normalizeBuildSqliteConnector(sqliteConnector) -const buildConnector = (requestedConnector === 'bun' && !process.versions.bun) ? undefined : requestedConnector +const buildConnector = normalizeBuildSqliteConnector(requestedConnector)Also applies to: 288-291
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/module.ts` around lines 194 - 201, Duplicate Bun-on-Node connector fallback logic (where sqliteConnector is mapped to localSqliteConnector using (sqliteConnector === 'bun' && !process.versions.bun) ? undefined : sqliteConnector) should be extracted to a small helper function (e.g., normalizeSqliteConnector or getCompatibleSqliteConnector) and used in both places before calling resolveDatabaseAdapter; update the calls that currently compute localSqliteConnector and the other occurrence at lines ~288-291 to call this helper, passing the computed sqliteConnector and letting the helper return the connector compatible with the current runtime.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/module.ts`:
- Around line 194-201: Duplicate Bun-on-Node connector fallback logic (where
sqliteConnector is mapped to localSqliteConnector using (sqliteConnector ===
'bun' && !process.versions.bun) ? undefined : sqliteConnector) should be
extracted to a small helper function (e.g., normalizeSqliteConnector or
getCompatibleSqliteConnector) and used in both places before calling
resolveDatabaseAdapter; update the calls that currently compute
localSqliteConnector and the other occurrence at lines ~288-291 to call this
helper, passing the computed sqliteConnector and letting the helper return the
connector compatible with the current runtime.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
docs/content/docs/1.getting-started/3.configuration.mdsrc/module.tssrc/utils/database.ts
🔗 Linked issue
Related: oven-sh/bun#4290 (
better-sqlite3not supported in Bun)❓ Type of change
📚 Description
When building on Node.js but deploying to a Bun runtime (e.g. Vercel with
bun1.x),process.versions.bunis absent at build time. This causesfindBestSqliteAdapter()to fall back tobetter-sqlite3, which fails at runtime because Bun doesn't support it:The
bun:sqliteconnector already exists in the codebase (src/runtime/internal/connectors/bun-sqlite.ts), but there was no way to explicitly select it when cross-compiling from Node.js to Bun.Changes:
'bun'to theSQLiteConnectortype ('native' | 'sqlite3' | 'better-sqlite3' | 'bun')sqliteConnector: 'bun'infindBestSqliteAdapter()nitro.vercel.functions.runtimestarts with'bun'better-sqlite3type import fromdatabase.server.ts(replaced with inline type to make runtime code connector-agnostic)process.versions.bunsniffing'bun'connector is set but the build runs on Node.jsUsage:
🧪 Testing
Automated:
pnpm test— full vitest suite passes (24 files, 181+ tests, 0 regressions)pnpm test:bun— all 6 bun-specific tests passManual (Bun 1.3.9):
getLocalDatabase()with no config on Bun correctly routes tobun:sqlitesqliteConnector: 'bun'correctly routes tobun:sqlitesqliteConnector: 'better-sqlite3'on Bun correctly attemptsbetter-sqlite3(not overridden by Bun auto-detect)bun:sqlite: CREATE TABLE, INSERT, SELECT, UPDATE, DROP — all work with the exact_content_infoand_content_docstable schemas from the original errorbun1.xruntime from nitro config, setssqliteConnector: 'bun'sqliteConnectorvaluenodejs20.xruntime📝 Checklist