alchemy 0.52.0 → 0.54.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/alchemy.js +196353 -0
- package/bin/alchemy.ts +2 -2
- package/bin/commands/init.ts +1190 -0
- package/bin/constants.ts +18 -0
- package/bin/services/execute-alchemy.ts +7 -2
- package/bin/services/package-manager.ts +1 -1
- package/bin/types.ts +14 -5
- package/lib/cloudflare/astro.d.ts.map +1 -1
- package/lib/cloudflare/astro.js +4 -3
- package/lib/cloudflare/astro.js.map +1 -1
- package/lib/cloudflare/bundle/externals.d.ts +3 -0
- package/lib/cloudflare/bundle/externals.d.ts.map +1 -0
- package/lib/cloudflare/bundle/externals.js +35 -0
- package/lib/cloudflare/bundle/externals.js.map +1 -0
- package/lib/cloudflare/bundle/internal-worker-bundle.d.ts +8 -1
- package/lib/cloudflare/bundle/internal-worker-bundle.d.ts.map +1 -1
- package/lib/cloudflare/bundle/internal-worker-bundle.js +11 -4
- package/lib/cloudflare/bundle/internal-worker-bundle.js.map +1 -1
- package/lib/cloudflare/bundle/plugin-hot-reload.d.ts +4 -1
- package/lib/cloudflare/bundle/plugin-hot-reload.d.ts.map +1 -1
- package/lib/cloudflare/bundle/plugin-hot-reload.js +27 -11
- package/lib/cloudflare/bundle/plugin-hot-reload.js.map +1 -1
- package/lib/cloudflare/bundle/plugin-hybrid-node-compat.js +5 -1
- package/lib/cloudflare/bundle/plugin-hybrid-node-compat.js.map +1 -1
- package/lib/cloudflare/bundle/plugin-wasm.d.ts +6 -2
- package/lib/cloudflare/bundle/plugin-wasm.d.ts.map +1 -1
- package/lib/cloudflare/bundle/plugin-wasm.js +21 -47
- package/lib/cloudflare/bundle/plugin-wasm.js.map +1 -1
- package/lib/cloudflare/bundle/validate-node-compat.d.ts +2 -1
- package/lib/cloudflare/bundle/validate-node-compat.d.ts.map +1 -1
- package/lib/cloudflare/bundle/validate-node-compat.js +5 -4
- package/lib/cloudflare/bundle/validate-node-compat.js.map +1 -1
- package/lib/cloudflare/compatibility-date.gen.d.ts +1 -1
- package/lib/cloudflare/compatibility-date.gen.js +1 -1
- package/lib/cloudflare/d1-database.js +1 -1
- package/lib/cloudflare/d1-database.js.map +1 -1
- package/lib/cloudflare/d1-migrations.d.ts.map +1 -1
- package/lib/cloudflare/d1-migrations.js +8 -10
- package/lib/cloudflare/d1-migrations.js.map +1 -1
- package/lib/cloudflare/miniflare/miniflare-worker-options.d.ts +3 -3
- package/lib/cloudflare/miniflare/miniflare-worker-options.d.ts.map +1 -1
- package/lib/cloudflare/miniflare/miniflare-worker-options.js +23 -6
- package/lib/cloudflare/miniflare/miniflare-worker-options.js.map +1 -1
- package/lib/cloudflare/miniflare/miniflare.d.ts.map +1 -1
- package/lib/cloudflare/miniflare/miniflare.js +2 -4
- package/lib/cloudflare/miniflare/miniflare.js.map +1 -1
- package/lib/cloudflare/miniflare/remote-binding-proxy.d.ts.map +1 -1
- package/lib/cloudflare/miniflare/remote-binding-proxy.js +4 -6
- package/lib/cloudflare/miniflare/remote-binding-proxy.js.map +1 -1
- package/lib/cloudflare/react-router.d.ts +4 -0
- package/lib/cloudflare/react-router.d.ts.map +1 -1
- package/lib/cloudflare/react-router.js +8 -5
- package/lib/cloudflare/react-router.js.map +1 -1
- package/lib/cloudflare/worker-bundle.d.ts +80 -0
- package/lib/cloudflare/worker-bundle.d.ts.map +1 -0
- package/lib/cloudflare/worker-bundle.js +266 -0
- package/lib/cloudflare/worker-bundle.js.map +1 -0
- package/lib/cloudflare/worker.d.ts +1 -1
- package/lib/cloudflare/worker.d.ts.map +1 -1
- package/lib/cloudflare/worker.js +81 -195
- package/lib/cloudflare/worker.js.map +1 -1
- package/lib/cloudflare/wrangler.json.js +2 -3
- package/lib/cloudflare/wrangler.json.js.map +1 -1
- package/lib/esbuild/bundle.d.ts +1 -0
- package/lib/esbuild/bundle.d.ts.map +1 -1
- package/lib/state/cloudflare-state-store.js +1 -5
- package/lib/state/cloudflare-state-store.js.map +1 -1
- package/lib/state/dofs-state-store/store.d.ts.map +1 -1
- package/lib/state/dofs-state-store/store.js +4 -4
- package/lib/state/dofs-state-store/store.js.map +1 -1
- package/package.json +10 -5
- package/src/cloudflare/astro.ts +4 -3
- package/src/cloudflare/bundle/externals.ts +36 -0
- package/src/cloudflare/bundle/internal-worker-bundle.ts +12 -3
- package/src/cloudflare/bundle/plugin-hot-reload.ts +32 -12
- package/src/cloudflare/bundle/plugin-hybrid-node-compat.ts +6 -1
- package/src/cloudflare/bundle/plugin-wasm.ts +26 -54
- package/src/cloudflare/bundle/validate-node-compat.ts +6 -5
- package/src/cloudflare/compatibility-date.gen.ts +1 -1
- package/src/cloudflare/d1-database.ts +1 -1
- package/src/cloudflare/d1-migrations.ts +19 -18
- package/src/cloudflare/miniflare/miniflare-worker-options.ts +27 -13
- package/src/cloudflare/miniflare/miniflare.ts +2 -4
- package/src/cloudflare/miniflare/remote-binding-proxy.ts +5 -7
- package/src/cloudflare/react-router.ts +14 -6
- package/src/cloudflare/worker-bundle.ts +364 -0
- package/src/cloudflare/worker.ts +104 -241
- package/src/cloudflare/wrangler.json.ts +2 -3
- package/src/state/cloudflare-state-store.ts +1 -5
- package/src/state/dofs-state-store/store.ts +4 -4
- package/templates/nuxt/package.json +1 -1
- package/templates/nuxt/types/env.d.ts +11 -0
- package/templates/react-router/workers/app.ts +2 -2
- package/templates/rwsdk/.cursor/rules/alchemy_cloudflare.mdc +385 -0
- package/templates/rwsdk/wrangler.jsonc +10 -7
- package/templates/typescript/tsconfig.json +1 -2
- package/bin/alchemy.mjs +0 -51766
- package/lib/cloudflare/bundle/esbuild.d.ts +0 -16
- package/lib/cloudflare/bundle/esbuild.d.ts.map +0 -1
- package/lib/cloudflare/bundle/esbuild.js +0 -159
- package/lib/cloudflare/bundle/esbuild.js.map +0 -1
- package/lib/cloudflare/bundle/fs.d.ts +0 -18
- package/lib/cloudflare/bundle/fs.d.ts.map +0 -1
- package/lib/cloudflare/bundle/fs.js +0 -78
- package/lib/cloudflare/bundle/fs.js.map +0 -1
- package/lib/cloudflare/bundle/index.d.ts +0 -3
- package/lib/cloudflare/bundle/index.d.ts.map +0 -1
- package/lib/cloudflare/bundle/index.js +0 -2
- package/lib/cloudflare/bundle/index.js.map +0 -1
- package/lib/cloudflare/bundle/inline.d.ts +0 -12
- package/lib/cloudflare/bundle/inline.d.ts.map +0 -1
- package/lib/cloudflare/bundle/inline.js +0 -32
- package/lib/cloudflare/bundle/inline.js.map +0 -1
- package/lib/cloudflare/bundle/normalize.d.ts +0 -21
- package/lib/cloudflare/bundle/normalize.d.ts.map +0 -1
- package/lib/cloudflare/bundle/normalize.js +0 -49
- package/lib/cloudflare/bundle/normalize.js.map +0 -1
- package/lib/cloudflare/bundle/shared.d.ts +0 -30
- package/lib/cloudflare/bundle/shared.d.ts.map +0 -1
- package/lib/cloudflare/bundle/shared.js +0 -34
- package/lib/cloudflare/bundle/shared.js.map +0 -1
- package/src/cloudflare/bundle/esbuild.ts +0 -194
- package/src/cloudflare/bundle/fs.ts +0 -103
- package/src/cloudflare/bundle/index.ts +0 -6
- package/src/cloudflare/bundle/inline.ts +0 -42
- package/src/cloudflare/bundle/normalize.ts +0 -76
- package/src/cloudflare/bundle/shared.ts +0 -73
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Core Alchemy development guidelines and patterns
|
|
3
|
+
globs: *.ts
|
|
4
|
+
alwaysApply: true
|
|
5
|
+
---
|
|
6
|
+
# Alchemy - TypeScript-native Infrastructure as Code
|
|
7
|
+
|
|
8
|
+
Alchemy is a TypeScript-native Infrastructure-as-Code framework that allows you to define cloud resources using familiar TypeScript patterns. This guide focuses on Cloudflare integration.
|
|
9
|
+
|
|
10
|
+
## Core Concepts
|
|
11
|
+
|
|
12
|
+
**Resources**: Async functions that manage cloud infrastructure lifecycle (create, update, delete)
|
|
13
|
+
**Bindings**: Type-safe connections between resources (workers, KV stores, databases)
|
|
14
|
+
**State Management**: Tracks resource state for consistent deployments
|
|
15
|
+
**Finalization**: Always call `app.finalize()` to clean up orphaned resources
|
|
16
|
+
|
|
17
|
+
## Basic Setup
|
|
18
|
+
|
|
19
|
+
### Project Structure
|
|
20
|
+
```typescript
|
|
21
|
+
// alchemy.run.ts - Your infrastructure definition
|
|
22
|
+
import alchemy from "alchemy";
|
|
23
|
+
import { Worker, KVNamespace } from "alchemy/cloudflare";
|
|
24
|
+
|
|
25
|
+
const app = await alchemy("my-app");
|
|
26
|
+
|
|
27
|
+
export const worker = await Worker("api", {
|
|
28
|
+
entrypoint: "./src/worker.ts",
|
|
29
|
+
bindings: {
|
|
30
|
+
CACHE: await KVNamespace("cache", { title: "cache-store" })
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
console.log({ url: worker.url });
|
|
35
|
+
await app.finalize(); // ⚠️ ALWAYS call finalize()
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Environment Setup
|
|
39
|
+
```bash
|
|
40
|
+
# .env - Required for secret encryption
|
|
41
|
+
ALCHEMY_PASSWORD=your-secure-password
|
|
42
|
+
|
|
43
|
+
# Optional: Use Cloudflare state store in production
|
|
44
|
+
ALCHEMY_STATE_STORE=cloudflare
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Commands
|
|
48
|
+
```bash
|
|
49
|
+
bun alchemy deploy # Deploy
|
|
50
|
+
bun alchemy dev # Local development
|
|
51
|
+
bun alchemy destroy # Destroy all resources
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Workers
|
|
55
|
+
|
|
56
|
+
### Basic Worker
|
|
57
|
+
```typescript
|
|
58
|
+
const worker = await Worker("api", {
|
|
59
|
+
entrypoint: "./src/worker.ts",
|
|
60
|
+
url: true, // Get public URL
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Worker with Bindings
|
|
65
|
+
```typescript
|
|
66
|
+
const worker = await Worker("api", {
|
|
67
|
+
entrypoint: "./src/worker.ts",
|
|
68
|
+
bindings: {
|
|
69
|
+
// Resource bindings
|
|
70
|
+
CACHE: kvNamespace,
|
|
71
|
+
STORAGE: r2Bucket,
|
|
72
|
+
COUNTER: durableObjectNamespace,
|
|
73
|
+
QUEUE: queue,
|
|
74
|
+
API: otherWorker,
|
|
75
|
+
|
|
76
|
+
// Environment variables
|
|
77
|
+
API_KEY: alchemy.secret(process.env.API_KEY),
|
|
78
|
+
DEBUG: "true"
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Worker Implementation
|
|
84
|
+
```typescript
|
|
85
|
+
// src/worker.ts
|
|
86
|
+
import type { worker } from "../alchemy.run";
|
|
87
|
+
|
|
88
|
+
export default {
|
|
89
|
+
async fetch(request: Request, env: typeof worker.Env): Promise<Response> {
|
|
90
|
+
// Type-safe access to all bindings
|
|
91
|
+
const cached = await env.CACHE.get("key");
|
|
92
|
+
const apiKey = env.API_KEY;
|
|
93
|
+
|
|
94
|
+
return new Response("Hello World");
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Type Safety Setup
|
|
100
|
+
```typescript
|
|
101
|
+
// types/env.d.ts
|
|
102
|
+
import type { worker } from "../alchemy.run";
|
|
103
|
+
|
|
104
|
+
declare module "cloudflare:workers" {
|
|
105
|
+
namespace Cloudflare {
|
|
106
|
+
export interface Env extends typeof worker.Env {}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Durable Objects
|
|
112
|
+
|
|
113
|
+
### Creating Durable Object Namespace
|
|
114
|
+
```typescript
|
|
115
|
+
// Use 'new' instead of 'await' for DurableObjectNamespace
|
|
116
|
+
const counter = DurableObjectNamespace("counter", {
|
|
117
|
+
className: "Counter",
|
|
118
|
+
sqlite: true, // Enable SQLite storage
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const worker = await Worker("api", {
|
|
122
|
+
entrypoint: "./src/worker.ts",
|
|
123
|
+
bindings: {
|
|
124
|
+
COUNTER: counter, // Bind to worker
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Durable Object Implementation
|
|
130
|
+
```typescript
|
|
131
|
+
// src/counter.ts
|
|
132
|
+
import { DurableObject } from "cloudflare:workers";
|
|
133
|
+
|
|
134
|
+
export class Counter extends DurableObject {
|
|
135
|
+
async increment(): Promise<number> {
|
|
136
|
+
let count = (await this.ctx.storage.get("count")) || 0;
|
|
137
|
+
count++;
|
|
138
|
+
await this.ctx.storage.put("count", count);
|
|
139
|
+
return count;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async fetch(request: Request): Promise<Response> {
|
|
143
|
+
const count = await this.increment();
|
|
144
|
+
return new Response(JSON.stringify({ count }));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Using Durable Objects in Worker
|
|
150
|
+
```typescript
|
|
151
|
+
// src/worker.ts
|
|
152
|
+
export default {
|
|
153
|
+
async fetch(request: Request, env: typeof worker.Env) {
|
|
154
|
+
// Get a Durable Object instance
|
|
155
|
+
const id = env.COUNTER.idFromName("global-counter");
|
|
156
|
+
const obj = env.COUNTER.get(id);
|
|
157
|
+
|
|
158
|
+
// Call the Durable Object
|
|
159
|
+
return obj.fetch(request);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Cross-Script Durable Objects
|
|
165
|
+
```typescript
|
|
166
|
+
// Method 1: Re-export from provider worker
|
|
167
|
+
const sharedCounter = host.bindings.SHARED_COUNTER;
|
|
168
|
+
|
|
169
|
+
// Method 2: Reference by worker script name
|
|
170
|
+
const counter = DurableObjectNamespace("counter", {
|
|
171
|
+
className: "Counter",
|
|
172
|
+
scriptName: "provider-worker"
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Key Resources
|
|
177
|
+
|
|
178
|
+
### KV Namespace
|
|
179
|
+
```typescript
|
|
180
|
+
const cache = await KVNamespace("cache", {
|
|
181
|
+
title: "my-cache-store"
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Usage in worker
|
|
185
|
+
const value = await env.CACHE.get("key");
|
|
186
|
+
await env.CACHE.put("key", "value", { expirationTtl: 3600 });
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### R2 Bucket
|
|
190
|
+
```typescript
|
|
191
|
+
const storage = await R2Bucket("storage", {
|
|
192
|
+
allowPublicAccess: false
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Usage in worker
|
|
196
|
+
const object = await env.STORAGE.get("file.txt");
|
|
197
|
+
await env.STORAGE.put("file.txt", "content");
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Queue
|
|
201
|
+
```typescript
|
|
202
|
+
// Typed queue
|
|
203
|
+
const queue = await Queue<{ name: string; email: string }>("notifications");
|
|
204
|
+
|
|
205
|
+
// Producer worker
|
|
206
|
+
const producer = await Worker("producer", {
|
|
207
|
+
bindings: { QUEUE: queue },
|
|
208
|
+
// Queue sending code
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// Consumer worker
|
|
212
|
+
const consumer = await Worker("consumer", {
|
|
213
|
+
eventSources: [queue], // Register as consumer
|
|
214
|
+
// Queue processing code
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Worker implementation for queue processing
|
|
218
|
+
export default {
|
|
219
|
+
async queue(batch: typeof queue.Batch, env: Env) {
|
|
220
|
+
for (const message of batch.messages) {
|
|
221
|
+
console.log("Processing:", message.body);
|
|
222
|
+
message.ack(); // Acknowledge message
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Custom Domains
|
|
229
|
+
```typescript
|
|
230
|
+
const worker = await Worker("api", {
|
|
231
|
+
entrypoint: "./src/worker.ts",
|
|
232
|
+
routes: [
|
|
233
|
+
{ pattern: "api.example.com/*", zone: "example.com" },
|
|
234
|
+
{ pattern: "example.com/api/*", zone: "example.com" }
|
|
235
|
+
]
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Converting Existing Cloudflare Projects
|
|
240
|
+
|
|
241
|
+
### 1. Migrate wrangler.toml Configuration
|
|
242
|
+
```toml
|
|
243
|
+
# Old wrangler.toml
|
|
244
|
+
name = "my-worker"
|
|
245
|
+
main = "src/index.js"
|
|
246
|
+
|
|
247
|
+
[env.production]
|
|
248
|
+
kv_namespaces = [
|
|
249
|
+
{ binding = "CACHE", id = "abc123" }
|
|
250
|
+
]
|
|
251
|
+
|
|
252
|
+
[[env.production.r2_buckets]]
|
|
253
|
+
binding = "STORAGE"
|
|
254
|
+
bucket_name = "my-bucket"
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
// New alchemy.run.ts
|
|
259
|
+
const worker = await Worker("my-worker", {
|
|
260
|
+
entrypoint: "./src/index.js",
|
|
261
|
+
bindings: {
|
|
262
|
+
CACHE: await KVNamespace("cache", {
|
|
263
|
+
title: "cache-store",
|
|
264
|
+
adopt: true // Use existing KV namespace
|
|
265
|
+
}),
|
|
266
|
+
STORAGE: await R2Bucket("storage", {
|
|
267
|
+
name: "my-bucket",
|
|
268
|
+
adopt: true // Use existing bucket
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 2. Enable Development Compatibility
|
|
275
|
+
```typescript
|
|
276
|
+
// Generate wrangler.json for local development
|
|
277
|
+
await WranglerJson("wrangler.json", {
|
|
278
|
+
worker,
|
|
279
|
+
});
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### 3. Adopt Existing Resources
|
|
283
|
+
```typescript
|
|
284
|
+
// Use adopt: true to use existing resources instead of failing
|
|
285
|
+
const bucket = await R2Bucket("storage", {
|
|
286
|
+
name: "existing-bucket-name",
|
|
287
|
+
adopt: true // Won't fail if bucket already exists
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### 4. State Store Migration
|
|
292
|
+
```typescript
|
|
293
|
+
// Local development (default)
|
|
294
|
+
const app = await alchemy("my-app");
|
|
295
|
+
|
|
296
|
+
// Production with Cloudflare state store
|
|
297
|
+
const app = await alchemy("my-app", {
|
|
298
|
+
stateStore: process.env.NODE_ENV === "production"
|
|
299
|
+
? (scope) => new DOStateStore(scope)
|
|
300
|
+
: undefined
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## Advanced Patterns
|
|
305
|
+
|
|
306
|
+
### Framework Integration (Vite/React/etc.)
|
|
307
|
+
```typescript
|
|
308
|
+
const website = await Vite("website", {
|
|
309
|
+
main: "./src/worker.ts",
|
|
310
|
+
command: "bun run build", // Build command
|
|
311
|
+
bindings: {
|
|
312
|
+
API: await Worker("api", { entrypoint: "./api/worker.ts" }),
|
|
313
|
+
STORAGE: await R2Bucket("assets")
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### Resource Scoping
|
|
319
|
+
```typescript
|
|
320
|
+
// Organize resources into logical groups
|
|
321
|
+
await alchemy.run("backend", async () => {
|
|
322
|
+
await Worker("api", { entrypoint: "./api.ts" });
|
|
323
|
+
await KVNamespace("cache", { title: "api-cache" });
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
await alchemy.run("frontend", async () => {
|
|
327
|
+
await Vite("website", { main: "./src/worker.ts" });
|
|
328
|
+
});
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Testing Pattern
|
|
332
|
+
```typescript
|
|
333
|
+
import { alchemy } from "../../src/alchemy";
|
|
334
|
+
const test = alchemy.test(import.meta, { prefix: "test" });
|
|
335
|
+
|
|
336
|
+
describe("Worker Tests", () => {
|
|
337
|
+
test("creates worker", async (scope) => {
|
|
338
|
+
const worker = await Worker("test-worker", {
|
|
339
|
+
entrypoint: "./src/worker.ts"
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
expect(worker.url).toBeTruthy();
|
|
343
|
+
// Resources auto-cleaned after test
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## Best Practices
|
|
349
|
+
|
|
350
|
+
1. **Always call finalize()**: `await app.finalize()` at the end of your script
|
|
351
|
+
2. **Use adoption for migrations**: `adopt: true` when converting existing projects
|
|
352
|
+
3. **Type-safe bindings**: Set up env.d.ts for full type safety
|
|
353
|
+
4. **Resource naming**: Use consistent, descriptive names for resources
|
|
354
|
+
5. **State store**: Use DOStateStore for production deployments
|
|
355
|
+
6. **Secrets**: Use `alchemy.secret()` for sensitive values
|
|
356
|
+
7. **Scoping**: Organize related resources using `alchemy.run()`
|
|
357
|
+
8. **Testing**: Use `alchemy.test()` for integration tests
|
|
358
|
+
|
|
359
|
+
## Common Issues
|
|
360
|
+
|
|
361
|
+
- **Missing finalize()**: Resources won't be cleaned up properly
|
|
362
|
+
- **Wrong DurableObject syntax**: Use `DurableObjectNamespace()` not `await`
|
|
363
|
+
- **Type errors**: Set up env.d.ts file for binding types
|
|
364
|
+
- **State conflicts**: Use different stages/prefixes for different environments
|
|
365
|
+
- **Resource adoption**: Use `adopt: true` when migrating existing resources
|
|
366
|
+
|
|
367
|
+
## Development Workflow
|
|
368
|
+
|
|
369
|
+
```bash
|
|
370
|
+
# Create new project
|
|
371
|
+
bunx alchemy create my-app --template=vite
|
|
372
|
+
cd my-app
|
|
373
|
+
|
|
374
|
+
# Set up authentication (one-time)
|
|
375
|
+
bun wrangler login
|
|
376
|
+
|
|
377
|
+
# Deploy infrastructure
|
|
378
|
+
bun run deploy
|
|
379
|
+
|
|
380
|
+
# Local development
|
|
381
|
+
bun run dev
|
|
382
|
+
|
|
383
|
+
# Clean up
|
|
384
|
+
bun run destroy
|
|
385
|
+
```
|
|
@@ -6,8 +6,11 @@
|
|
|
6
6
|
"assets": { "binding": "ASSETS", "directory": "dist/client" },
|
|
7
7
|
"durable_objects": {
|
|
8
8
|
"bindings": [
|
|
9
|
-
{
|
|
10
|
-
|
|
9
|
+
{
|
|
10
|
+
"name": "SESSION_DURABLE_OBJECT",
|
|
11
|
+
"class_name": "SessionDurableObject",
|
|
12
|
+
},
|
|
13
|
+
],
|
|
11
14
|
},
|
|
12
15
|
"d1_databases": [
|
|
13
16
|
{
|
|
@@ -15,14 +18,14 @@
|
|
|
15
18
|
"database_id": "33c4fe7d-fbfe-4cef-959c-bb54292af712",
|
|
16
19
|
"database_name": "my-alchemy-app-db",
|
|
17
20
|
"migrations_dir": "migrations",
|
|
18
|
-
"preview_database_id": "33c4fe7d-fbfe-4cef-959c-bb54292af712"
|
|
19
|
-
}
|
|
21
|
+
"preview_database_id": "33c4fe7d-fbfe-4cef-959c-bb54292af712",
|
|
22
|
+
},
|
|
20
23
|
],
|
|
21
24
|
"migrations": [
|
|
22
25
|
{
|
|
23
26
|
"tag": "v1",
|
|
24
27
|
"new_sqlite_classes": [],
|
|
25
|
-
"new_classes": ["SessionDurableObject"]
|
|
26
|
-
}
|
|
27
|
-
]
|
|
28
|
+
"new_classes": ["SessionDurableObject"],
|
|
29
|
+
},
|
|
30
|
+
],
|
|
28
31
|
}
|