compose.up
compose.up
is the primary function used to initialize list of containers, ensuring they are set up in the correct order based on their dependencies. This function manages the execution flow and provides optional debugging output.
Syntax
Parameters
- containers (
Container[]
): An array of containers to initialize. Each container in the array is processed based on its dependencies (dependsOn
andoptionalDependsOn
), regardless of the order in which it appears. - config (optional,
{ debug?: boolean, apis?: boolean, autoResolveDeps?: { strict: true, optional?: boolean } }
): An optional configuration object with the following properties:- debug (
boolean
): Iftrue
,compose.up
will output detailed status updates to the console each time a container’s status changes. This is useful for tracking initialization and troubleshooting dependency issues. - apis (
boolean
): If set totrue
, the returned result fromcompose.up
will include anapis
object. In this object, each key is theid
of a container, and the corresponding value is theapi
from the container’sstart
function. The keys inapis
are always optional because, at the type level, we cannot determine if a container has transitioned to thedone
status. - autoResolveDeps (
{ strict: true, optional?: boolean }
): Allows automatic resolution of dependencies. Ifstrict
is set totrue
, all strict dependencies are resolved automatically. Theoptional
property, if set totrue
, enables automatic resolution of optional dependencies as well, eliminating the need to manually pass them tocompose.up
. - onFail (
function
): An optional callback that is called when a container transitions to thefail
state. Receives an object with the container’sid
and theerror
that caused the failure. This is useful for logging, error tracking, or triggering recovery mechanisms.
- debug (
Return Value
compose.up
returns a promise that resolves to an object with the following structure:
- hasErrors (
boolean
): Indicates whether any container encountered an error during initialization. - statuses (
Record<Container['id'], Container['status']>
): An object that maps each container ID to its final status (idle
,pending
,done
,fail
, oroff
). - apis (
Record<Container['id'], Container['api']>
): Ifcompose.up.config
set totrue
Example
Expected Resolution Order
How autoResolveDeps
Works
When autoResolveDeps
is used, compose.up
will automatically identify and include all necessary dependencies for each container. This feature is particularly useful when dealing with complex dependency graphs where manually listing all containers is error-prone and difficult to manage.