# Framework Adapters

> Use kunii from Cloudflare-hosted SvelteKit, Next, Nuxt, Astro, and Koze apps

Package: Kunii
Canonical: https://kuratchi.dev/docs/kunii/framework-adapters
Markdown: https://kuratchi.dev/docs/kunii/framework-adapters.md

`kunii` is Cloudflare-only. The adapters below do not add support for
Node SQLite, Postgres, or external SQL providers. They make the same D1
migration and query-client flow work wherever Cloudflare exposes `env`.

Each adapter exports an adapter-specific `initKuniiORM`. All adapters run
`migrateOnce(env)` before your handler and expose schema-aware clients. Direct
client creation (`kunii`, `orm.db`, and `orm.clients`) never runs DDL by
itself.

## Plain Workers

Plain Workers do not need an adapter. Use the top-level ORM primitives inside
the native `fetch(request, env, ctx)` handler:

```ts
import { initKuniiORM } from '@kuratchi/kunii';
import { appSchema } from './schemas/app';

const orm = initKuniiORM({ DB: appSchema });

export default {
  async fetch(request, env, ctx) {
    await orm.migrateOnce(env);
    const db = orm.db(env, 'DB');
    const todos = await db.todos.orderBy({ created_at: 'desc' }).many();
    return Response.json(todos.data);
  },
};
```

## Koze

```ts
import { defineMiddleware } from '@kuratchi/koze';
import { initKuniiORM } from '@kuratchi/kunii/koze';
import { appSchema } from './server/schemas/app';

const orm = initKuniiORM({ DB: appSchema });

export default defineMiddleware({
  orm: orm.middleware(),
});
```

The existing `autoMigrate({ DB: appSchema })` helper remains supported for
compatibility.

## SvelteKit

Use this in `src/hooks.server.ts` with `@sveltejs/adapter-cloudflare`:

```ts
import { initKuniiORM } from '@kuratchi/kunii/sveltekit';
import { appSchema } from '$lib/server/schemas/app';

const orm = initKuniiORM({ DB: appSchema });

export const handle = orm.handle();
```

The adapter stores clients on `event.locals.koze.DB`.

## Astro

Use this with `@astrojs/cloudflare` middleware:

```ts
import { initKuniiORM } from '@kuratchi/kunii/astro';
import { appSchema } from './schemas/app';

const orm = initKuniiORM({ DB: appSchema });

export const onRequest = orm.middleware();
```

The adapter reads Cloudflare bindings from `context.locals.runtime.env` and
stores clients on `context.locals.koze.DB`.

## Nuxt

Use this in Nitro server middleware on Cloudflare:

```ts
import { initKuniiORM } from '@kuratchi/kunii/nuxt';
import { appSchema } from '../schemas/app';

const orm = initKuniiORM({ DB: appSchema });

export default orm.eventHandler();
```

The adapter reads `event.context.cloudflare.env` and stores clients on
`event.context.koze.DB`.

Plain Vue apps do not have a server ORM lifecycle. On Cloudflare, a Vue SPA
uses a Worker backend, so use the top-level Worker primitives there.

## Next.js

Use this with Next.js deployed through OpenNext on Cloudflare:

```ts
import { initKuniiORM } from '@kuratchi/kunii/next';
import { appSchema } from '@/server/schemas/app';

const orm = initKuniiORM({ DB: appSchema });

export const GET = orm.route(async ({ db }) => {
  const todos = await db.DB.todos.many();
  return Response.json(todos.data);
});
```

The Next adapter migrates inside the request wrapper. It does not rely on
startup instrumentation because Cloudflare Workers do not allow D1 I/O outside
a request context.

## Durable Objects

Durable Objects still use synchronous setup in the constructor:

```ts
import { initKuniiDO } from '@kuratchi/kunii';

export class OrgDO extends DurableObject {
  db;

  constructor(ctx, env) {
    super(ctx, env);
    this.db = initKuniiDO(ctx.storage, orgSchema);
  }
}
```
