Revert "fix:去除KV检查和KV提示"

This reverts commit 15055b6293.
This commit is contained in:
初衷
2025-08-20 08:43:14 +08:00
parent 15055b6293
commit ccfcffdfed
2 changed files with 131 additions and 22 deletions

View File

@@ -16,13 +16,127 @@ export function createDatabaseAdapter(env) {
// 使用D1数据库
console.log('Using D1 Database');
return new D1Database(env.DB);
} else if (env.img_url && typeof env.img_url.get === 'function') {
// 回退到KV存储
console.log('Using KV Storage (fallback)');
return new KVAdapter(env.img_url);
} else {
console.error('D1 database not configured. Please configure D1 database (env.DB)');
console.error('No database configured. Please configure either D1 (env.DB) or KV (env.img_url)');
return null;
}
}
// KV适配器已移除 - 此D1版本仅支持D1数据库
/**
* KV适配器类
* 保持与原有KV接口的兼容性
*/
class KVAdapter {
constructor(kv) {
this.kv = kv;
}
// 直接代理到KV的方法
async put(key, value, options) {
options = options || {};
return await this.kv.put(key, value, options);
}
async get(key) {
return await this.kv.get(key);
}
async getWithMetadata(key) {
return await this.kv.getWithMetadata(key);
}
async delete(key) {
return await this.kv.delete(key);
}
async list(options) {
options = options || {};
return await this.kv.list(options);
}
// 为了兼容性,添加一些别名方法
async putFile(fileId, value, options) {
return await this.put(fileId, value, options);
}
async getFile(fileId) {
const result = await this.getWithMetadata(fileId);
return result;
}
async getFileWithMetadata(fileId) {
return await this.getWithMetadata(fileId);
}
async deleteFile(fileId) {
return await this.delete(fileId);
}
async listFiles(options) {
return await this.list(options);
}
async putSetting(key, value) {
return await this.put(key, value);
}
async getSetting(key) {
return await this.get(key);
}
async deleteSetting(key) {
return await this.delete(key);
}
async listSettings(options) {
return await this.list(options);
}
async putIndexOperation(operationId, operation) {
const key = 'manage@index@operation_' + operationId;
return await this.put(key, JSON.stringify(operation));
}
async getIndexOperation(operationId) {
const key = 'manage@index@operation_' + operationId;
const result = await this.get(key);
return result ? JSON.parse(result) : null;
}
async deleteIndexOperation(operationId) {
const key = 'manage@index@operation_' + operationId;
return await this.delete(key);
}
async listIndexOperations(options) {
const listOptions = Object.assign({}, options, {
prefix: 'manage@index@operation_'
});
const result = await this.list(listOptions);
// 转换格式以匹配D1Database的返回格式
const operations = [];
for (const item of result.keys) {
const operationData = await this.get(item.name);
if (operationData) {
const operation = JSON.parse(operationData);
operations.push({
id: item.name.replace('manage@index@operation_', ''),
type: operation.type,
timestamp: operation.timestamp,
data: operation.data,
processed: false // KV中没有这个字段默认为false
});
}
}
return operations;
}
}
/**
* 获取数据库实例的便捷函数
@@ -33,7 +147,7 @@ export function createDatabaseAdapter(env) {
export function getDatabase(env) {
var adapter = createDatabaseAdapter(env);
if (!adapter) {
throw new Error('D1 database not configured. Please configure D1 database (env.DB) in Cloudflare Pages Dashboard.');
throw new Error('Database not configured. Please configure D1 database (env.DB) or KV storage (env.img_url).');
}
return adapter;
}
@@ -45,12 +159,14 @@ export function getDatabase(env) {
*/
export function checkDatabaseConfig(env) {
var hasD1 = env.DB && typeof env.DB.prepare === 'function';
var hasKV = env.img_url && typeof env.img_url.get === 'function';
return {
hasD1: hasD1,
hasKV: hasKV,
usingD1: hasD1,
configured: hasD1,
database: hasD1 ? 'D1' : 'None'
usingKV: !hasD1 && hasKV,
configured: hasD1 || hasKV
};
}
@@ -73,9 +189,14 @@ export async function healthCheck(env) {
try {
var db = getDatabase(env);
// D1健康检查 - 尝试查询一个简单的表
var stmt = db.db.prepare('SELECT 1 as test');
await stmt.first();
if (config.usingD1) {
// D1健康检查 - 尝试查询一个简单的表
var stmt = db.db.prepare('SELECT 1 as test');
await stmt.first();
} else {
// KV健康检查 - 尝试列出键
await db.list({ limit: 1 });
}
return {
healthy: true,

View File

@@ -125,17 +125,8 @@ async function checkDatabaseConfigMiddleware(context) {
return new Response(
JSON.stringify({
success: false,
error: "D1数据库未配置 / D1 Database not configured",
message: "请在 Cloudflare Pages Dashboard 中配置 D1 数据库绑定。 / Please configure D1 database binding in Cloudflare Pages Dashboard.",
details: {
hasD1: dbConfig.hasD1,
database: dbConfig.database,
steps: [
"1. Go to Pages Dashboard → Your Project → Settings → Functions",
"2. Add D1 database binding: Variable name = 'DB', Database = 'imgbed-database'",
"3. Redeploy your project"
]
}
error: "数据库未配置 / Database not configured",
message: "请配置 D1 数据库 (env.DB) 或 KV 存储 (env.img_url)。 / Please configure D1 database (env.DB) or KV storage (env.img_url)."
}),
{
status: 500,
@@ -146,9 +137,6 @@ async function checkDatabaseConfigMiddleware(context) {
);
}
// 记录使用D1数据库
console.log('Using D1 database');
// 继续执行
return await context.next();
}