In my PostgreSQL database I have a ema
schema and 10 tables for different timeframes for which EMAs were calculated: ema.ema_1s
, ema.ema_1min
etc.
In TypeORM each entity is created with specified name and used as Repository<Entity>
inside a service.
I’ve created Ema
entity:
import { Column, Entity, PrimaryColumn } from 'typeorm';
@Entity({ schema: 'ema' })
export class Ema {
@PrimaryColumn({ type: 'text' })
symbol: string;
@PrimaryColumn({ type: 'text' })
exchange: string;
@Column({ type: 'double precision' })
ema: number;
@PrimaryColumn({ type: 'timestamp' })
timestamp: Date;
}
Is there a way to use this entity for each table inside ema
schema or I should create separate entities for each table like:
@Entity('ema_1s', { schema: 'ema' })
export class Ema1s extends Ema {}
@Entity('ema_1min', { schema: 'ema' })
export class Ema1min extends Ema {}
...
And use them in code like this:
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Ema1min, Ema1s } from '@/common/entities';
@Injectable()
export class PostgresService {
constructor(
@InjectRepository(Ema1s)
private readonly ema1sRepository: Repository<Ema1s>,
@InjectRepository(Ema1min)
private readonly ema1minRepository: Repository<Ema1min>,
...
) {}
}
This approach seems to me not really suitable because I have 10 different tables for ema
and 3 other schemas, so there will be 40 different injections so I can use all the tables.