支持在action中配置d1
This commit is contained in:
@@ -67,6 +67,15 @@ jobs:
|
|||||||
echo "INIT_CONFIG=${{ secrets.INIT_CONFIG }}" >> $GITHUB_ENV
|
echo "INIT_CONFIG=${{ secrets.INIT_CONFIG }}" >> $GITHUB_ENV
|
||||||
echo "CONFIG_SUBSCRIPTION_URL=${{ secrets.CONFIG_SUBSCRIPTION_URL }}" >> $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
|
- name: Update wrangler.toml with environment variables
|
||||||
run: |
|
run: |
|
||||||
# Function to append variable to wrangler.toml if it has a value
|
# Function to append variable to wrangler.toml if it has a value
|
||||||
|
|||||||
+17
-22
@@ -53,36 +53,31 @@ function getD1Adapter(): any {
|
|||||||
const isCloudflare = process.env.CF_PAGES === '1' || process.env.BUILD_TARGET === 'cloudflare';
|
const isCloudflare = process.env.CF_PAGES === '1' || process.env.BUILD_TARGET === 'cloudflare';
|
||||||
|
|
||||||
// 生产环境:Cloudflare Workers/Pages
|
// 生产环境:Cloudflare Workers/Pages
|
||||||
if (isCloudflare && typeof process !== 'undefined' && (process as any).env?.DB) {
|
if (isCloudflare) {
|
||||||
console.log('Using Cloudflare D1 database');
|
console.log('Using Cloudflare D1 database');
|
||||||
return new CloudflareD1Adapter((process as any).env.DB);
|
return new CloudflareD1Adapter((process as any).env.DB);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 开发环境:better-sqlite3
|
// 开发环境:better-sqlite3
|
||||||
try {
|
const Database = require('better-sqlite3');
|
||||||
const Database = require('better-sqlite3');
|
const path = require('path');
|
||||||
const path = require('path');
|
const fs = require('fs');
|
||||||
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)) {
|
if (!fs.existsSync(dbPath)) {
|
||||||
console.error('❌ SQLite database not found. Please run: npm run init:sqlite');
|
console.error('❌ SQLite database not found. Please run: npm run init:sqlite');
|
||||||
throw new Error('SQLite database not initialized');
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 单例存储实例
|
// 单例存储实例
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ main = ".open-next/worker.js"
|
|||||||
directory = ".open-next/assets"
|
directory = ".open-next/assets"
|
||||||
binding = "ASSETS"
|
binding = "ASSETS"
|
||||||
|
|
||||||
|
# D1 数据库绑定
|
||||||
|
[[d1_databases]]
|
||||||
|
binding = "DB"
|
||||||
|
database_name = "moontvplus"
|
||||||
|
database_id = "REPLACE_WITH_YOUR_D1_DATABASE_ID"
|
||||||
|
|
||||||
[vars]
|
[vars]
|
||||||
NODE_ENV = "production"
|
NODE_ENV = "production"
|
||||||
BUILD_TARGET = "cloudflare"
|
BUILD_TARGET = "cloudflare"
|
||||||
|
|||||||
Reference in New Issue
Block a user