postgres init

This commit is contained in:
Ben Wilson
2023-12-04 00:34:33 -05:00
parent f7303d73af
commit 39c4c033e2
11 changed files with 305 additions and 85 deletions
+4 -9
View File
@@ -1,14 +1,9 @@
import { Column, Entity, Index, PrimaryColumn } from 'typeorm';
import { DateTimeTransformer } from '../utils/DateTimeTransformer';
import { TrackedEntity } from '../utils/TrackedEntity.entity';
//https://www.sqlite.org/withoutrowid.html
//The WITHOUT ROWID optimization is likely to be helpful for tables that have non-integer
// or composite (multi-column) PRIMARY KEYs and that do not store large strings or BLOBs.
//WITHOUT ROWID tables work best when individual rows are not too large.
@Entity({ withoutRowid: true })
@Entity()
@Index(['address', 'clientName', 'sessionId'], { unique: true })
export class ClientEntity extends TrackedEntity {
@@ -28,13 +23,13 @@ export class ClientEntity extends TrackedEntity {
@Column({ type: 'datetime', transformer: new DateTimeTransformer() })
@Column({ type: 'timestamp' })
startTime: Date;
@Column({ type: 'real', default: 0 })
@Column({ type: 'decimal', default: 0 })
bestDifficulty: number
@Column({ default: 0 })
@Column({ default: 0, type: 'decimal' })
hashRate: number;
}
+5 -30
View File
@@ -1,8 +1,6 @@
import { Injectable } from '@nestjs/common';
import { Interval } from '@nestjs/schedule';
import { InjectRepository } from '@nestjs/typeorm';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { ObjectLiteral, Repository } from 'typeorm';
import { Repository } from 'typeorm';
import { ClientEntity } from './client.entity';
@@ -12,8 +10,6 @@ import { ClientEntity } from './client.entity';
export class ClientService {
public insertQueue: { result: BehaviorSubject<ObjectLiteral | null>, partialClient: Partial<ClientEntity> }[] = [];
constructor(
@InjectRepository(ClientEntity)
@@ -22,26 +18,13 @@ export class ClientService {
}
@Interval(1000 * 5)
public async insertClients() {
const queueCopy = [...this.insertQueue];
this.insertQueue = [];
const results = await this.clientRepository.insert(queueCopy.map(c => c.partialClient));
queueCopy.forEach((c, index) => {
c.result.next(results.generatedMaps[index]);
});
}
public async killDeadClients() {
var fiveMinutes = new Date(new Date().getTime() - (5 * 60 * 1000)).toISOString();
return await this.clientRepository
.createQueryBuilder()
.update(ClientEntity)
.set({ deletedAt: () => "DATETIME('now')" })
.where("deletedAt IS NULL AND updatedAt < DATETIME(:fiveMinutes)", { fiveMinutes })
.set({ deletedAt: () => "NOW()" })
.where("deletedAt IS NULL AND updatedAt < NOW() + interval '5 minutes' ")
.execute();
}
@@ -55,19 +38,11 @@ export class ClientService {
public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> {
const result = new BehaviorSubject(null);
this.insertQueue.push({ result, partialClient });
// const insertResult = await this.clientRepository.insert(partialClient);
const generatedMap = await firstValueFrom(result);
const insertResult = await this.clientRepository.insert(partialClient);
const client = {
...partialClient,
...generatedMap
...insertResult.generatedMaps[0]
};
return client as ClientEntity;