# Universal Adapter Pattern

> The Universal Adapter pattern powering Aerostack modules. Three-layer architecture for portability across Cloudflare Workers, Node.js, Bun, and Deno.

The core philosophy of Aerostack modules is **Platform Portability**. By following the three-layer architecture, your logic can run on Cloudflare Workers, Node.js, Bun, or Deno with zero changes.

## The Three Layers

### 1. Logic Layer (`core.ts`)
This is where your business logic lives. 
- **Rule**: No framework imports (no Hono, Express, etc.).
- **Rule**: Use the `CoreContext` pattern to access DB and Environment.

```typescript

  return ctx.db.insert(notes).values(data).returning();
}
```

### 2. Data Layer (`schema.ts`)
Defines your database tables using **Drizzle ORM**.
- **Benefit**: Allows the Aerostack CLI to auto-detect and sync your schema when a module is installed.

### 3. Adapter Layer (`adapter.ts`)
The "glue" that connects your core logic to a specific web framework.
- **Hono Adapter** (for Workers/Bun):
  ```typescript
  import { Hono } from 'hono';
  import { createNote } from './core';
  const app = new Hono();
  app.post('/', async (c) => c.json(await createNote({ db: c.env.DB }, await c.req.json())));
  ```

## Why this matters

1. **No Vendor Lock-in**: Move from Cloudflare to a dedicated server without rewriting your feature logic.
2. **Easy Testing**: You can unit test your `core.ts` in standard Node.js without mocking complex Worker environments.
3. **Composable Repos**: The CLI uses these layers to safely "stitch" community modules into your codebase.
