Skip to content

Creating Commands

Use a Command to represent an application operation that the Xeno.JS CQRS pipeline sends to its Handler. A Command can also declare cache keys or patterns to invalidate when query caching is enabled.

A Command consists of:

  1. a class extending Command;
  2. an intent identifying the Command;
  3. the data required by the operation;
  4. a Handler registered with the same intent;
  5. optionally, validation and other Command pipeline features.

You need:

  • an XenoAppBuilder;
  • the CQRS pipeline enabled with addPipeline();
  • a Command class;
  • a Handler registered with the Command intent;
  • execution within an active service scope (or use of runInScope() outside an existing request).

If you want validation, you also need a Zod schema.

addPipeline() registers the CQRS infrastructure used to execute Commands and Queries.

import { XenoAppBuilder } from 'xeno-js'
const app = new XenoAppBuilder()
.addPipeline()
await app.build()

Pass pipeline plugins to addPipeline() to enable optional behaviors such as validation, idempotency, concurrency, or performance tracking. The CQRS module also includes exception handling and logging behaviors.


Extend the Command class and pass the Command intent to super().

import { Command } from '@xeno-js/shared'
export class CreateUserCommand extends Command<User> {
constructor(
public readonly email: string,
public readonly name: string,
) {
super('CreateUserCommand')
}
}

The Command base class automatically sets:

command.type === REQUEST_TYPE.COMMAND

Define the data required by the application operation. The optional second constructor argument can also declare cache keys or patterns to invalidate after a successful Command, when query caching is enabled.

super('CreateUserCommand', {
keys: ['users:list'],
patterns: ['user:*'],
})

The intent identifies the Command and is used to resolve its Handler and any intent-specific pipeline configuration.

In this example:

super('CreateUserCommand')

means that the Handler must be registered with:

CreateUserCommand

as its service token.

Command properties should contain the input required by the application operation.

For example:

export class CreateUserCommand extends Command<User> {
constructor(
public readonly email: string,
public readonly name: string,
) {
super('CreateUserCommand')
}
}

The resulting Command contains:

{
intent: 'CreateUserCommand',
type: 'COMMAND',
email: 'john@example.com',
name: 'John Doe',
}

You do not need to manually add intent or type to the class.


If the Command accepts external input, add a validation plugin to addPipeline() to validate it before the Handler runs.

First enable validation:

import { XenoAppBuilder } from 'xeno-js'
import { withZodValidation } from '@xeno-js/zod'
const app = new XenoAppBuilder().addPipeline(
withZodValidation((options) => {
options.schemas = {}
}),
)
await app.build()

Then register the schema using the Command intent as the key.

For example:

import { z } from 'zod'
import { XenoAppBuilder } from 'xeno-js'
import { withZodValidation } from '@xeno-js/zod'
const createUserSchema = z.object({
intent: z.literal('CreateUserCommand'),
type: z.literal('COMMAND'),
email: z.string().email(),
name: z.string().min(1),
})
const app = new XenoAppBuilder().addPipeline(
withZodValidation((options) => {
options.schemas = {
CreateUserCommand: createUserSchema,
}
}),
)
await app.build()

The schema key must match:

super('CreateUserCommand')

The validation pipeline uses the Command’s intent to select the schema.

For larger applications, keep the Command and its schema together:

src/
└── application/
└── users/
├── create-user.command.ts
└── create-user.schema.ts

For example:

create-user.schema.ts
import { z } from 'zod'
export const createUserSchema = z.object({
intent: z.literal('CreateUserCommand'),
type: z.literal('COMMAND'),
email: z.string().email(),
name: z.string().min(1),
})

Then register it during application bootstrap:

import { XenoAppBuilder } from 'xeno-js'
import { withZodValidation } from '@xeno-js/zod'
import { createUserSchema } from './application/users/create-user.schema'
const app = new XenoAppBuilder().addPipeline(
withZodValidation((options) => {
options.schemas = {
CreateUserCommand: createUserSchema,
}
}),
)
await app.build()

For more details about validation behavior and custom validation, see Validation.


A Command must have a Handler registered with the same intent.

For the previous example:

super('CreateUserCommand')

the Handler must be registered as:

CreateUserCommand

A Handler extends BaseHandler and implements executeAsync().

import { BaseHandler } from 'xeno-js'
import { Result, type ResultType } from '@xeno-js/shared'
import type { CreateUserCommand, User } from './create-user.command'
export class CreateUserHandler extends BaseHandler<
CreateUserCommand,
User
> {
protected async executeAsync(
request: CreateUserCommand,
): Promise<ResultType<User>> {
const user = {
id: crypto.randomUUID(),
email: request.email,
name: request.name,
}
return Result.ok(user)
}
}

See Creating Handlers for the complete Handler configuration and dependency injection patterns.


Register the Handler in the service container using the Command intent. BaseHandler requires a user-context factory; the context module provides it under TOKENS.USER_CONTEXT_FACTORY.

import { XenoAppBuilder } from 'xeno-js'
import { TOKENS } from '@xeno-js/shared'
import { CreateUserHandler } from './application/users/create-user.handler'
const app = new XenoAppBuilder()
.addServices((container) => {
container.addTransient(
'CreateUserCommand',
(scope) => {
return new CreateUserHandler(
scope.resolve(TOKENS.USER_CONTEXT_FACTORY),
)
},
)
})
.addPipeline()
await app.build()

The registration token:

'CreateUserCommand'

must match the Command intent:

super('CreateUserCommand')

If the names do not match, Xeno.JS cannot resolve the Handler when the Command is executed.

If your Handler requires application services, resolve them from the active service scope.

For example:

export class CreateUserHandler extends BaseHandler<
CreateUserCommand,
User
> {
constructor(
identityFactory: IFactory<void, UserContext>,
private readonly userRepository: UserRepository,
) {
super(identityFactory)
}
protected async executeAsync(
request: CreateUserCommand,
): Promise<ResultType<User>> {
const user = await this.userRepository.create({
email: request.email,
name: request.name,
})
return Result.ok(user)
}
}

Register the dependencies explicitly:

import { TOKENS } from '@xeno-js/shared'
const app = new XenoAppBuilder()
.addServices((container) => {
container.addScoped(
'USER_REPOSITORY',
(scope) => new UserRepository(
scope.resolve(TOKENS.DB_CONTEXT),
),
)
container.addTransient(
'CreateUserCommand',
(scope) => {
return new CreateUserHandler(
scope.resolve(TOKENS.USER_CONTEXT_FACTORY),
scope.resolve('USER_REPOSITORY'),
)
},
)
})
.addPipeline()
await app.build()

Xeno.JS uses explicit dependency registration. The Handler’s dependencies are resolved from the service scope passed to the factory.

For more information, see Dependency Injection.


Once the Command and Handler are registered, execute the Command through the mediator inside an active service scope. For non-HTTP work, runInScope() creates that scope and disposes it when the operation completes.

import { TOKENS } from '@xeno-js/shared'
const command = new CreateUserCommand(
'john@example.com',
'John Doe',
)
const application = await app.build()
const result = await application.runInScope(async (services) => {
const mediator = services[TOKENS.MEDIATOR]
return mediator.send(command, new AbortController().signal)
})

The mediator executes the Command pipeline and eventually resolves the Handler using:

command.intent

For this Command:

command.intent === 'CreateUserCommand'

so the mediator resolves the Handler registered with:

'CreateUserCommand'

The returned value is a ResultType<TResponse>.

Handle the result according to your application’s error-handling conventions.


A simple Command implementation can be organized as follows:

src/
├── application/
│ └── users/
│ ├── create-user.command.ts
│ ├── create-user.handler.ts
│ └── create-user.schema.ts
└── bootstrap.ts
application/users/create-user.command.ts
import { Command } from '@xeno-js/shared'
export interface User {
id: string
email: string
name: string
}
export class CreateUserCommand extends Command<User> {
constructor(
public readonly email: string,
public readonly name: string,
) {
super('CreateUserCommand')
}
}
application/users/create-user.schema.ts
import { z } from 'zod'
export const createUserSchema = z.object({
intent: z.literal('CreateUserCommand'),
type: z.literal('COMMAND'),
email: z.string().email(),
name: z.string().min(1),
})
application/users/create-user.handler.ts
import { BaseHandler } from 'xeno-js'
import { Result, type ResultType } from '@xeno-js/shared'
import type { CreateUserCommand, User } from './create-user.command'
export class CreateUserHandler extends BaseHandler<
CreateUserCommand,
User
> {
protected async executeAsync(
request: CreateUserCommand,
): Promise<ResultType<User>> {
const user: User = {
id: crypto.randomUUID(),
email: request.email,
name: request.name,
}
return Result.ok(user)
}
}
bootstrap.ts
import { XenoAppBuilder } from 'xeno-js'
import { TOKENS } from '@xeno-js/shared'
import { withZodValidation } from '@xeno-js/zod'
import { createUserSchema } from './application/users/create-user.schema'
import { CreateUserHandler } from './application/users/create-user.handler'
const builder = new XenoAppBuilder()
.addServices((container) => {
container.addTransient(
'CreateUserCommand',
(scope) => {
return new CreateUserHandler(
scope.resolve(TOKENS.USER_CONTEXT_FACTORY),
)
},
)
})
.addPipeline(withZodValidation((options) => {
options.schemas = {
CreateUserCommand: createUserSchema,
}
}))
const application = await builder.build()
await application.runInScope(async (services) => {
const mediator = services[TOKENS.MEDIATOR]
return mediator.send(
new CreateUserCommand('john@example.com', 'John Doe'),
new AbortController().signal,
)
})

The important relationship is:

CreateUserCommand
│
│ intent = "CreateUserCommand"
▼
"CreateUserCommand" service registration
│
▼
CreateUserHandler

The same intent connects the Command to its Handler and to its validation schema.


Idempotency is a Command-specific pipeline feature.

Add the withIdempotency plugin to the Command pipeline:

import { XenoAppBuilder, withIdempotency } from 'xeno-js'
const app = new XenoAppBuilder()
.addPipeline(withIdempotency((options) => {
options.lockTtlSeconds = 300
options.processedTtlSeconds = 86400
}))
await app.build()

The idempotency mechanism uses the active network context’s requestId to identify an operation; it does not read an ID from the Command object. The options also support per-intent settings through intentRegistry.

See Idempotency for the complete configuration and behavior.


Concurrency retry handling is also Command-specific.

import { XenoAppBuilder, withConcurrency } from 'xeno-js'
const app = new XenoAppBuilder()
.addPipeline(withConcurrency((options) => {
options.maxRetries = 3
options.delayConfig = {
baseDelayMs: 100,
maxJitterMs: 50,
}
}))
await app.build()

See Concurrency for the complete configuration and retry behavior.


Commands can also use the common CQRS pipeline features:

Add the corresponding pipeline plugins to addPipeline() where required. addPipeline() configures the CQRS module and does not take a configuration callback.


If executing a Command produces a HANDLER_NOT_FOUND error, check that:

  1. the Command has an intent;
  2. the Handler is registered in the service container;
  3. the registration token exactly matches the Command intent;
  4. the Handler exposes a handle() method through BaseHandler;
  5. addPipeline() has been called;
  6. the application has been built with await app.build().

For example:

super('CreateUserCommand')

must correspond to:

import { TOKENS } from '@xeno-js/shared'
container.addTransient(
'CreateUserCommand',
(scope) => new CreateUserHandler(
scope.resolve(TOKENS.USER_CONTEXT_FACTORY),
),
)

Check that:

  1. addPipeline(withZodValidation(...)) is configured;
  2. the Command runs inside an active service scope;
  3. the schema is registered under the exact Command intent;
  4. the Command is executed through the mediator;
  5. the schema validates the Command request shape.

For example:

withZodValidation((options) => {
options.schemas = {
CreateUserCommand: createUserSchema,
}
})

must match:

super('CreateUserCommand')

The Handler is not receiving the expected data

Section titled “The Handler is not receiving the expected data”

The Command class defines the data passed to the Handler:

export class CreateUserCommand extends Command<User> {
constructor(
public readonly email: string,
public readonly name: string,
) {
super('CreateUserCommand')
}
}

The Handler receives the same Command instance:

protected async executeAsync(
request: CreateUserCommand,
) {
request.email
request.name
}

If validation is enabled, remember that the schema validates the request object before the Handler executes.



Xeno.JS is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section