@farming-labs/orm

Cloudflare KV

Cloudflare KV support in Farming Labs ORM is the worker-native key-value path for teams that want one storage layer without building a separate Cloudflare-only adapter surface.

The supported path is the real Worker binding:

That keeps the runtime Cloudflare-friendly while still letting framework and package code stay backend-agnostic.

What this gives you

You still write the schema and storage layer once.

Then the runtime translates that layer into KV-backed operations with:

That is a strong fit for sessions, tokens, cache metadata, independent key-value state, rate limits, and other lightweight framework-owned records inside Workers.

Create the runtime directly

cloudflare-kv-runtime.ts
import { createOrm } from "@farming-labs/orm";
import { createKvDriver } from "@farming-labs/orm-kv";
import { appSchema } from "./schema";

type Env = {
  KV: KVNamespace;
};

export default {
  async fetch(_request: Request, env: Env) {
    const orm = createOrm({
      schema: appSchema,
      driver: createKvDriver({
        client: env.KV,
      }),
    });

    const count = await orm.session.count();
    return Response.json({ count });
  },
};

Use the runtime helper path

If a framework or shared package wants to accept the raw KV namespace and normalize it later, use the runtime helpers:

import { createOrmFromRuntime } from "@farming-labs/orm-runtime";

const orm = await createOrmFromRuntime({
  schema: appSchema,
  client: env.KV,
});

That keeps the package boundary generic instead of forcing a Cloudflare KV specific branch.

Setup helpers

Cloudflare KV is a runtime-first backend, not a schema-push backend.

That means:

import { bootstrapDatabase } from "@farming-labs/orm-runtime/setup";

const orm = await bootstrapDatabase({
  schema: appSchema,
  client: env.KV,
});

Joins, relations, and transactions

Cloudflare KV is not a join-first runtime, so the ORM stays explicit and conservative.

That means:

What is supported well

Important limits

Why it matters

This keeps Cloudflare KV inside the same bigger ORM story: