Skip to content

Instantly share code, notes, and snippets.

@AntsiferovMaxim
Created May 28, 2020 21:17
Show Gist options
  • Save AntsiferovMaxim/8db4e4b61d49ebb45dbb1ac1f73ed0d5 to your computer and use it in GitHub Desktop.
Save AntsiferovMaxim/8db4e4b61d49ebb45dbb1ac1f73ed0d5 to your computer and use it in GitHub Desktop.
@Entity('checks')
export class ChecksEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToOne(() => SessionsEntity, session => session.check, {onDelete: 'CASCADE'})
@JoinColumn()
session_id: string;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
}
@Entity('sessions')
export class SessionsEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToOne(
() => ChecksEntity,
check => check.session_id,
{ nullable: true, onDelete: 'CASCADE' },
)
@JoinColumn()
check: ChecksEntity;
@Column('timestamptz', { nullable: true })
expireAt: Date;
@CreateDateColumn()
created_at: Date;
@UpdateDateColumn()
updated_at: Date;
}
select date,
(select count(checks.id)
from checks
left join sessions s on checks.id = s.check_id
where s.workspace_id = '${payload.workspace_id}'
and checks.created_at BETWEEN date AND date + INTERVAL '${interval}') count
FROM generate_series(CURRENT_DATE - INTERVAL '${last}', CURRENT_DATE, INTERVAL '${interval}') date
ORDER BY date;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment