Skip to content

chore(deps): make better-sqlite3 dependency optional #3349

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@shikijs/langs": "^3.3.0",
"@sqlite.org/sqlite-wasm": "3.49.1-build4",
"@webcontainer/env": "^1.1.1",
"better-sqlite3": "^11.9.1",
"c12": "^3.0.3",
"chokidar": "^4.0.3",
"consola": "^3.4.2",
Expand Down Expand Up @@ -101,7 +100,8 @@
"peerDependencies": {
"@electric-sql/pglite": "*",
"@libsql/client": "*",
"sqlite3": "*"
"sqlite3": "*",
"better-sqlite3": "11.x"
},
"peerDependenciesMeta": {
"@electric-sql/pglite": {
Expand All @@ -112,6 +112,9 @@
},
"sqlite3": {
"optional": true
},
"better-sqlite3": {
"optional": true
}
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,22 @@ function findBestSqliteAdapter(opts: { sqliteConnector?: SQLiteConnector }) {
}

if (opts.sqliteConnector === 'better-sqlite3') {
requireBetterSqlite3()

return 'db0/connectors/better-sqlite3'
}

if (isWebContainer()) {
if (!isSqlite3PackageInstalled()) {
if (!isPackageInstalled('sqlite3')) {
logger.error('Nuxt Content requires `sqlite3` module to work in WebContainer environment. Please run `npm install sqlite3` to install it and try again.')
process.exit(1)
}

return 'db0/connectors/sqlite3'
}

requireBetterSqlite3()

return 'db0/connectors/better-sqlite3'
}

Expand Down Expand Up @@ -217,9 +221,16 @@ function isNodeSqliteAvailable() {
}
}

function isSqlite3PackageInstalled() {
function requireBetterSqlite3() {
if (!isPackageInstalled('better-sqlite3')) {
logger.error('Nuxt Content requires `better-sqlite3` module to work in Node environment. Please run `npm install better-sqlite3` to install it and try again.')
process.exit(1)
}
}

function isPackageInstalled(packageName: string) {
try {
require.resolve('sqlite3')
require.resolve(packageName)
return true
}
catch {
Expand Down