Skip to main content

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 calling alchemy(), 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.
Returns: A physical name in the format {appName}-{scope-chain}-{id}-{stage} Example:

fqn()

Generates a fully qualified name for a resource.
string
required
The resource identifier.
Returns: Fully qualified name in the format {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.
Returns: 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.
Returns: The stored value.

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.
Returns: The result of the function. Example:

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.
Example:

defer()

Defers execution of a function until the scope finalizes.
function
required
The async function to defer.
Returns: A promise that resolves when the deferred function completes. Example:

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.
Returns: Extracted value if 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.
Returns: Object containing exit code and output.

onCleanup()

Registers a cleanup function to run when the process exits.
function
required
The async cleanup function.
Example:

Static Methods

Scope.current

Gets the current scope from the async context.
Returns: The current scope. Throws: Error if not running within an Alchemy scope. Example:

Scope.root

Gets the root scope of the current application.
Returns: The root scope.

Examples

Creating a Root Scope

Creating Child Scopes

Using Scope Data Storage

Deferred Operations