mirror of
https://github.com/ZeroCatDev/ClassworksKV.git
synced 2025-09-03 16:19:24 +00:00
feat(kv): 添加获取键名列表功能
新增 listKeysOnly 方法用于获取指定命名空间下的键名列表,并添加对应的路由接口 /:namespace/_keys 支持分页和排序查询。返回结果包含键名数组、总数和分页信息,便于前端展示大量键名时使用。
This commit is contained in:
parent
f1dba22f75
commit
22838ee71a
54
routes/kv.js
54
routes/kv.js
@ -234,6 +234,60 @@ router.delete(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /:namespace/_keys
|
||||||
|
* 获取指定命名空间下的键名列表(分页,不包括内容)
|
||||||
|
*/
|
||||||
|
router.get(
|
||||||
|
"/:namespace/_keys",
|
||||||
|
checkRestrictedUUID,
|
||||||
|
readAuthMiddleware,
|
||||||
|
errors.catchAsync(async (req, res, next) => {
|
||||||
|
const { namespace } = req.params;
|
||||||
|
const { sortBy, sortDir, limit, skip } = req.query;
|
||||||
|
|
||||||
|
// 构建选项
|
||||||
|
const options = {
|
||||||
|
sortBy: sortBy || "key",
|
||||||
|
sortDir: sortDir || "asc",
|
||||||
|
limit: limit ? parseInt(limit) : 100,
|
||||||
|
skip: skip ? parseInt(skip) : 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const keys = await kvStore.listKeysOnly(namespace, options);
|
||||||
|
|
||||||
|
// 获取总记录数
|
||||||
|
const totalRows = await kvStore.count(namespace);
|
||||||
|
|
||||||
|
// 构建响应对象
|
||||||
|
const response = {
|
||||||
|
keys: keys,
|
||||||
|
total_rows: totalRows,
|
||||||
|
current_page: {
|
||||||
|
limit: options.limit,
|
||||||
|
skip: options.skip,
|
||||||
|
count: keys.length,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// 如果还有更多数据,添加load_more字段
|
||||||
|
const nextSkip = options.skip + options.limit;
|
||||||
|
if (nextSkip < totalRows) {
|
||||||
|
const baseUrl = `${req.baseUrl}/${namespace}/_keys`;
|
||||||
|
const queryParams = new URLSearchParams({
|
||||||
|
sortBy: options.sortBy,
|
||||||
|
sortDir: options.sortDir,
|
||||||
|
limit: options.limit,
|
||||||
|
skip: nextSkip,
|
||||||
|
}).toString();
|
||||||
|
|
||||||
|
response.load_more = `${baseUrl}?${queryParams}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json(response);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /:namespace
|
* GET /:namespace
|
||||||
* 获取指定命名空间下的所有键名及元数据列表
|
* 获取指定命名空间下的所有键名及元数据列表
|
||||||
|
@ -160,6 +160,36 @@ class KVStore {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定命名空间下的键名列表(不包括内容)
|
||||||
|
* @param {string} namespace - 命名空间
|
||||||
|
* @param {object} options - 查询选项
|
||||||
|
* @returns {Array} 键名列表
|
||||||
|
*/
|
||||||
|
async listKeysOnly(namespace, options = {}) {
|
||||||
|
const { sortBy = "key", sortDir = "asc", limit = 100, skip = 0 } = options;
|
||||||
|
|
||||||
|
// 构建排序条件
|
||||||
|
const orderBy = {};
|
||||||
|
orderBy[sortBy] = sortDir.toLowerCase();
|
||||||
|
|
||||||
|
// 查询以命名空间开头的所有键,只选择键名
|
||||||
|
const items = await prisma.kVStore.findMany({
|
||||||
|
where: {
|
||||||
|
namespace: namespace,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
key: true,
|
||||||
|
},
|
||||||
|
orderBy,
|
||||||
|
take: limit,
|
||||||
|
skip: skip,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 只返回键名数组
|
||||||
|
return items.map((item) => item.key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 统计指定命名空间下的键值对数量
|
* 统计指定命名空间下的键值对数量
|
||||||
* @param {string} namespace - 命名空间
|
* @param {string} namespace - 命名空间
|
||||||
|
Loading…
x
Reference in New Issue
Block a user