支持在action中配置d1

This commit is contained in:
mtvpls
2026-01-30 15:46:43 +08:00
parent 465821a6c9
commit 0237b77447
3 changed files with 32 additions and 22 deletions

View File

@@ -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

View File

@@ -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);
}
// 单例存储实例

View File

@@ -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"