From 0237b77447f8b6c188ad1d50adb32b64c3abf228 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Fri, 30 Jan 2026 15:46:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9C=A8action=E4=B8=AD?= =?UTF-8?q?=E9=85=8D=E7=BD=AEd1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cloudflare-deploy.yml | 9 ++++++ src/lib/db.ts | 39 +++++++++++-------------- wrangler.toml | 6 ++++ 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/.github/workflows/cloudflare-deploy.yml b/.github/workflows/cloudflare-deploy.yml index 697a172..d3a0855 100644 --- a/.github/workflows/cloudflare-deploy.yml +++ b/.github/workflows/cloudflare-deploy.yml @@ -67,6 +67,15 @@ jobs: echo "INIT_CONFIG=${{ secrets.INIT_CONFIG }}" >> $GITHUB_ENV echo "CONFIG_SUBSCRIPTION_URL=${{ secrets.CONFIG_SUBSCRIPTION_URL }}" >> $GITHUB_ENV + - name: Replace D1 Database ID in wrangler.toml + run: | + if [ -n "${{ secrets.D1_DATABASE_ID }}" ]; then + sed -i 's/REPLACE_WITH_YOUR_D1_DATABASE_ID/${{ secrets.D1_DATABASE_ID }}/g' wrangler.toml + echo "D1 Database ID replaced successfully" + else + echo "D1_DATABASE_ID secret not set, skipping replacement" + fi + - name: Update wrangler.toml with environment variables run: | # Function to append variable to wrangler.toml if it has a value diff --git a/src/lib/db.ts b/src/lib/db.ts index a5c4fec..7c19745 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -53,36 +53,31 @@ function getD1Adapter(): any { const isCloudflare = process.env.CF_PAGES === '1' || process.env.BUILD_TARGET === 'cloudflare'; // 生产环境:Cloudflare Workers/Pages - if (isCloudflare && typeof process !== 'undefined' && (process as any).env?.DB) { + if (isCloudflare) { console.log('Using Cloudflare D1 database'); return new CloudflareD1Adapter((process as any).env.DB); } // 开发环境:better-sqlite3 - try { - const Database = require('better-sqlite3'); - const path = require('path'); - const fs = require('fs'); + const Database = require('better-sqlite3'); + const path = require('path'); + const fs = require('fs'); - const dbPath = path.join(process.cwd(), '.data', 'moontv.db'); + const dbPath = path.join(process.cwd(), '.data', 'moontv.db'); - // 检查数据库文件是否存在 - if (!fs.existsSync(dbPath)) { - console.error('❌ SQLite database not found. Please run: npm run init:sqlite'); - throw new Error('SQLite database not initialized'); - } - - const db = new Database(dbPath); - db.pragma('journal_mode = WAL'); // 启用 WAL 模式提升性能 - - console.log('Using SQLite database (development mode)'); - console.log('Database location:', dbPath); - - return new SQLiteAdapter(db); - } catch (err) { - console.error('Failed to initialize SQLite:', err); - throw err; + // 检查数据库文件是否存在 + if (!fs.existsSync(dbPath)) { + console.error('❌ SQLite database not found. Please run: npm run init:sqlite'); + throw new Error('SQLite database not initialized'); } + + const db = new Database(dbPath); + db.pragma('journal_mode = WAL'); // 启用 WAL 模式提升性能 + + console.log('Using SQLite database (development mode)'); + console.log('Database location:', dbPath); + + return new SQLiteAdapter(db); } // 单例存储实例 diff --git a/wrangler.toml b/wrangler.toml index 71dfc64..1ac78ef 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -9,6 +9,12 @@ main = ".open-next/worker.js" directory = ".open-next/assets" binding = "ASSETS" +# D1 数据库绑定 +[[d1_databases]] +binding = "DB" +database_name = "moontvplus" +database_id = "REPLACE_WITH_YOUR_D1_DATABASE_ID" + [vars] NODE_ENV = "production" BUILD_TARGET = "cloudflare"