KV Storage
Key Value Storage is a data storage that allows you to store and retrieve data globally.
With unstorage
Installation
- Install
unstorage.
Install with NPM
sh
npm i unstorageInstall with Bun
sh
bun add unstorage- Setup the oRPC context and the KV storage.
ts
// src/lib/kv.ts
import { createStorage } from "unstorage";
export const kv = createStorage();ts
// src/lib/orpc.ts
import { os } from "@orpc/server";
import type { Storage } from "unstorage";
export const base = os
.$context<{ headers: Headers }>()
.$context<{ headers: Headers; kv: Storage }>()
.response({
OK: {},
CREATED: {},
})
.errors({
BAD_REQUEST: {},
UNAUTHORIZED: {},
FORBIDDEN: {},
NOT_FOUND: {},
TOO_MANY_REQUESTS: {},
INTERNAL_SERVER_ERROR: {},
SERVICE_UNAVAILABLE: {},
});
export { type } from "@orpc/server";ts
// src/server.ts
import { RPCHandler } from "@orpc/server/fetch";
import { router } from "$oxide";
import { kv } from "$lib/kv";
const orpcHandler = new RPCHandler(router);
export default {
async fetch(request: Request) {
// ...
const orpcResult = await orpcHandler.handle(request, {
prefix: "/rpc",
context: { headers: request.headers },
context: { headers: request.headers, kv },
});
// ...
},
};Usage
ts
// src/routers/example.ts
import { base, type } from "$lib/orpc";
export const router = {
set: base
.input(type<{ foo: string }>())
.handler(async ({ context, input }) => {
await context.kv.setItem("foo", input.foo);
}),
};