Skip to content

Commit b40eb04

Browse files
authored
Merge pull request #355 from objectstack-ai/copilot/complete-edge-runtime-support
2 parents 273121b + 4004620 commit b40eb04

34 files changed

Lines changed: 3748 additions & 44 deletions

content/docs/server/edge.mdx

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
---
2+
title: "Edge Runtime"
3+
description: Edge Runtime Support
4+
---
5+
6+
ObjectQL runs natively on edge runtimes. The `@objectql/edge-adapter` package provides runtime detection, capability validation, and automatic driver binding resolution for serverless edge environments.
7+
8+
## 1. Supported Runtimes
9+
10+
| Runtime | Detection Signal | Default Driver |
11+
|----------------------|-------------------------------------------|---------------------------------|
12+
| Cloudflare Workers | `globalThis.caches` + `WebSocketPair` | `@objectql/driver-sqlite-wasm` |
13+
| Deno Deploy | `globalThis.Deno` | `@objectql/driver-pg-wasm` |
14+
| Vercel Edge | `globalThis.EdgeRuntime` | `@objectql/driver-memory` |
15+
| Bun | `globalThis.Bun` | `@objectql/driver-sqlite-wasm` |
16+
| Node.js | Default fallback | `@objectql/driver-sql` |
17+
18+
## 2. Installation
19+
20+
```bash
21+
pnpm add @objectql/edge-adapter
22+
```
23+
24+
## 3. Basic Configuration
25+
26+
Register the `EdgeAdapterPlugin` in your kernel. It auto-detects the runtime and resolves driver bindings.
27+
28+
```typescript
29+
import { EdgeAdapterPlugin } from '@objectql/edge-adapter';
30+
import { createKernel } from '@objectstack/runtime';
31+
32+
const kernel = createKernel({
33+
plugins: [
34+
new EdgeAdapterPlugin()
35+
]
36+
});
37+
38+
await kernel.start();
39+
```
40+
41+
## 4. Runtime Detection
42+
43+
The adapter inspects `globalThis` to detect the active runtime. Detection checks run in order from most specific to least specific, preventing false positives.
44+
45+
```typescript
46+
import { detectRuntime } from '@objectql/edge-adapter';
47+
48+
const runtime = detectRuntime();
49+
// => 'cloudflare-workers' | 'deno-deploy' | 'vercel-edge' | 'bun' | 'node'
50+
```
51+
52+
You can override auto-detection by passing an explicit runtime:
53+
54+
```typescript
55+
new EdgeAdapterPlugin({
56+
runtime: 'cloudflare-workers',
57+
});
58+
```
59+
60+
## 5. Capability Validation
61+
62+
Before initializing drivers, validate that the runtime meets your requirements. The plugin throws on startup if validation fails.
63+
64+
```typescript
65+
import { EdgeAdapterPlugin } from '@objectql/edge-adapter';
66+
67+
new EdgeAdapterPlugin({
68+
requirements: {
69+
wasm: true,
70+
persistentStorage: true,
71+
webSocket: true,
72+
minExecutionTime: 30000,
73+
},
74+
});
75+
```
76+
77+
You can also validate programmatically:
78+
79+
```typescript
80+
import { detectRuntime, validateCapabilities } from '@objectql/edge-adapter';
81+
82+
const runtime = detectRuntime();
83+
const result = validateCapabilities(runtime, {
84+
wasm: true,
85+
persistentStorage: true,
86+
});
87+
88+
if (!result.valid) {
89+
console.error('Missing:', result.missing);
90+
}
91+
```
92+
93+
## 6. Driver Bindings
94+
95+
Each runtime has a recommended default driver. Override bindings for custom datasource configurations.
96+
97+
```typescript
98+
new EdgeAdapterPlugin({
99+
bindings: {
100+
default: {
101+
driver: '@objectql/driver-sqlite-wasm',
102+
binding: 'DB',
103+
config: { pragma: { journal_mode: 'WAL' } },
104+
},
105+
analytics: {
106+
driver: '@objectql/driver-memory',
107+
config: { maxSize: 5000 },
108+
},
109+
},
110+
});
111+
```
112+
113+
When no explicit bindings are provided, the adapter generates a `default` binding using the recommended driver for the detected runtime.
114+
115+
## 7. Platform Examples
116+
117+
### Cloudflare Workers
118+
119+
```typescript
120+
import { EdgeAdapterPlugin } from '@objectql/edge-adapter';
121+
import { createKernel } from '@objectstack/runtime';
122+
123+
const kernel = createKernel({
124+
plugins: [
125+
new EdgeAdapterPlugin({
126+
runtime: 'cloudflare-workers',
127+
bindings: {
128+
default: {
129+
driver: '@objectql/driver-sqlite-wasm',
130+
binding: 'DB', // Cloudflare D1 binding name
131+
config: {},
132+
},
133+
},
134+
requirements: {
135+
wasm: true,
136+
persistentStorage: true,
137+
},
138+
})
139+
]
140+
});
141+
142+
export default {
143+
async fetch(request: Request, env: Record<string, unknown>) {
144+
await kernel.start();
145+
// Handle request...
146+
return new Response('OK');
147+
},
148+
};
149+
```
150+
151+
### Deno Deploy
152+
153+
```typescript
154+
import { EdgeAdapterPlugin } from '@objectql/edge-adapter';
155+
import { createKernel } from '@objectstack/runtime';
156+
157+
const kernel = createKernel({
158+
plugins: [
159+
new EdgeAdapterPlugin({
160+
runtime: 'deno-deploy',
161+
bindings: {
162+
default: {
163+
driver: '@objectql/driver-pg-wasm',
164+
config: {
165+
connectionString: Deno.env.get('DATABASE_URL'),
166+
},
167+
},
168+
},
169+
})
170+
]
171+
});
172+
173+
await kernel.start();
174+
175+
Deno.serve(async (request: Request) => {
176+
// Handle request...
177+
return new Response('OK');
178+
});
179+
```
180+
181+
### Vercel Edge
182+
183+
```typescript
184+
import { EdgeAdapterPlugin } from '@objectql/edge-adapter';
185+
import { createKernel } from '@objectstack/runtime';
186+
187+
export const config = { runtime: 'edge' };
188+
189+
const kernel = createKernel({
190+
plugins: [
191+
new EdgeAdapterPlugin({
192+
runtime: 'vercel-edge',
193+
maxExecutionTime: 25000,
194+
requestScoped: true,
195+
})
196+
]
197+
});
198+
199+
export default async function handler(request: Request) {
200+
await kernel.start();
201+
// Handle request...
202+
return new Response('OK');
203+
}
204+
```
205+
206+
## 8. Plugin Options
207+
208+
| Option | Type | Default | Description |
209+
|-------------------|-------------------------|----------------|-------------------------------------------|
210+
| `runtime` | `EdgeRuntime` | Auto-detected | Override the detected runtime |
211+
| `bindings` | `Record<string, ...>` | Default driver | Explicit driver bindings per datasource |
212+
| `maxExecutionTime`| `number` | From runtime | Override max execution time (ms) |
213+
| `requestScoped` | `boolean` | `true` | Enable request-scoped connections |
214+
| `requirements` | `CapabilityRequirement` | `undefined` | Capability requirements to validate |

content/docs/server/meta.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"integration",
77
"security",
88
"microservices",
9-
"plugins"
9+
"plugins",
10+
"edge",
11+
"sync"
1012
]
1113
}

0 commit comments

Comments
 (0)