Agregate root with entities #121
-
Hello, Thank you for this great repo. Is the best. Thanks you for you help. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Not sure why you need to put Vehicles into an Employee, for me it looks like two entirely different domains. I would separate them and just put As for saving it, you just start a transaction and save everything, something like this: await this.repo.transaction(async () => {
await this.repo.insert(employee);
await this.repo.insert(vehicles);
}) If you still want to have await this.repo.saveEmployee(employee); And repository will save things into separate tables: async saveEmployee(employee: Employee): Promise<void> {
await this.transaction(() => {
await this.writeQuery(sql`INSERT INTO employee (name, ... ) VALUES (${employee.name}, ... )`);
await this.writeQuery(sql`INSERT INTO vehicles VALUES (employee_id, name, ... ) (${employee.id}, ${employee.vehicle.name}, ... )`); // <- you may need use map here to insert all vehicles
})
} |
Beta Was this translation helpful? Give feedback.
-
Maybe in your case it makes sense to have it like that, but just to clarify things:
You can tweak your repository and a mapper a little so the mapper produces an array of entities it needs to save. For example an employee mapper can return something like this: If you want to make things that generic you might consider using an ORM that handles this for you, most ORMs let you insert multiple entities like that out of the box. Using an ORM just to automate migrations and bulk inserts is totally fine IMO, makes life easier. Also, if you have a unique repository for the entire app, how do you handle updates? if, for example, you only need to update an
The third one is not a very good approach IMO. You should be able to save a bunch of entities in a single transaction. Just tweak your repository/mapper a little to adapt to your needs. |
Beta Was this translation helpful? Give feedback.
Maybe in your case it makes sense to have it like that, but just to clarify things:
the fact that vehicles cannot exist without an employee not necessary means it has to be part of the same aggregate root. Your employees can also have a
Manager
, aWallet
, aDepartment
they belong to, and a bunch of other relations that cannot exist one without the other. But it doesn't mean that you have to put all of the above into the same aggregate root.