typeorm#ViewEntity TypeScript Examples

The following examples show how to use typeorm#ViewEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: latest-data.entity.ts    From aqualink-app with MIT License 5 votes vote down vote up
@ViewEntity({
  expression: (connection: Connection) => {
    return (
      connection
        .createQueryBuilder()
        .select(
          'DISTINCT ON (metric, type, site_id, survey_point_id) time_series.id',
        )
        .addSelect('metric')
        .addSelect('timestamp')
        .addSelect('value')
        .addSelect('type', 'source')
        .addSelect('site_id')
        .addSelect('survey_point_id')
        .from(TimeSeries, 'time_series')
        .innerJoin('sources', 'sources', 'sources.id = time_series.source_id')
        // Limit data to the past week. Bonus, it makes view refreshes a lot faster.
        .where("timestamp >= current_date - INTERVAL '7 days'")
        // Look a bit further in the past for sonde data
        .orWhere(
          "type IN ('sonde') AND (timestamp >= current_date - INTERVAL '90 days')",
        )
        .orderBy('metric, type, site_id, survey_point_id, timestamp', 'DESC')
    );
  },
  materialized: true,
})
export class LatestData {
  @ApiProperty({ example: 1 })
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: false })
  timestamp: Date;

  @ApiProperty({ example: 11.05 })
  @Column({ type: 'float', nullable: false })
  value: number;

  @ManyToOne(() => Site, { onDelete: 'CASCADE', nullable: false })
  site: Site;

  @ApiProperty({ example: 15 })
  @RelationId((latestData: LatestData) => latestData.site)
  siteId: number;

  @ManyToOne(() => SiteSurveyPoint, { onDelete: 'CASCADE', nullable: true })
  surveyPoint: SiteSurveyPoint | null;

  @Column({ type: 'enum', enum: SourceType })
  source: SourceType;

  @Column({ type: 'enum', enum: Metric })
  metric: Metric;
}