alchemy 0.2.0 → 0.2.1
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/README.md +506 -0
- package/lib/alchemy.js +10 -2
- package/lib/apply.js +86 -77
- package/lib/cloudflare/index.d.ts +2 -0
- package/lib/cloudflare/index.js +2 -0
- package/lib/cloudflare/static-site.js +9 -9
- package/lib/cloudflare/worker.js +1 -1
- package/lib/cloudflare/wrangler.json.d.ts +243 -0
- package/lib/cloudflare/wrangler.json.js +35 -0
- package/lib/cloudflare/zone-settings.d.ts +301 -0
- package/lib/cloudflare/zone-settings.js +1 -0
- package/lib/cloudflare/zone.d.ts +196 -0
- package/lib/cloudflare/zone.js +175 -0
- package/lib/destroy.js +1 -1
- package/lib/project/index.d.ts +1 -0
- package/lib/project/index.js +1 -0
- package/lib/project/vite.d.ts +39 -0
- package/lib/project/vite.js +68 -0
- package/lib/scope.d.ts +2 -0
- package/lib/scope.js +20 -1
- package/lib/state.js +6 -1
- package/lib/util/rm.d.ts +1 -0
- package/lib/util/rm.js +11 -0
- package/package.json +3 -2
- package/src/alchemy.ts +9 -2
- package/src/apply.ts +100 -94
- package/src/cloudflare/index.ts +2 -0
- package/src/cloudflare/static-site.ts +11 -11
- package/src/cloudflare/worker.ts +1 -1
- package/src/cloudflare/wrangler.json.ts +309 -0
- package/src/cloudflare/zone-settings.ts +368 -0
- package/src/cloudflare/zone.ts +491 -0
- package/src/destroy.ts +1 -1
- package/src/project/index.ts +1 -0
- package/src/project/vite.ts +152 -0
- package/src/scope.ts +25 -1
- package/src/state.ts +9 -1
- package/src/util/rm.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
# Alchemy
|
|
2
|
+
|
|
3
|
+
Alchemy is an embeddable, zero-dependency, TypeScript-native Infrastructure-as-Code (IaC) library for modeling Resources that are Created, Updated and Deleted automatically.
|
|
4
|
+
|
|
5
|
+
Unlike similar tools like Pulumi, Terraform, and CloudFormation, Alchemy is implemented in pure ESM-native TypeScript code with zero dependencies.
|
|
6
|
+
|
|
7
|
+
Resources are simple memoized async functions that can run in any JavaScript runtime, including the browser, serverless functions and durable workflows.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import alchemy from "alchemy";
|
|
11
|
+
|
|
12
|
+
await using _ = alchemy("cloudflare-worker");
|
|
13
|
+
|
|
14
|
+
export const worker = await Worker("worker", {
|
|
15
|
+
name: "my-worker",
|
|
16
|
+
entrypoint: "./src/index.ts",
|
|
17
|
+
bindings: {
|
|
18
|
+
COUNTER: counter,
|
|
19
|
+
STORAGE: storage, // Bind the R2 bucket to the worker
|
|
20
|
+
AUTH_STORE: authStore,
|
|
21
|
+
GITHUB_CLIENT_ID: secret(process.env.GITHUB_CLIENT_ID),
|
|
22
|
+
GITHUB_CLIENT_SECRET: secret(process.env.GITHUB_CLIENT_SECRET),
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
# Features
|
|
28
|
+
|
|
29
|
+
- **JS-native** - no second language, toolchains, dependencies, processes, services, etc. to lug around.
|
|
30
|
+
- **Async-native** - resources are just async functions - no complex abstraction to learn.
|
|
31
|
+
- **ESM-native** - built exclusively on ESM, with a slight preference for modern JS runtimes like Bun.
|
|
32
|
+
- **Embeddable** - runs in any JavaScript/TypeScript environment, including the browser!
|
|
33
|
+
- **Extensible** - implement your own resources with a simple function.
|
|
34
|
+
- **AI-first** - alchemy actively encourages you to use LLMs to create/copy/fork/modify resources to fit your needs. No more waiting around for a provider to be implemented, just do it yourself in a few minutes.
|
|
35
|
+
- **No dependencies** - the `alchemy` core package has 0 required dependencies.
|
|
36
|
+
- **No service** - state files are stored locally in your project and can be easily inspected, modified, checked into your repo, etc.
|
|
37
|
+
- **No strong opinions** - structure your codebase however you want, store state anywhere - we don't care!
|
|
38
|
+
|
|
39
|
+
# Examples
|
|
40
|
+
|
|
41
|
+
- CloudFlare ViteJS Website + API Backend with Durable Objects: [examples/cloudflare-vite/](./examples/cloudflare-vite/alchemy.config.ts)
|
|
42
|
+
- Deploy an AWS Lambda Function with a DynamoDB Table and IAM Role: [examples/aws-app/](./examples/aws-app/alchemy.config.ts)
|
|
43
|
+
|
|
44
|
+
# Getting Started
|
|
45
|
+
|
|
46
|
+
An alchemy "app" (if you want to call it that) is just an ordinary TypeScript or JavaScript script. Once you've installed the `alchemy` package, you can start using it however you want.
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# I recommend bun, but you can use any JavaScript runtime.
|
|
50
|
+
bun add alchemy
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Usually, you'll want to create an `alchemy.config.ts` script and then define your Resources.
|
|
54
|
+
|
|
55
|
+
> [!TIP]
|
|
56
|
+
> The `alchemy.config.ts` file is just a convention, not a requirement.
|
|
57
|
+
|
|
58
|
+
Your script should start by creating the Alchemy `app` (aka. "Root Scope", more on [Scopes](#resource-scope-tree) later):
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import alchemy from "alchemy";
|
|
62
|
+
|
|
63
|
+
// async disposables trigger finalization of the stack at the end of the script (after resources are declared)
|
|
64
|
+
await using app = alchemy("my-app", {
|
|
65
|
+
// namespace for stages
|
|
66
|
+
stage: process.env.STAGE ?? "dev",
|
|
67
|
+
// update or destroy the app
|
|
68
|
+
phase: process.argv.includes("--destroy") ? "destroy" : "up"
|
|
69
|
+
// password for encrypting/decrypting secrets stored in state
|
|
70
|
+
password: process.env.SECRET_PASSPHRASE,
|
|
71
|
+
// whether to log Create/Update/Delete events
|
|
72
|
+
quiet: process.argv.includes("--verbose") ? false : true,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// (otherwise, declare resources here AFTER the bootstrap)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Now that our app is initialized, we can start creating Resources, e.g. an AWS IAM Role:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { Role } from "alchemy/aws";
|
|
82
|
+
|
|
83
|
+
export const role = await Role("my-role", {
|
|
84
|
+
roleName: "my-role",
|
|
85
|
+
assumeRolePolicy: {
|
|
86
|
+
Version: "2012-10-17",
|
|
87
|
+
Statement: [
|
|
88
|
+
{
|
|
89
|
+
Effect: "Allow",
|
|
90
|
+
// Or whatever principal you want
|
|
91
|
+
Principal: { Service: "lambda.amazonaws.com" },
|
|
92
|
+
Action: "sts:AssumeRole",
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Notice how the `Role` is created by an `await Role(..)` function call.
|
|
100
|
+
In contrast to other IaC frameworks, Alchemy models Resources as memoized async functions that can be executed in any async environment - including the browser, serverless functions and durable workflows.
|
|
101
|
+
|
|
102
|
+
A nice benefit of async-await is how easy it becomes to access physical properties (otherwise known as "Stack Outputs").
|
|
103
|
+
You can just log the role name (crazy concept, right?):
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
console.log({
|
|
107
|
+
roleName: role.roleName, // string
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Alchemy State
|
|
112
|
+
|
|
113
|
+
Now, when you run your script:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
bun ./my-app.ts
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
You'll notice some files show up in `.alchemy/`:
|
|
120
|
+
|
|
121
|
+
```sh
|
|
122
|
+
.alchemy/
|
|
123
|
+
my-app/
|
|
124
|
+
prod/
|
|
125
|
+
my-role.json
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
These are called the "state files".
|
|
129
|
+
|
|
130
|
+
Go ahead, click on one and take a look - here's how my `my-role.json` looks:
|
|
131
|
+
|
|
132
|
+
```jsonc
|
|
133
|
+
{
|
|
134
|
+
"provider": "iam::Role",
|
|
135
|
+
"data": {},
|
|
136
|
+
"deps": [],
|
|
137
|
+
"status": "updated",
|
|
138
|
+
"output": {
|
|
139
|
+
"roleName": "alchemy-api-lambda-role"
|
|
140
|
+
// ..
|
|
141
|
+
},
|
|
142
|
+
"props": {
|
|
143
|
+
"roleName": "alchemy-api-lambda-role",
|
|
144
|
+
"assumeRolePolicy": {
|
|
145
|
+
"Version": "2012-10-17"
|
|
146
|
+
// ..
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Alchemy uses state to determine when to Create, Update, Delete or Skip Resources at runtime:
|
|
153
|
+
|
|
154
|
+
1. If the resource doesn't have a prior state, it will be `created`
|
|
155
|
+
1. If the inputs haven't changed since the last deployment, then it will be `skipped`,
|
|
156
|
+
1. If the inputs have changed, it will be `updated`
|
|
157
|
+
1. If the Resource no longer exists in the program (aka. is an orphan), then it will be `deleted`.
|
|
158
|
+
|
|
159
|
+
> [!TIP]
|
|
160
|
+
> Alchemy goes to great effort to be fully transparent. Each Resource's state is just a JSON file, nothing more. You can inspect it, modify it, commit it to your repo, store it in a database, etc.
|
|
161
|
+
|
|
162
|
+
## "Custom" Resources
|
|
163
|
+
|
|
164
|
+
Adding new Resources is the whole point of Alchemy, and is therefore very simple.
|
|
165
|
+
|
|
166
|
+
A Resource provider is just a function with a globally unique name, e.g. `dynamo::Table`, and an implementation of the Create, Update, Delete lifecycle operations.
|
|
167
|
+
|
|
168
|
+
Below is an illustrative example of the `dynamo::Table` provider.
|
|
169
|
+
|
|
170
|
+
> [!NOTE]
|
|
171
|
+
> See [table.ts](./alchemy/src/aws/table.ts) for the full implementation.
|
|
172
|
+
|
|
173
|
+
All Resources follow the same templated structure/convention:
|
|
174
|
+
|
|
175
|
+
1. an interface (or type) for the Resource's (Input) Properties
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
// a type to represent the Resource's input properties
|
|
179
|
+
export interface TableProps {
|
|
180
|
+
name: string;
|
|
181
|
+
//..
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
2. an interface (or type) for the Resource's (Output) Attributes
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
// declare a type to represent the Resource's properties (aka. attributes)
|
|
189
|
+
export interface Table extends Resource<"dynamo::Table"> {
|
|
190
|
+
tableArn: string;
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
3. a special "Resource" function defining the Resource's globally unique name and resource lifecycle handler:
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
export const Table = Resource(
|
|
198
|
+
"dynamo::Table",
|
|
199
|
+
async function (
|
|
200
|
+
// the resource context (phase, previous state, etc.) is made available as the bound `this` param
|
|
201
|
+
this: Context<TableOutput>,
|
|
202
|
+
// the resource's ID (unique within the current Scope)
|
|
203
|
+
id: string,
|
|
204
|
+
// the resource input properties
|
|
205
|
+
props: TableInputs
|
|
206
|
+
): Promise<Table> {
|
|
207
|
+
// this function implement the CRUD resource lifecycle for an instance of this Resource
|
|
208
|
+
|
|
209
|
+
if (this.phase === "create") {
|
|
210
|
+
// (create logic)
|
|
211
|
+
} else if (this.phase === "update") {
|
|
212
|
+
// (update logic)
|
|
213
|
+
} else if (this.phase === "delete") {
|
|
214
|
+
// (delete logic)
|
|
215
|
+
|
|
216
|
+
// terminate the delete process early
|
|
217
|
+
return this.destroy();
|
|
218
|
+
}
|
|
219
|
+
// return the created/updated resource properties
|
|
220
|
+
return this(props);
|
|
221
|
+
}
|
|
222
|
+
);
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
<details>
|
|
226
|
+
<summary>Nitty gritty details on this pattern's design and oddities</summary>
|
|
227
|
+
I call this pattern the "pseudo class", designed to model a Resource with a CRUD lifecycle implemented with memoized async functions.
|
|
228
|
+
|
|
229
|
+
The `this` parameter in this "pseudo class" serves many purposes:
|
|
230
|
+
|
|
231
|
+
1. contains the resource' `phase` (`create`, `update`, `delete`)
|
|
232
|
+
2. contains the resource's current state and previous props (`this.props`, `this.fqn`, `this.stage`, `this.scope`)
|
|
233
|
+
3. provides a handle to destroy the resource (`this.destroy`)
|
|
234
|
+
4. provides a factory for constructing the resource object (`this({..}`) - you can think of this as emulating `super({..})`
|
|
235
|
+
</details>
|
|
236
|
+
|
|
237
|
+
> [!TIP]
|
|
238
|
+
> Use Cursor or an LLM like Claude/OpenAI to generate the implementation of your resource. I think you'll be pleasantly surprised at how well it works, especially if you provide the API reference docs in your context.
|
|
239
|
+
|
|
240
|
+
That's it! Now you can instantiate DynamoDB Tables:
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
const table = await Table("items", {
|
|
244
|
+
name: "items",
|
|
245
|
+
//..
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
table.tableArn; // string
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Secrets
|
|
252
|
+
|
|
253
|
+
Recall that the `alchemy` function accepts a `password` property:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
await using app = alchemy("my-app", {
|
|
257
|
+
// password for encrypting/decrypting secrets stored in state
|
|
258
|
+
password: process.env.SECRET_PASSPHRASE,
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
This password is used to encrypt and decrypt secret data within an Alchemy state:
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
const OPENAI_API_KEY = alchemy.secret(process.env.OPENAI_API_KEY);
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Now, I can pass this secret to a Resource safely:
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
await Worker("my-func", {
|
|
272
|
+
bindings: {
|
|
273
|
+
OPENAI_API_KEY,
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
In our `.alchemy/` state, the property will be encrypted instead of plain text:
|
|
279
|
+
|
|
280
|
+
```json
|
|
281
|
+
{
|
|
282
|
+
"props": {
|
|
283
|
+
"bindings": {
|
|
284
|
+
"OPENAI_API_KEY": {
|
|
285
|
+
"@secret": "Tgz3e/WAscu4U1oanm5S4YXH..."
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Resource Scope Tree
|
|
293
|
+
|
|
294
|
+
Alchemy manages resources with a named tree of `Scope`s, similar to a file system. Each Scope has a name and contains named Resources and other (named) Scopes.
|
|
295
|
+
|
|
296
|
+
### Application Scope
|
|
297
|
+
|
|
298
|
+
The `alchemy` bootstrap (in your `alchemy.config.ts`) creates and binds to the Alchemy Application Scope (aka. "Root Scope"):
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
await using app = alchemy("my-app", {
|
|
302
|
+
stage: "prod",
|
|
303
|
+
// ..
|
|
304
|
+
});
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
To get a better understanding, notice how it has 1:1 correspondence with the `.alchemy/` state files:
|
|
308
|
+
|
|
309
|
+
```sh
|
|
310
|
+
.alchemy/
|
|
311
|
+
my-app/ # app scope
|
|
312
|
+
prod/ # stage scope
|
|
313
|
+
my-role.json # resource instance
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### Stage Scope
|
|
317
|
+
|
|
318
|
+
When you create an app, you can also specify a `stage`.
|
|
319
|
+
|
|
320
|
+
Stage is just an opinionated Scope placed under the root useful as a convention for isolating "stages" such as `prod`, `dev`, `$USER`.
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
await using app = alchemy("my-app", {
|
|
324
|
+
// scope: my-app/prod
|
|
325
|
+
stage: "prod",
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Instance Scope
|
|
330
|
+
|
|
331
|
+
Each Resource instance has its own scope to isolate Resources created in its Lifecycle Handler:
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
export const MyResource = Resource(
|
|
335
|
+
"my::Resource",
|
|
336
|
+
async function (this, id, props) {
|
|
337
|
+
if (this.phase === "delete") {
|
|
338
|
+
return this.destroy();
|
|
339
|
+
}
|
|
340
|
+
await Role("my-role");
|
|
341
|
+
await Worker("my-worker");
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
When you create an instance of `MyResource`, its nested Resources will be scoped to the Resource Instance:
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
await MyResource("instance");
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
```sh
|
|
353
|
+
.alchemy/
|
|
354
|
+
my-app/ # app
|
|
355
|
+
prod/ # stage
|
|
356
|
+
instance.json # instance
|
|
357
|
+
instance/ # instance scope
|
|
358
|
+
my-role.json # instance
|
|
359
|
+
my-worker.json # instance
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### Nested Scopes
|
|
363
|
+
|
|
364
|
+
Nested Scopes are stored within their parent Scope's state folder:
|
|
365
|
+
|
|
366
|
+
```sh
|
|
367
|
+
.alchemy/
|
|
368
|
+
my-app/ # app
|
|
369
|
+
prod/ # stage
|
|
370
|
+
nested/ # scope
|
|
371
|
+
my-worker.json # instance
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
> [!TIP]
|
|
375
|
+
> Scopes can be nested arbitrarily.
|
|
376
|
+
|
|
377
|
+
### `alchemy.scope`
|
|
378
|
+
|
|
379
|
+
You can create and "enter" a Nested Scope synchronously in a function. This will create and set the current async context's Scope (using AsyncLocalStorage):
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
await using scope = alchemy.scope("nested");
|
|
383
|
+
|
|
384
|
+
// resources created AFTER are placed in the "nested' Scope
|
|
385
|
+
await Worker("my-worker");
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### `alchemy.run`
|
|
389
|
+
|
|
390
|
+
You can also create nested scopes using the `alchemy.run` function and a closure:
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
await alchemy.run("nested", async () => {
|
|
394
|
+
// resources created in here are isolated to the "nested' Scope
|
|
395
|
+
await Worker("my-worker");
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
// resources out here are placed in the "parent" SCope
|
|
399
|
+
await Worker("my-worker");
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Get the current Scope
|
|
403
|
+
|
|
404
|
+
The current Scope is stored in `AsyncLocalStorage` and accessible when needed:
|
|
405
|
+
|
|
406
|
+
```ts
|
|
407
|
+
Scope.current; // will throw if not in a scope
|
|
408
|
+
Scope.get(); // Scope | undefined
|
|
409
|
+
await alchemy.run("nested", async (scope) => {
|
|
410
|
+
// scope is passed in as an argument
|
|
411
|
+
});
|
|
412
|
+
// create a Scope and bind to the current async context
|
|
413
|
+
using scope = alchemy.scope("nested");
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
## `destroy`
|
|
417
|
+
|
|
418
|
+
`Scope`, `Resource` and `ResourcePromise` can be "destroyed" individually and programmatically.
|
|
419
|
+
|
|
420
|
+
### Destroy a Resource
|
|
421
|
+
|
|
422
|
+
Say, you've got some two resources, a `Role` and a `Function`.
|
|
423
|
+
|
|
424
|
+
```ts
|
|
425
|
+
const role = await Role("my-role", {
|
|
426
|
+
name: "my-role",
|
|
427
|
+
//..
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
const func = await Function("my-function", {
|
|
431
|
+
name: "my-function",
|
|
432
|
+
role: role.roleArn,
|
|
433
|
+
//..
|
|
434
|
+
});
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Each of these Resources is known as a "sub-graph".
|
|
438
|
+
|
|
439
|
+
In this case we have `Role` (a 1-node graph, `Role`), and `Function` (a 2-node graph, `Role → Function`).
|
|
440
|
+
|
|
441
|
+
Each sub-graph can be "applied" or "destroyed" individually using the `apply` and `destroy` functions:
|
|
442
|
+
|
|
443
|
+
```ts
|
|
444
|
+
import { destroy } from "alchemy";
|
|
445
|
+
|
|
446
|
+
await destroy(func); // will delete just the Function
|
|
447
|
+
|
|
448
|
+
// destroy deletes the resource and any downstream dependencies
|
|
449
|
+
// so, if you want to delete Role AND Function, you should call destroy(role)
|
|
450
|
+
await destroy(role); // will delete Role and then Function
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### Destroy a Scope
|
|
454
|
+
|
|
455
|
+
You can destroy all Resources in a Scope with a single `destroy` call:
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
const scope = alchemy.scope("scope");
|
|
459
|
+
try {
|
|
460
|
+
await Role("role");
|
|
461
|
+
await Worker("worker");
|
|
462
|
+
} finally {
|
|
463
|
+
// destroy them all!
|
|
464
|
+
await destroy(scope);
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
### Destroy the App
|
|
469
|
+
|
|
470
|
+
To destroy the whole app (aka. the whole graph), you can call `alchemy` with the `phase: "destroy"` option. This will delete all resources in the specified or default stage.
|
|
471
|
+
|
|
472
|
+
```ts
|
|
473
|
+
await using _ = alchemy({
|
|
474
|
+
phase: "destroy",
|
|
475
|
+
// ..
|
|
476
|
+
});
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
> [!TIP]
|
|
480
|
+
> Alchemy is designed to have the minimum number of opinions as possible. This "embeddable" design is so that you can implement your own tools around Alchemy, e.g. a CLI or UI, instead of being stuck with a specific tool.
|
|
481
|
+
>
|
|
482
|
+
> ```ts
|
|
483
|
+
> await using _ = alchemy({
|
|
484
|
+
> // decide the mode/stage however you want, e.g. a CLI parser
|
|
485
|
+
> phase: process.argv[2] === "destroy" ? "destroy" : "up",
|
|
486
|
+
> stage: process.argv[3],
|
|
487
|
+
> });
|
|
488
|
+
> ```
|
|
489
|
+
|
|
490
|
+
## Test Resources
|
|
491
|
+
|
|
492
|
+
> [!NOTE]
|
|
493
|
+
> TODO
|
|
494
|
+
|
|
495
|
+
## Physical Names
|
|
496
|
+
|
|
497
|
+
> [!CAUTION]
|
|
498
|
+
> It is up to you to ensure that the physical names of resources don't conflict - alchemy does not (yet) offer any help or opinions here. You must decide on physical names, but you're free to add name generation logic to your resources if you so desire.
|
|
499
|
+
>
|
|
500
|
+
> ```ts
|
|
501
|
+
> const Table = Resource("dynamo::Table", async function (this, inputs) {
|
|
502
|
+
> const tableName = `${this.stage}-${inputs.tableName}`;
|
|
503
|
+
>
|
|
504
|
+
> // ..
|
|
505
|
+
> });
|
|
506
|
+
> ```
|
package/lib/alchemy.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { destroy } from "./destroy";
|
|
1
|
+
import { DestroyedSignal, destroy } from "./destroy";
|
|
2
2
|
import { Scope } from "./scope";
|
|
3
3
|
import { secret } from "./secret";
|
|
4
4
|
// TODO: support browser
|
|
@@ -36,5 +36,13 @@ async function run(...args) {
|
|
|
36
36
|
? [args[0], undefined, args[1]]
|
|
37
37
|
: args;
|
|
38
38
|
await using scope = alchemy.scope(id, options);
|
|
39
|
-
|
|
39
|
+
try {
|
|
40
|
+
return await fn.bind(scope)(scope);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (!(error instanceof DestroyedSignal)) {
|
|
44
|
+
scope.fail();
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
40
48
|
}
|
package/lib/apply.js
CHANGED
|
@@ -4,89 +4,98 @@ import { PROVIDERS, } from "./resource";
|
|
|
4
4
|
import { serialize } from "./util/serde";
|
|
5
5
|
export async function apply(resource, props, options) {
|
|
6
6
|
const scope = resource.Scope;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
state
|
|
7
|
+
try {
|
|
8
|
+
const quiet = props.quiet ?? scope.quiet;
|
|
9
|
+
await scope.init();
|
|
10
|
+
let state = (await scope.state.get(resource.ID));
|
|
11
|
+
const provider = PROVIDERS.get(resource.Kind);
|
|
12
|
+
if (provider === undefined) {
|
|
13
|
+
throw new Error(`Provider "${resource.Kind}" not found`);
|
|
14
|
+
}
|
|
15
|
+
if (state === undefined) {
|
|
16
|
+
state = {
|
|
17
|
+
kind: resource.Kind,
|
|
18
|
+
id: resource.ID,
|
|
19
|
+
fqn: resource.FQN,
|
|
20
|
+
seq: resource.Seq,
|
|
21
|
+
status: "creating",
|
|
22
|
+
data: {},
|
|
23
|
+
output: {
|
|
24
|
+
ID: resource.ID,
|
|
25
|
+
FQN: resource.FQN,
|
|
26
|
+
Kind: resource.Kind,
|
|
27
|
+
Scope: scope,
|
|
28
|
+
Seq: resource.Seq,
|
|
29
|
+
},
|
|
30
|
+
// deps: [...deps],
|
|
31
|
+
props,
|
|
32
|
+
};
|
|
33
|
+
await scope.state.set(resource.ID, state);
|
|
34
|
+
}
|
|
35
|
+
const alwaysUpdate = options?.alwaysUpdate ?? provider.options?.alwaysUpdate ?? false;
|
|
36
|
+
// Skip update if inputs haven't changed and resource is in a stable state
|
|
37
|
+
if (state.status === "created" || state.status === "updated") {
|
|
38
|
+
if (JSON.stringify(state.props) ===
|
|
39
|
+
JSON.stringify(await serialize(scope, props)) &&
|
|
40
|
+
alwaysUpdate !== true) {
|
|
41
|
+
if (!quiet) {
|
|
42
|
+
console.log(`Skip: "${resource.FQN}" (no changes)`);
|
|
43
|
+
}
|
|
44
|
+
// if (resourceState.output !== undefined) {
|
|
45
|
+
// resource[Provide](resourceState.output);
|
|
46
|
+
// }
|
|
47
|
+
return state.output;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const phase = state.status === "creating" ? "create" : "update";
|
|
51
|
+
state.status = phase === "create" ? "creating" : "updating";
|
|
52
|
+
state.oldProps = state.props;
|
|
53
|
+
state.props = props;
|
|
54
|
+
if (!quiet) {
|
|
55
|
+
console.log(`${phase === "create" ? "Create" : "Update"}: "${resource.FQN}"`);
|
|
56
|
+
}
|
|
57
|
+
await scope.state.set(resource.ID, state);
|
|
58
|
+
let isReplaced = false;
|
|
59
|
+
const ctx = context({
|
|
60
|
+
scope,
|
|
61
|
+
phase,
|
|
16
62
|
kind: resource.Kind,
|
|
17
63
|
id: resource.ID,
|
|
18
64
|
fqn: resource.FQN,
|
|
19
65
|
seq: resource.Seq,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Seq: resource.Seq,
|
|
66
|
+
state,
|
|
67
|
+
replace: () => {
|
|
68
|
+
if (isReplaced) {
|
|
69
|
+
console.warn(`Resource ${resource.Kind} ${resource.FQN} is already marked as REPLACE`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
isReplaced = true;
|
|
28
73
|
},
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// Skip update if inputs haven't changed and resource is in a stable state
|
|
36
|
-
if (state.status === "created" || state.status === "updated") {
|
|
37
|
-
if (JSON.stringify(state.props) === JSON.stringify(serialize(scope, props)) &&
|
|
38
|
-
alwaysUpdate !== true) {
|
|
39
|
-
if (!quiet) {
|
|
40
|
-
console.log(`Skip: "${resource.FQN}" (no changes)`);
|
|
41
|
-
}
|
|
42
|
-
// if (resourceState.output !== undefined) {
|
|
43
|
-
// resource[Provide](resourceState.output);
|
|
44
|
-
// }
|
|
45
|
-
return state.output;
|
|
74
|
+
});
|
|
75
|
+
const output = await alchemy.run(resource.ID, async () => {
|
|
76
|
+
return provider.handler.bind(ctx)(resource.ID, props);
|
|
77
|
+
});
|
|
78
|
+
if (!quiet) {
|
|
79
|
+
console.log(`${phase === "create" ? "Created" : "Updated"}: "${resource.FQN}"`);
|
|
46
80
|
}
|
|
81
|
+
await scope.state.set(resource.ID, {
|
|
82
|
+
kind: resource.Kind,
|
|
83
|
+
id: resource.ID,
|
|
84
|
+
fqn: resource.FQN,
|
|
85
|
+
seq: resource.Seq,
|
|
86
|
+
data: state.data,
|
|
87
|
+
status: phase === "create" ? "created" : "updated",
|
|
88
|
+
output,
|
|
89
|
+
props,
|
|
90
|
+
// deps: [...deps],
|
|
91
|
+
});
|
|
92
|
+
// if (output !== undefined) {
|
|
93
|
+
// resource[Provide](output as Out);
|
|
94
|
+
// }
|
|
95
|
+
return output;
|
|
47
96
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
state.props = props;
|
|
52
|
-
if (!quiet) {
|
|
53
|
-
console.log(`${phase === "create" ? "Create" : "Update"}: "${resource.FQN}"`);
|
|
54
|
-
}
|
|
55
|
-
await scope.state.set(resource.ID, state);
|
|
56
|
-
let isReplaced = false;
|
|
57
|
-
const ctx = context({
|
|
58
|
-
scope,
|
|
59
|
-
phase,
|
|
60
|
-
kind: resource.Kind,
|
|
61
|
-
id: resource.ID,
|
|
62
|
-
fqn: resource.FQN,
|
|
63
|
-
seq: resource.Seq,
|
|
64
|
-
state,
|
|
65
|
-
replace: () => {
|
|
66
|
-
if (isReplaced) {
|
|
67
|
-
console.warn(`Resource ${resource.Kind} ${resource.FQN} is already marked as REPLACE`);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
isReplaced = true;
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
const output = await alchemy.run(resource.ID, async () => provider.handler.bind(ctx)(resource.ID, props));
|
|
74
|
-
if (!quiet) {
|
|
75
|
-
console.log(`${phase === "create" ? "Created" : "Updated"}: "${resource.FQN}"`);
|
|
97
|
+
catch (error) {
|
|
98
|
+
scope.fail();
|
|
99
|
+
throw error;
|
|
76
100
|
}
|
|
77
|
-
await scope.state.set(resource.ID, {
|
|
78
|
-
kind: resource.Kind,
|
|
79
|
-
id: resource.ID,
|
|
80
|
-
fqn: resource.FQN,
|
|
81
|
-
seq: resource.Seq,
|
|
82
|
-
data: state.data,
|
|
83
|
-
status: phase === "create" ? "created" : "updated",
|
|
84
|
-
output,
|
|
85
|
-
props,
|
|
86
|
-
// deps: [...deps],
|
|
87
|
-
});
|
|
88
|
-
// if (output !== undefined) {
|
|
89
|
-
// resource[Provide](output as Out);
|
|
90
|
-
// }
|
|
91
|
-
return output;
|
|
92
101
|
}
|