mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
wrap save in a transaction
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { DataSource, EntityManager, Repository } from 'typeorm';
|
||||||
|
|
||||||
import { ClientStatisticsEntity } from './client-statistics.entity';
|
import { ClientStatisticsEntity } from './client-statistics.entity';
|
||||||
|
|
||||||
@@ -10,7 +10,8 @@ export class ClientStatisticsService {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
|
|
||||||
|
@InjectDataSource()
|
||||||
|
private dataSource: DataSource,
|
||||||
@InjectRepository(ClientStatisticsEntity)
|
@InjectRepository(ClientStatisticsEntity)
|
||||||
private clientStatisticsRepository: Repository<ClientStatisticsEntity>,
|
private clientStatisticsRepository: Repository<ClientStatisticsEntity>,
|
||||||
) {
|
) {
|
||||||
@@ -18,18 +19,30 @@ export class ClientStatisticsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async save(clientStatistic: Partial<ClientStatisticsEntity>) {
|
public async save(clientStatistic: Partial<ClientStatisticsEntity>) {
|
||||||
const res1 = await this.clientStatisticsRepository.createQueryBuilder()
|
// Use transaction to ensure atomicity
|
||||||
.update(ClientStatisticsEntity)
|
await this.dataSource.transaction(async (entityManager: EntityManager) => {
|
||||||
.set({
|
// Attempt to update the existing record
|
||||||
shares: () => `"shares" + ${clientStatistic.shares}`, // Use the actual value of shares here
|
const updateResult = await entityManager
|
||||||
acceptedCount: () => `"acceptedCount" + 1`
|
.createQueryBuilder()
|
||||||
})
|
.update(ClientStatisticsEntity)
|
||||||
.where('address = :address AND clientName = :clientName AND sessionId = :sessionId AND time = :time', { address: clientStatistic.address, clientName: clientStatistic.clientName, sessionId: clientStatistic.sessionId, time: clientStatistic.time })
|
.set({
|
||||||
.execute();
|
shares: () => `"shares" + ${clientStatistic.shares}`,
|
||||||
|
acceptedCount: () => `"acceptedCount" + 1`
|
||||||
|
})
|
||||||
|
.where('address = :address AND clientName = :clientName AND sessionId = :sessionId AND time = :time', {
|
||||||
|
address: clientStatistic.address,
|
||||||
|
clientName: clientStatistic.clientName,
|
||||||
|
sessionId: clientStatistic.sessionId,
|
||||||
|
time: clientStatistic.time
|
||||||
|
})
|
||||||
|
.execute();
|
||||||
|
|
||||||
if (res1.affected == 0) {
|
// Check if the update affected any rows
|
||||||
await this.clientStatisticsRepository.insert(clientStatistic);
|
if (updateResult.affected === 0) {
|
||||||
}
|
// If no rows were updated, insert a new record
|
||||||
|
await entityManager.insert(ClientStatisticsEntity, clientStatistic);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteOldStatistics() {
|
public async deleteOldStatistics() {
|
||||||
|
|||||||
Reference in New Issue
Block a user