generate Commands
Generate boilerplate code with the generate (or g) command.
Usage
stratix generate <generator> <name> [options]
stratix g <generator> <name> [options]
Available Generators
| Generator | Description |
|---|---|
entity | Domain entity |
value-object | Value object |
command | CQRS command |
query | CQRS query |
repository | Repository interface |
context | Bounded context (modular architecture) |
quality | Quality checks (tests, linting) |
entity Generator
Generate a domain entity:
stratix generate entity Product
stratix g entity User --path src/domain/entities
Generated files:
product.entity.ts- Entity classproduct.entity.spec.ts- Unit tests
Example output:
import { Entity, EntityId } from '@stratix/core';
export interface ProductProps {
name: string;
price: number;
stock: number;
}
export class Product extends Entity<ProductProps> {
private constructor(
id: EntityId<'Product'>,
props: ProductProps,
createdAt: Date,
updatedAt: Date
) {
super(id, props, createdAt, updatedAt);
}
static create(props: ProductProps): Product {
return new Product(
EntityId.create<'Product'>(),
props,
new Date(),
new Date()
);
}
get name(): string {
return this.props.name;
}
get price(): number {
return this.props.price;
}
get stock(): number {
return this.props.stock;
}
}
value-object Generator
Generate a value object:
stratix generate value-object Email
stratix g value-object Price --path src/domain/value-objects
Generated files:
email.vo.ts- Value object classemail.vo.spec.ts- Unit tests
Example output:
import { ValueObject, Result, Success, Failure } from '@stratix/core';
export interface EmailProps {
value: string;
}
export class Email extends ValueObject<EmailProps> {
private constructor(props: EmailProps) {
super(props);
}
static create(value: string): Result<Email> {
if (!value || value.trim().length === 0) {
return Failure.create('Email cannot be empty');
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(value)) {
return Failure.create('Invalid email format');
}
return Success.create(new Email({ value }));
}
get value(): string {
return this.props.value;
}
}
command Generator
Generate a CQRS command:
stratix generate command CreateProduct
stratix g command UpdateUser --path src/application/commands
Generated files:
create-product.command.ts- Command classcreate-product.handler.ts- Command handlercreate-product.handler.spec.ts- Unit tests
Example output:
// create-product.command.ts
import { Command } from '@stratix/core';
export class CreateProductCommand implements Command {
constructor(
public readonly name: string,
public readonly price: number,
public readonly stock: number
) {}
}
// create-product.handler.ts
import { CommandHandler, Result, Success } from '@stratix/core';
import { CreateProductCommand } from './create-product.command';
import { Product } from '../../domain/entities/product.entity';
export class CreateProductHandler
implements CommandHandler<CreateProductCommand, Product> {
constructor(
private productRepository: IProductRepository
) {}
async handle(command: CreateProductCommand): Promise<Result<Product>> {
const product = Product.create({
name: command.name,
price: command.price,
stock: command.stock
});
await this.productRepository.save(product);
return Success.create(product);
}
}
query Generator
Generate a CQRS query:
stratix generate query GetProductById
stratix g query GetAllUsers --path src/application/queries
Generated files:
get-product-by-id.query.ts- Query classget-product-by-id.handler.ts- Query handlerget-product-by-id.handler.spec.ts- Unit tests
repository Generator
Generate a repository interface:
stratix generate repository Product
stratix g repository User --path src/domain/repositories
Generated files:
product.repository.ts- Repository interfacepostgres-product.repository.ts- PostgreSQL implementation
context Generator
Generate a complete bounded context for modular architecture projects:
stratix generate context Order
stratix g context Product --props '[{"name":"name","type":"string"},{"name":"price","type":"number"}]'
Generated files:
order/domain/entities/Order.ts- Entity/Aggregate rootorder/domain/repositories/OrderRepository.ts- Repository interfaceorder/infrastructure/repositories/InMemoryOrderRepository.ts- In-memory implementationorder/application/commands/CreateOrder.ts- Create commandorder/application/commands/CreateOrderHandler.ts- Command handlerorder/application/queries/GetOrderById.ts- Get by ID queryorder/application/queries/GetOrderByIdHandler.ts- Query handlerorder/application/queries/ListOrders.ts- List queryorder/application/queries/ListOrdersHandler.ts- List handlerorder/index.ts- Barrel exports
Example output structure:
src/contexts/order/
├── domain/
│ ├── entities/
│ │ └── Order.ts
│ └── repositories/
│ └── OrderRepository.ts
├── application/
│ ├── commands/
│ │ ├── CreateOrder.ts
│ │ └── CreateOrderHandler.ts
│ └── queries/
│ ├── GetOrderById.ts
│ ├── GetOrderByIdHandler.ts
│ ├── ListOrders.ts
│ └── ListOrdersHandler.ts
├── infrastructure/
│ └── repositories/
│ └── InMemoryOrderRepository.ts
└── index.ts
With properties:
stratix g context Product --props '[
{"name":"name","type":"string"},
{"name":"price","type":"number"},
{"name":"stock","type":"number"}
]'
This creates a complete bounded context with the specified properties in the entity. The context generator is ideal for modular monolith projects where you want to keep related functionality together.
Note: This generator automatically creates all necessary files and intelligent dependencies. If an entity or repository is missing, it will be created automatically.
quality Generator
Generate quality checks:
stratix generate quality
stratix g quality --tests --lint
Generated files:
jest.config.js- Jest configuration.eslintrc.js- ESLint rules.prettierrc- Prettier config
Options
| Option | Description | Default |
|---|---|---|
--path | Output directory | Auto-detected |
--skip-tests | Skip test file generation | false |
--dry-run | Preview without creating files | false |
Examples
Generate entity with custom path
stratix g entity Product --path src/modules/catalog/domain
Generate command without tests
stratix g command CreateProduct --skip-tests
Preview generation
stratix g entity Product --dry-run
Best Practices
1. Use Consistent Naming
# ✅ Good: PascalCase
stratix g entity Product
stratix g command CreateProduct
# ❌ Bad: inconsistent
stratix g entity product
stratix g command create-product
2. Organize by Feature
stratix g entity Product --path src/domain/entities
stratix g command CreateProduct --path src/application/commands
3. Generate Tests
# Always generate tests (default)
stratix g entity Product
Next Steps
- add Command - Add extensions
- CLI Overview - All commands
- Domain Modeling - Entity patterns