Откат транзакции LB4 с помощью .createAll () - PullRequest
0 голосов
/ 16 апреля 2020

Я пытаюсь создать массовые записи в базе данных, которая включает в себя пару репозиториев.

  async calculateScore(@requestBody() surveyRequestBody: SurveyBody) {
    const { answers, surveyId, userId } = surveyRequestBody;
    // TODO: Add transaction and roll back changes if the server result fails
    const clusterDetails = await this.getClusterInformation();
    const surveyAnswerTransaction: Transaction = await this.surveyAnswerRepository.beginTransaction(
      IsolationLevel.READ_COMMITTED,
    );
    const userClusterTransaction: Transaction = await this.userClusterRepository.beginTransaction(
      IsolationLevel.READ_COMMITTED,
    );
    const formattedSurveyValues = this.formatSurveyAnswers(answers, surveyId);
    console.log(formattedSurveyValues, 'Formatted survey answers are ');
    // TODO: Update the user role from the survey
    const calculatedPoints = await this.getCalculatedPoints(surveyRequestBody, clusterDetails);
    await this.surveyAnswerRepository.createAll(formattedSurveyValues, { transaction: surveyAnswerTransaction });
    const clusterScoreCollection = calculatedPoints.map(({ clusterId, percentage }) => {
      return { clusterId, clusterScore: percentage, userId, surveyId };
    });    
    await this.userClusterRepository.createAll(clusterScoreCollection, { transaction : userClusterTransaction });

    await surveyAnswerTransaction.commit();
    await userClusterTransaction.commit();
    return calculatedPoints;
  }

Есть ли какой-нибудь способ, которым я мог бы обрабатывать транзакции с помощью миксинов или обрабатывать эти ошибки. Какие-нибудь альтернативные решения для этого?

...