Skip to content

Implementation of Single Transaction per Request in NestJS, based on @vendure-ecommerce project.

License

Notifications You must be signed in to change notification settings

kiaksarg/nestjs-transaction-per-request

Repository files navigation

transaction-per-request

Nestjs Transaction per request

transaction-per-request

async create(
    ctx: RequestContext,
    post: PostCreateInput,
    groupName = 'Default Group',
  ): Promise<PostEntity> {
    //Creating a Group
    const defaultGroup = await this.connection
      .getRepository(ctx, GroupEntity)
      .save(new GroupEntity({ name: groupName, description: 'A default group for given post.' }));

    const postEntity = new PostEntity({ ...(post as any), groupId: defaultGroup.id });
    //Making a required field null, to throw an error
    postEntity.testRequiredField = null;

    //Error
    const savedPost = await this.connection.getRepository(ctx, PostEntity).save(postEntity);

    return savedPost;
  }
  @Transaction()
  @Mutation(() => Post)
  async createPostTransactional(
    @Ctx() ctx: RequestContext,
    @Args('postCreateInput') postCreateInput: PostCreateInput,
  ) {
    return await this.postService.create(ctx, postCreateInput, 'Transactional Group Name');
  }

About

Implementation of Single Transaction per Request in NestJS, based on @vendure-ecommerce project.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages