Skip to content

feat: add explicit bun sqlite connector for Bun runtime deployments#3741

Open
0x77dev wants to merge 3 commits intonuxt:mainfrom
0x77dev:feat/bun-sqlite-connector
Open

feat: add explicit bun sqlite connector for Bun runtime deployments#3741
0x77dev wants to merge 3 commits intonuxt:mainfrom
0x77dev:feat/bun-sqlite-connector

Conversation

@0x77dev
Copy link

@0x77dev 0x77dev commented Mar 3, 2026

🔗 Linked issue

Related: oven-sh/bun#4290 (better-sqlite3 not supported in Bun)

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

When building on Node.js but deploying to a Bun runtime (e.g. Vercel with bun1.x), process.versions.bun is absent at build time. This causes findBestSqliteAdapter() to fall back to better-sqlite3, which fails at runtime because Bun doesn't support it:

'better-sqlite3' is not yet supported in Bun.
Track the status in https://github.com/oven-sh/bun/issues/4290
In the meantime, you could try bun:sqlite which has a similar API.

The bun:sqlite connector 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:

  • Add 'bun' to the SQLiteConnector type ('native' | 'sqlite3' | 'better-sqlite3' | 'bun')
  • Handle sqliteConnector: 'bun' in findBestSqliteAdapter()
  • Auto-detect Bun runtime in the Vercel preset when nitro.vercel.functions.runtime starts with 'bun'
  • Remove better-sqlite3 type import from database.server.ts (replaced with inline type to make runtime code connector-agnostic)
  • Reorder auto-detection so explicit user choices always take priority over process.versions.bun sniffing
  • Add a build-time warning when 'bun' connector is set but the build runs on Node.js

Usage:

// Explicit (for cross-compilation: build on Node, deploy to Bun)
export default defineNuxtConfig({
  content: {
    experimental: { sqliteConnector: 'bun' },
  },
})
// Zero-config for Vercel with Bun runtime
export default defineNuxtConfig({
  nitro: {
    vercel: {
      functions: { runtime: 'bun1.x' },
    },
  },
})

🧪 Testing

Automated:

  • pnpm test — full vitest suite passes (24 files, 181+ tests, 0 regressions)
  • pnpm test:bun — all 6 bun-specific tests pass

Manual (Bun 1.3.9):

  • Auto-detect: getLocalDatabase() with no config on Bun correctly routes to bun:sqlite
  • Explicit: sqliteConnector: 'bun' correctly routes to bun:sqlite
  • Override respected: sqliteConnector: 'better-sqlite3' on Bun correctly attempts better-sqlite3 (not overridden by Bun auto-detect)
  • Full CRUD cycle through bun:sqlite: CREATE TABLE, INSERT, SELECT, UPDATE, DROP — all work with the exact _content_info and _content_docs table schemas from the original error
  • Vercel preset: auto-detects bun1.x runtime from nitro config, sets sqliteConnector: 'bun'
  • Vercel preset: does NOT override explicit user-set sqliteConnector value
  • Vercel preset: does NOT trigger for nodejs20.x runtime

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

…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
@vercel
Copy link

vercel bot commented Mar 3, 2026

@0x77dev is attempting to deploy a commit to the Nuxt Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 3, 2026

npm i https://pkg.pr.new/@nuxt/content@3741

commit: 8a7f3b3

@coderabbitai
Copy link

coderabbitai bot commented Mar 3, 2026

📝 Walkthrough

Walkthrough

This 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 nuxt parameter and auto-enables the Bun connector when Nitro uses Bun. Database resolution logic was refactored to support explicit 'bun', runtime auto-detection, and runtime fallback warnings; local/build-time adapter selection was centralized. A server runtime file loosened a SQLite options type used during config construction. No exported function signatures (other than the preset setup parameter) were added or removed.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: add explicit bun sqlite connector for Bun runtime deployments' directly and clearly summarizes the main change: adding explicit Bun sqlite connector support for Bun runtime environments.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, explaining the problem, implementation details, usage examples, and testing performed.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/utils/database.ts (1)

174-176: Silent fallthrough when native SQLite is unavailable.

When sqliteConnector: 'native' is explicitly set but isNodeSqliteAvailable() returns false, 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

📥 Commits

Reviewing files that changed from the base of the PR and between ca99792 and 658b731.

📒 Files selected for processing (5)
  • docs/content/docs/1.getting-started/3.configuration.md
  • src/presets/vercel.ts
  • src/runtime/internal/database.server.ts
  • src/types/module.ts
  • src/utils/database.ts

0x77dev added 2 commits March 3, 2026 00:38
…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.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 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

📥 Commits

Reviewing files that changed from the base of the PR and between 658b731 and 57ae932.

📒 Files selected for processing (3)
  • docs/content/docs/1.getting-started/3.configuration.md
  • src/module.ts
  • src/utils/database.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant