- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以这里有一个官方的例子
https://github.com/kamilmysliwiec/nest-cqrs-example
我尝试为三个简单的功能创建自己的功能:
create.user.handler.ts
):
@CommandHandler(CreateUserCommand)
export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
constructor(
private readonly usersRepository: UsersRepository,
private readonly eventPublisher: EventPublisher,
) {}
public async execute(command: CreateUserCommand): Promise<void> {
const createdUser: User = await this.usersRepository.createUser(
command.username,
command.password,
);
// const userAggregate: UserAggregate = this.eventPublisher.mergeObjectContext(
// createdUser,
// );
// userAggregate.doSomething(createdUser);
// userAggregate.commit();
}
}
它所做的只是将一个新的用户实体持久化到数据库中。但是我现在没有得到的是如何处理 User 聚合。合并对象上下文时,我无法传入创建的用户实体。而且我不知道聚合应该处理哪个逻辑。所以基于这个用户聚合(
user.model.ts
):
export class User extends AggregateRoot {
constructor(private readonly id: string) {
super();
}
public doSomething(user: UserEntity): void {
// do something here?
this.apply(new UserCreatedEvent(user));
}
}
我知道我可以引发事件,即创建了一个新用户并将其推送到历史记录中。但这是它唯一负责的事情吗?
Argument of type 'User' is not assignable to parameter of type 'AggregateRoot'. Type 'User' is missing the following properties fromtype 'AggregateRoot': autoCommit, publish, commit, uncommit, and 7more.ts(2345)
BaseEntity
并且聚合扩展
AggregateRoot
.不幸的是,官方示例没有展示如何处理聚合和数据库实体。
最佳答案
@cojack 的回答假设您的 TypeORM 实体定义和 CQRS AggregateRoot 是由同一个类定义的。许多 NestJS + CQRS 项目都是这样实现的(虽然这不是最佳实践,并且违反了单一职责原则。并且您的域类不应依赖于您的数据库)。
正如您在评论中所述,如果您的 TypeORM 定义必须从 BaseEntity
扩展,这是有问题的。 ,因为您也不能从 AggregateRoot
扩展.
但没有要求这样做。在您的命令处理程序中,您可以调用 UsersRepository
例如验证具有该名称的用户是否已存在,然后创建该用户。
然后在您的 mergeObjectContext
您可以实例化在单独的类中定义的聚合根实体,并从您刚刚创建的 User 实体中传递适当的信息。
这方面的一个例子可以在 nestjs-rest-cqrs-example 中找到。项目:
@CommandHandler(CreateAccountCommand)
export class CreateAccountCommandHandler implements ICommandHandler<CreateAccountCommand> {
constructor(
@InjectRepository(AccountEntity) private readonly repository: AccountRepository,
private readonly publisher: EventPublisher,
) {}
async execute(command: CreateAccountCommand): Promise<void> {
// Validation of the command (check if account with this email already exists).
await this.repository.findOne({ where: [{ email: command.email }]}).then((item) => {
if (item) throw new HttpException('Conflict', HttpStatus.CONFLICT);
});
const { name, email, password } = command;
// Create the account via the repository. The TypeORM entity is returned.
const result = await this.repository.save(new CreateAccountMapper(name, email, bcrypt.hashSync(password)));
const account: Account = this.publisher.mergeObjectContext(
// Create the AggregateRoot entity to be passed to the context.
new Account(result.id, name, email, result.password, result.active),
);
account.commit();
}
}
这个项目在聚合根中的事件方面做得并不多,但我希望你明白了。
关于typescript - NestJs CQRS - 与现有的 TypeORM 实体聚合设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703497/
我知道 typeorm 可以创建整个 db 模式,甚至可以将数据库模式与实体定义同步(请参阅 synchronize 中的 Connection Options ) 但在生产中,我想要求 typeor
我有一个 Role Entity 和一个 Route Entity,它们是树形结构,它们是 ManyToMany 关系。 现在我想通过 RoleRepository.find({relations:
我已经设置了 logging: true当createConnection()来自 TypeORM ,在大多数情况下都可以正常工作。 但是有一种情况,我的数据字段之一来自特定的 query/mutat
在 Rails 上,每个测试用例都会创建一个 ActiveRecord 事务,它允许测试所有内容,然后将数据库恢复到原始状态,而不必删除所有表或任何可能影响播种机等的内容。 可以在 Typeorm 上
Note 与 Subject 具有多对多关系 查询它的最佳方式是什么?我喜欢编写以下内容来获取给定注释上的所有主题: const subjectRepo = connection.getRepos
我使用 UUID 作为我的实体的主键,它工作得很好。但我想删除那些破折号。 现在 ID 被保存为 8e5365f4-3d42-4274-bafc-93b97bd6e3f2 36 个字符 我想要的是 8
我一整天都在尝试将实体保存到 MySQL 数据库中,遇到了巨大的困难。我正在使用 NestJS 和 TypeORM。 teacher.entity.ts import { BeforeInsert,
我也想在前端使用我用 TypeORM 创建的实体。有没有一种干净的方法可以做到这一点?或者我应该继续这样做。 此时,我的文件结构如下所示(前端的 Angular 8): /server /enti
我想得到一个 basic setup使用 TypeORM 工作,并在设置后收到此错误。 这是一个 REPL(只需执行 yarn install && yarn db:dev 后跟 yarn db:mi
我正在尝试通过 Nest/TypeORM 构建 SAAS 产品,我需要按子域配置/更改数据库连接。 customer1.domain.com => connect to customer1 datab
如何将 TypeORM 与 Better-sqlite3 一起使用? 上官方documentation ,有一个section形式更好-sqlite3。 我已经通过 typeorm@latest 安装
操作系统:macOS Sierra v 10.12.6 我正在尝试使用 typeorm 在 Typescript 中构建一个应用程序,这是我第一次使用其中任何一个。 我使用了以下两种方法来安装 typ
有没有一种简单的方法可以使用 DataSource 在 typeORM v.0.3.6 中播种数据? typeorm-seeding 似乎使用已弃用的 Connection。 最佳答案 找到这个包裹
我正在做一门类(class),我们使用 express-node postgres 和 react(使用 typescript)和 typeORM。到目前为止一切都运行良好,但我面临 typeORM
尽管进行了大量研究和反复试验,但不幸的是,我还没有针对以下用例的完美解决方案:一个用户有多个帖子,这被正确地实现为一对多关系。我想为用户加载他的所有帖子,但不是帖子的所有属性,只有某些属性。 方法 1
我用 NestJS 和 ORM TypeORM 做一个 API 我有实体: 用户 关系类型 UserRelationshipType(带有用户和关系类型 ForeignKey) 和关系(与 userR
我有两个实体 Tag 和 Photo: // tag.entity.ts import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm
我有以下要保存的实体: @Entity('approvals') export class Approval { @PrimaryGeneratedColumn() id: string;
我有命令: .leftJoinAndSelect("permission.user", "user") .where("permissi
我是使用 typeorm 的新手,这是我第二次与 typeorm 混淆,我有以下查询: SELECT t1.a,t1.b,t2.a (SELECT TOP 1 t1.a FROM table1
我是一名优秀的程序员,十分优秀!