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 5 commits 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
8 changes: 6 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 All @@ -79,6 +78,7 @@
"micromatch": "^4.0.8",
"minimatch": "^10.0.1",
"nuxt-component-meta": "^0.11.0",
"nypm": "^0.6.0",
"ohash": "^2.0.11",
"pathe": "^2.0.3",
"pkg-types": "^2.1.0",
Expand All @@ -101,7 +101,8 @@
"peerDependencies": {
"@electric-sql/pglite": "*",
"@libsql/client": "*",
"sqlite3": "*"
"sqlite3": "*",
"better-sqlite3": "11.x"
},
"peerDependenciesMeta": {
"@electric-sql/pglite": {
Expand All @@ -112,6 +113,9 @@
},
"sqlite3": {
"optional": true
},
"better-sqlite3": {
"optional": true
}
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

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

33 changes: 27 additions & 6 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mkdir } from 'node:fs/promises'
import type { Connector } from 'db0'
import type { Resolver } from '@nuxt/kit'
import { addDependency } from 'nypm'
import cloudflareD1Connector from 'db0/connectors/cloudflare-d1'
import { isAbsolute, join, dirname } from 'pathe'
import { isWebContainer } from '@webcontainer/env'
Expand Down Expand Up @@ -168,18 +169,19 @@ async function findBestSqliteAdapter(opts: { sqliteConnector?: SQLiteConnector }
}

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

return 'db0/connectors/better-sqlite3'
}

if (isWebContainer()) {
if (!await isSqlite3PackageInstalled()) {
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)
}
await ensurePackageInstalled('sqlite3')

return 'db0/connectors/sqlite3'
}

await ensurePackageInstalled('better-sqlite3')

return 'db0/connectors/better-sqlite3'
}

Expand Down Expand Up @@ -217,9 +219,28 @@ function isNodeSqliteAvailable() {
}
}

async function isSqlite3PackageInstalled() {
async function ensurePackageInstalled(pkg: string) {
if (!await isPackageInstalled(pkg)) {
logger.error(`Nuxt Content requires \`${pkg}\` module to operate.`)

const confirm = await logger.prompt(`Do you want to install \`${pkg}\` package?`, {
type: 'confirm',
name: 'confirm',
initial: true,
})

if (!confirm) {
logger.error(`Nuxt Content requires \`${pkg}\` module to operate. Please install \`${pkg}\` package manually and try again. \`npm install ${pkg}\``)
process.exit(1)
}

await addDependency(pkg)
}
}

async function isPackageInstalled(packageName: string) {
try {
await import('sqlite3')
await import(packageName)
return true
}
catch {
Expand Down