title: Typescript API Repositories and Unit of Work implementation (Domain-Driven Design)
body: <p>I'm trying to implement <strong>Repositories</strong> and the <strong>Unit of Work</strong> pattern using <strong>Sequelize</strong> and Typescript in the existing project, following <strong>Domain-Driven Design</strong>. However, I encountered some difficulties due to a lack of relevant examples on the internet.</p>
  <p>Let's consider the following code example.</p>
  <pre><code>interface IPostRepo {
      save(post: Post): Promise&lt;void&gt;;
  }
  
  interface ICommentRepo {
      save(comment: Comment): Promise&lt;void&gt;;
      saveBulk(comments: Comment[]): Promise&lt;void&gt;;
  }
  
  class SequelizePostRepo implements IPostRepo {
      private models: any;
      private commentRepo: ICommentRepo;
  
      constructor(models: any, commentRepo: ICommentRepo) {
          this.models = models;
          this.commentRepo = commentRepo;
      }
  
      public async save(post: Post): Promise&lt;void&gt; {
          const PostModel = this.models.Post;
          const rawSequelizePost = await PostMap.toPersistence(post);
  
          await PostModel.create(rawSequelizePost);
          await this.commentRepo.saveBulk(comments);
      }
  }
  
  class SequelizeCommentRepo implements ICommentRepo {
      private models: any;
  
      constructor(models: any) {
          this.models = models;
      }
  
      async save(comment: Comment): Promise&lt;void&gt; {
          const CommentModel = this.models.Comment;
          const rawSequelizeComment = CommentMap.toPersistence(comment);
  
          await CommentModel.create(rawSequelizeComment);
      }
  
      async saveBulk(comments: Comment[]): Promise&lt;void&gt; {
          for (let comment of comments) {
              await this.save(comment);
          }
      }
  }
  
  </code></pre>
  <p><strong>NOTE:</strong> The code is not complete and simplified for the problem demonstration purpose.</p>
  <p><code>IPostRepo</code> and <code>ICommentRepo</code> are contracts for the specific implementation such as <code>SequelizePostRepo</code> and <code>SequelizeCommentRepo</code>.
  Both of them implement a basic save method that operates on <strong>Domain Entities</strong>: <code>Post</code> and <code>Comment</code>. The <code>Post</code> is the <strong>Aggregate Root</strong> (models object contains the essential Sequelize models definition).</p>
  <pre><code>class CreatePostWithComments implements UseCase {
      private postRepo: IPostRepo;
  
      constructor (postRepo: IPostRepo) {
          this.postRepo = postRepo;
      }
  
      public async execute (req: ReplyToPostDTO) {
  
          /**
           *
           * More logic related to post and comments creation goes here
           *
           */
  
          post.addComment(comment1);
          post.addComment(comment2);
          post.addComment(comment3);
  
          await this.postRepo.save(post);
      }
  }
  </code></pre>
  <p>Having <code>CreatePostWithComments</code> <strong>Use Case</strong> in the end I want to save <code>Post</code> Aggregate with attached comments. I'd like to do it in a single MySQL transaction using the <strong>Unit of Work</strong> pattern correctly.</p>
  <p>I would appreciate any help that indicates correct implementation using simple examples.</p>
  <p>I found the following package: <a href="https://github.com/pilagod/uow-sequelize" rel="nofollow noreferrer">https://github.com/pilagod/uow-sequelize</a>, however by virtue of documentation deficiency I don't know how to use it rightly.</p>