parameterized typeorm

This commit is contained in:
Ben Wilson
2023-12-14 23:54:45 -05:00
parent 42fc863c7c
commit df245f396e
@@ -19,33 +19,30 @@ export class ClientStatisticsService {
} }
public async save(clientStatistic: Partial<ClientStatisticsEntity>) { public async save(clientStatistic: Partial<ClientStatisticsEntity>) {
await this.dataSource.transaction(async (entityManager) => {
const query = `
UPDATE client_statistics_entity
SET shares = shares + $1, "acceptedCount" = "acceptedCount" + 1
WHERE address = $2 AND "clientName" = $3 AND "sessionId" = $4 AND time = $5
RETURNING *;`;
const parameters = [ // Attempt to update the existing record
clientStatistic.shares || 0, // Ensure a default value for shares const updateResult = await this.clientStatisticsRepository
clientStatistic.address, .createQueryBuilder()
clientStatistic.clientName, .update(ClientStatisticsEntity)
clientStatistic.sessionId, .set({
clientStatistic.time shares: () => `"shares" + :sharesIncrement`,
]; 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,
sharesIncrement: clientStatistic.shares
})
.execute();
try { // Check if the update affected any rows
const result = await entityManager.query(query, parameters); if (updateResult.affected === 0) {
// If no rows were updated, insert a new record
await this.clientStatisticsRepository.insert(clientStatistic);
}
if (result.length === 0) {
await entityManager.insert(ClientStatisticsEntity, clientStatistic);
}
} catch (error) {
// Handle errors
console.error('Error during transaction:', error);
throw error;
}
});
} }
public async deleteOldStatistics() { public async deleteOldStatistics() {