添加key查看器
This commit is contained in:
@@ -46,4 +46,72 @@ export const kvLocalProvider = {
|
||||
return formatError("保存本地数据失败:" + error);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取本地存储的键名列表
|
||||
* @param {Object} options - 查询选项
|
||||
* @param {string} options.sortBy - 排序字段,默认为 "key"
|
||||
* @param {string} options.sortDir - 排序方向,"asc" 或 "desc",默认为 "asc"
|
||||
* @param {number} options.limit - 每页返回的记录数,默认为 100
|
||||
* @param {number} options.skip - 跳过的记录数,默认为 0
|
||||
* @returns {Promise<Object>} 包含键名列表和分页信息的响应对象
|
||||
*
|
||||
* 返回值示例:
|
||||
* {
|
||||
* keys: ["key1", "key2", "key3"],
|
||||
* total_rows: 150,
|
||||
* current_page: {
|
||||
* limit: 10,
|
||||
* skip: 0,
|
||||
* count: 10
|
||||
* },
|
||||
* load_more: null // 本地存储不需要分页URL
|
||||
* }
|
||||
*/
|
||||
async loadKeys(options = {}) {
|
||||
try {
|
||||
const db = await initDB();
|
||||
const transaction = db.transaction(["kv"], "readonly");
|
||||
const store = transaction.objectStore("kv");
|
||||
|
||||
// 获取所有键名
|
||||
const allKeys = await store.getAllKeys();
|
||||
|
||||
// 设置默认参数
|
||||
const {
|
||||
sortBy = "key",
|
||||
sortDir = "asc",
|
||||
limit = 100,
|
||||
skip = 0
|
||||
} = options;
|
||||
|
||||
// 排序键名(本地存储只支持按键名排序)
|
||||
const sortedKeys = allKeys.sort((a, b) => {
|
||||
if (sortDir === "desc") {
|
||||
return b.localeCompare(a);
|
||||
}
|
||||
return a.localeCompare(b);
|
||||
});
|
||||
|
||||
// 应用分页
|
||||
const totalRows = sortedKeys.length;
|
||||
const paginatedKeys = sortedKeys.slice(skip, skip + limit);
|
||||
|
||||
// 构建响应数据
|
||||
const responseData = {
|
||||
keys: paginatedKeys,
|
||||
total_rows: totalRows,
|
||||
current_page: {
|
||||
limit,
|
||||
skip,
|
||||
count: paginatedKeys.length
|
||||
},
|
||||
load_more: null // 本地存储不需要分页URL
|
||||
};
|
||||
|
||||
return formatResponse(responseData);
|
||||
} catch (error) {
|
||||
return formatError("获取本地键名列表失败:" + error.message);
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user