-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# @ddd-master/command-bus package | ||
|
||
[![npm](https://img.shields.io/npm/v/@ddd-master/command-bus?color=blue&label=%40ddd-master%2Fcommand-bus)](https://www.npmjs.com/package/@ddd-master/command-bus) | ||
|
||
The `@ddd-master/command-bus` package is a TypeScript library that provides an implementation of the command bus pattern for Domain-Driven Design (DDD) applications. It allows you to execute commands and perform actions in a centralized and decoupled manner. | ||
|
||
## Installation | ||
|
||
You can install the `@ddd-master/command-bus` package using npm: | ||
|
||
```bash | ||
npm install @ddd-master/command-bus | ||
``` | ||
|
||
## Usage | ||
|
||
To use the `@ddd-master/command-bus` package, you need to define your commands and command handlers. | ||
|
||
### Defining Commands | ||
|
||
Commands represent actions or requests for changes in the system. You can define a command by implementing the `ICommand` interface. For example: | ||
|
||
```typescript | ||
export interface ICreateUserCommand extends ICommand { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
export class CreateUserCommand implements ICreateUserCommand { | ||
public readonly email: string; | ||
public readonly password: string; | ||
|
||
public constructor({ email, password }: ICreateUserCommand) { | ||
this.email = email; | ||
this.password = password; | ||
} | ||
} | ||
``` | ||
|
||
### Defining Command Handlers | ||
|
||
Command handlers are responsible for executing commands and performing the necessary actions. You can implement the `ICommandHandler` interface to define your command handlers. For example: | ||
|
||
```typescript | ||
export class CreateUserCommandHandler implements ICommandHandler<ICreateUserCommand, CreateUserCommandHandlerResult> { | ||
private readonly userRepository: UserRepository; | ||
public constructor({ userRepository }: Dependencies) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public handle(command: ICommand): CreateUserCommandHandlerResult { | ||
const { email, password } = command as ICreateUserCommand; | ||
|
||
const createdUser: User = { email, password }; | ||
|
||
this.userRepository.save({ user: createdUser }); | ||
|
||
return { email, password }; | ||
} | ||
} | ||
``` | ||
|
||
### Registering Command Handlers | ||
|
||
Once you have your command handlers, you need to register them with the command bus. For example: | ||
|
||
```typescript | ||
const commandBus = new CommandBus(); | ||
const createUserCommandHandler = new CreateUserCommandHandler(); | ||
commandBus.register(CreateUserCommand, createUserCommandHandler); | ||
``` | ||
|
||
### Executing Commands | ||
|
||
To execute a command, you can use the `execute` method of the command bus. For example: | ||
|
||
```typescript | ||
const result = commandBus.execute<ICreateUserCommand, CreateUserCommandHandlerResult>( | ||
new CreateUserCommand({ email: '[email protected]', password: '123456' }), | ||
); | ||
|
||
const { email, password } = result; | ||
console.log(email, password); | ||
``` | ||
The `execute` method returns the result of the command handler. You can access the result and process it accordingly. | ||
|
||
## Contributing | ||
|
||
Contributions to the `@ddd-master/command-bus` package are welcome! Feel free to open issues or submit pull requests on the [GitHub repository](https://github.com/BartekCK/ddd-master/tree/master/libs/command-bus). Please follow the [code of conduct](https://github.com/BartekCK/ddd-master/blob/master/CODE_OF_CONDUCT.md) when contributing. | ||
|
||
## License | ||
|
||
The `@ddd-master/command-bus` package is open-source software licensed under the [MIT License](https://github.com/BartekCK/ddd-master/blob/master/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"url": "https://github.com/BartekCK/ddd-master.git", | ||
"directory": "libs/command-bus" | ||
}, | ||
"license": "MIT", | ||
"description": "Command bus implementation", | ||
"author": { | ||
"email": "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# @ddd-master/query-bus package | ||
|
||
[![npm](https://img.shields.io/npm/v/@ddd-master/query-bus?color=blue&label=%40ddd-master%2Fquery-bus)](https://www.npmjs.com/package/@ddd-master/query-bus) | ||
|
||
The `@ddd-master/query-bus` package is a TypeScript library that provides an implementation of the query bus pattern for Domain-Driven Design (DDD) applications. It enables you to execute queries and retrieve query results in a centralized and efficient manner. | ||
|
||
## Installation | ||
|
||
You can install the `@ddd-master/query-bus` package using npm: | ||
|
||
```bash | ||
npm install @ddd-master/query-bus | ||
``` | ||
|
||
## Usage | ||
|
||
To use the `@ddd-master/query-bus` package, you need to define your queries and query handlers. | ||
|
||
### Defining Queries | ||
|
||
Queries represent requests for information or data retrieval. You can define a query by implementing the `IQuery` interface. For example: | ||
|
||
```typescript | ||
export interface IFindUserQuery extends IQuery { | ||
email: string; | ||
} | ||
|
||
export class FindUserQuery implements IFindUserQuery { | ||
public readonly email: string; | ||
|
||
public constructor({ email }: IFindUserQuery) { | ||
this.email = email; | ||
} | ||
} | ||
``` | ||
|
||
### Defining Query Handlers | ||
|
||
Query handlers are responsible for executing queries and returning the query results. You can implement the `IQueryHandler` interface to define your query handlers. For example: | ||
|
||
```typescript | ||
export class FindUserQueryHandler implements IQueryHandler<IFindUserQuery, FindUserQueryHandlerResult> { | ||
private readonly userRepository: UserRepository; | ||
public constructor({ userRepository }: Dependencies) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public handle(query: IQuery): FindUserQueryHandlerResult { | ||
const { email } = query as IFindUserQuery; | ||
|
||
const user = this.userRepository.getByEmail({ email }); | ||
|
||
if (!user) { | ||
throw new Error('User not found'); | ||
} | ||
|
||
return { user }; | ||
} | ||
} | ||
``` | ||
|
||
### Registering Query Handlers | ||
|
||
Once you have your query handlers, you need to register them with the query bus. For example: | ||
|
||
```typescript | ||
const queryBus = new QueryBus(); | ||
const findUserQueryHandler = new FindUserQueryHandler(); | ||
queryBus.register(FindUserQuery, findUserQueryHandler); | ||
``` | ||
|
||
### Executing Queries | ||
|
||
To execute a query, you can use the `execute` method of the query bus. For example: | ||
|
||
```typescript | ||
const result = queryBus.execute<IFindUserQuery, FindUserQueryHandlerResult>( | ||
new FindUserQuery({ email: '[email protected]' }), | ||
); | ||
|
||
const { user } = result; | ||
console.log(user); | ||
``` | ||
|
||
The `execute` method returns the query result. You can access the result and process it accordingly. | ||
|
||
## Contributing | ||
|
||
Contributions to the `@ddd-master/query-bus` package are welcome! Feel free to open issues or submit pull requests on the [GitHub repository](https://github.com/BartekCK/ddd-master/tree/master/libs/query-bus). Please follow the [code of conduct](https://github.com/BartekCK/ddd-master/blob/master/CODE_OF_CONDUCT.md) when contributing. | ||
|
||
## License | ||
|
||
The `@ddd-master/query-bus` package is open-source software licensed under the [MIT License](https://github.com/BartekCK/ddd-master/blob/master/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
"url": "https://github.com/BartekCK/ddd-master.git", | ||
"directory": "libs/query-bus" | ||
}, | ||
"license": "MIT", | ||
"description": "Query bus implementation", | ||
"author": { | ||
"email": "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"url": "https://github.com/BartekCK/ddd-master.git", | ||
"directory": "libs/result" | ||
}, | ||
"license": "MIT", | ||
"description": "DDD Result pattern implementation", | ||
"author": { | ||
"email": "[email protected]" | ||
|