Convex KV 组件
一个分级的,为Convex订购的密钥价值商店. 将相干锅炉取而代之 get编号 :set API,以自动TTL和递归前缀删除为主.
安装
npm install @hamzasaleemorg/convex-kv
// convex/convex.config.ts
import { defineApp } from "convex/server";
import convexKv from "@hamzasaleemorg/convex-kv/convex.config.js";
const app = defineApp();
app.use(convexKv);
export default app;
快速启动: 过期邀请链接 QQ
管理一次性的魔法链接或邀请代码,而不克扣您的主要策略或写出清除逻辑.
// convex/invites.ts
import { kvClientFactory } from "@hamzasaleemorg/convex-kv";
import { components } from "./_generated/api";
import { mutation } from "./_generated/server";
import { v } from "convex/values";
const kv = kvClientFactory(components.convexKv);
const invites = kv.use<{ email: string; role: string }>(["invites"]);
export const createInvite = mutation({
args: { email: v.string(), role: v.string() },
handler: async (ctx, args) => {
const inviteCode = Math.random().toString(36).substring(7);
// Store invite that automatically EXPIRES in 48 hours
await invites.set(ctx, [inviteCode], {
email: args.email,
role: args.role
}, { ttl: 48 * 60 * 60 * 1000 });
return inviteCode;
},
});
export const acceptInvite = mutation({
args: { code: v.string() },
handler: async (ctx, args) => {
// If the link is older than 48hrs, .get() returns null automatically
const invite = await invites.get(ctx, [args.code]);
if (!invite) throw new Error("Invite invalid or expired");
// ... create membership logic ...
await invites.delete(ctx, [args.code]);
},
});
为什么用这个?
- 0舍马维护: 立即存储任何 JSON 序列数据 。 没有新的表格,没有索引等待.
- 有组织的等级制度: 使用数组密钥, 如
["org", orgId, "settings"]自然分离数据. - 自动 TTL: 设置
ttl任何密钥上,一旦失效,就会从询问中消失. - 有序扫描: 密钥以词典来存储. 立即用前缀扫描数百万个密钥.
- 插件删除: 用
deleteAll(["prefix"])它自动处理背景中的大数据集.
API 参考
初始化客户端
您可以使用全局客户端或将其范围限制到特定的递归命名空间:
const kv = kvClientFactory(components.convexKv);
// Everything sent through 'users' will stay in that folder
const users = kv.use<{ email: string }>(["users"]);
核心业务
| 方法 | 说明 |
|---|---|
get(key) | 返回值或 null 如果丢失/过期. |
set(key, val, options?) | 存储值 。 选项 : ttl (单位:百万美元), expiresAt, (中文(简体) ), metadata。 。 。 . |
has(key) | 快速布尔检查是否存在( 过期返回为假) . |
delete(key) | 删除特定的密钥. |
等级范围操作( R)
| 方法 | 说明 |
|---|---|
list(prefix, options?) | 在命名空间内进行分层扫描 . |
getAll(prefix) | 返回前缀下的所有条目( 最大 1000). |
deleteAll(prefix) | 递归式删除。在背景中安全清除整个前缀树. |
开发和测试
npm install
npm run dev # Starts the KV Explorer & Build Watcher
npm test # Runs full suite (Unit + Component Integration)
许可证
阿帕奇 - 2.0