Feat:适配D1数据库

This commit is contained in:
MarSeventh
2025-08-23 12:01:19 +08:00
parent 9e64ed9fd1
commit f182f41b55
2 changed files with 6 additions and 6 deletions

View File

@@ -12,14 +12,14 @@ import { D1Database } from './d1Database.js';
*/
export function createDatabaseAdapter(env) {
// 检查是否配置了D1数据库
if (env.DB && typeof env.DB.prepare === 'function') {
if (env.img_d1 && typeof env.img_d1.prepare === 'function') {
// 使用D1数据库
return new D1Database(env.DB);
return new D1Database(env.img_d1);
} else if (env.img_url && typeof env.img_url.get === 'function') {
// 回退到KV存储
return new KVAdapter(env.img_url);
} else {
console.error('No database configured. Please configure either D1 (env.DB) or KV (env.img_url)');
console.error('No database configured. Please configure either D1 (env.img_d1) or KV (env.img_url)');
return null;
}
}
@@ -145,7 +145,7 @@ class KVAdapter {
export function getDatabase(env) {
var adapter = createDatabaseAdapter(env);
if (!adapter) {
throw new Error('Database not configured. Please configure D1 database (env.DB) or KV storage (env.img_url).');
throw new Error('Database not configured. Please configure D1 database (env.img_d1) or KV storage (env.img_url).');
}
return adapter;
}
@@ -156,7 +156,7 @@ export function getDatabase(env) {
* @returns {Object} 配置信息
*/
export function checkDatabaseConfig(env) {
var hasD1 = env.DB && typeof env.DB.prepare === 'function';
var hasD1 = env.img_d1 && typeof env.img_d1.prepare === 'function';
var hasKV = env.img_url && typeof env.img_url.get === 'function';
return {

View File

@@ -123,7 +123,7 @@ export async function checkDatabaseConfig(context) {
JSON.stringify({
success: false,
error: "数据库未配置 / Database not configured",
message: "请配置 D1 数据库 (env.DB) 或 KV 存储 (env.img_url)。 / Please configure D1 database (env.DB) or KV storage (env.img_url)."
message: "请配置 D1 数据库 (env.img_d1) 或 KV 存储 (env.img_url)。 / Please configure D1 database (env.img_d1) or KV storage (env.img_url)."
}),
{
status: 500,