mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
trying bulk insert?
This commit is contained in:
@@ -1,15 +1,18 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { setTimeout } from 'timers/promises';
|
||||||
import { IsNull, LessThan, Repository } from 'typeorm';
|
import { IsNull, LessThan, Repository } from 'typeorm';
|
||||||
|
|
||||||
import { ClientEntity } from './client.entity';
|
import { ClientEntity } from './client.entity';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ClientService {
|
export class ClientService {
|
||||||
|
|
||||||
|
private bulkInsertClients: ClientEntity[] = [];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(ClientEntity)
|
@InjectRepository(ClientEntity)
|
||||||
private clientRepository: Repository<ClientEntity>
|
private clientRepository: Repository<ClientEntity>
|
||||||
@@ -17,6 +20,18 @@ export class ClientService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Cron(CronExpression.EVERY_SECOND)
|
||||||
|
private async bulkInsert() {
|
||||||
|
|
||||||
|
if (this.bulkInsertClients.length < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const clientsToInsert = [...this.bulkInsertClients];;
|
||||||
|
this.bulkInsertClients = [];
|
||||||
|
await this.clientRepository.insert(clientsToInsert);
|
||||||
|
}
|
||||||
|
|
||||||
public async killDeadClients() {
|
public async killDeadClients() {
|
||||||
var tenMinutes = new Date(new Date().getTime() - (60 * 60 * 1000));
|
var tenMinutes = new Date(new Date().getTime() - (60 * 60 * 1000));
|
||||||
|
|
||||||
@@ -36,15 +51,13 @@ export class ClientService {
|
|||||||
// return await this.clientRepository.save(client);
|
// return await this.clientRepository.save(client);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> {
|
public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> {
|
||||||
const insertResult = await this.clientRepository.insert(partialClient);
|
|
||||||
|
|
||||||
const client = {
|
const client = this.clientRepository.create(partialClient);
|
||||||
...partialClient,
|
this.bulkInsertClients.push(client);
|
||||||
...insertResult.generatedMaps[0]
|
await setTimeout(2000);
|
||||||
};
|
return client;
|
||||||
|
|
||||||
return client as ClientEntity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(sessionId: string) {
|
public async delete(sessionId: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user