Scope
A Scope represents a hierarchical container for resources in Alchemy. Scopes manage resource state, lifecycle, and provide utilities for resource management. The root scope is created by callingalchemy(), and child scopes can be created using alchemy.run().
Properties
string
The name of the scope.
string
The identifier for this scope level.
string
The name of the root application.
Scope | undefined
The parent scope, or undefined if this is the root scope.
string
The stage name for this scope (e.g., “dev”, “prod”).
'up' | 'destroy' | 'read'
The current lifecycle phase.
boolean
Whether resources should be simulated locally.
boolean
Whether to watch for changes and automatically update resources.
boolean
Whether to suppress log output.
boolean
Whether to force resource updates.
boolean
Whether to adopt existing resources.
string | undefined
The password used for encrypting secrets in this scope.
Map<string, PendingResource>
Map of all resources in this scope.
Map<string, Scope>
Map of child scopes.
Scope
The root scope of the application.
Methods
createPhysicalName()
Creates a physical name for a resource based on the application, stage, and resource ID.string
required
The resource identifier.
string
default:"'-'"
The delimiter to use between name components.
number
Maximum length for the physical name. If exceeded, components will be truncated.
{appName}-{scope-chain}-{id}-{stage}
Example:
fqn()
Generates a fully qualified name for a resource.string
required
The resource identifier.
{appName}/{scope-chain}/{resourceID}
Example:
has()
Checks if a resource with the given ID exists in the scope’s state.string
required
The resource identifier.
string
Optional resource type to match.
true if the resource exists (and matches the type if provided)
get()
Retrieves a value from the scope’s data storage.string
required
The key to retrieve.
set()
Stores a value in the scope’s data storage.string
required
The key to store.
T
required
The value to store.
delete()
Deletes a value from the scope’s data storage.string
required
The key to delete.
run()
Runs a function within this scope’s context.function
required
The async function to run within the scope.
finalize()
Finalizes the scope, destroying orphaned resources and cleaning up.boolean
default:"false"
Force finalization even if not the root scope.
boolean
default:"false"
Skip actual deletion operations.
defer()
Defers execution of a function until the scope finalizes.function
required
The async function to defer.
spawn()
Spawns an idempotent process managed by the scope.string
required
Unique identifier for the process.
string
required
The command to execute.
string[]
Command arguments.
string
Working directory for the command.
Record<string, string>
Environment variables.
function
Function to extract a value from process output lines.
extract is provided, otherwise undefined.
Example:
exec()
Executes a command and returns the result.string
required
Unique identifier for logging.
string
required
The command to execute.
onCleanup()
Registers a cleanup function to run when the process exits.function
required
The async cleanup function.
Static Methods
Scope.current
Gets the current scope from the async context.Scope.root
Gets the root scope of the current application.Examples
Creating a Root Scope
Creating Child Scopes
Using Scope Data Storage
Deferred Operations
Related
- alchemy() - Create the root scope
- alchemy.run() - Run code in a child scope
- Context - Resource context API