I am executing a query to Postgre DB to fetch data older than a specific date.
Here's my function
async filesListToDelete(): Promise<any> {
return await this.fileRepository.find({
where: { last_modified: { $lt: '2018-11-15 10:41:30.746877' } },
});
}
Here's how I defined my File entity:
export class File {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: false })
idFonc: number;
@Column({ nullable: false })
version: number;
@Column('varchar', { length: 100, nullable: false })
filename: string;
@Column({ nullable: true })
last_modified: Date;
@Column({ nullable: false })
device: boolean;
@ManyToOne(type => Type, { nullable: false })
@JoinColumn({ referencedColumnName: 'id' })
type: Type;
@OneToMany(type => FileDevice, filedevice => filedevice.file)
fileDevice: FileDevice[];
}
I get this error
QueryFailedError: invalid input syntax for type timestamp: "{"$lt":"2018-11-15 10:41:30.746877"}"
You can use MoreThan, the doc
async filesListToDelete(): Promise<any> {
return await this.fileRepository.find({
where: {
last_modified: MoreThan('2018-11-15 10:41:30.746877') },
});}
Also you can do this using createQueryBuilder
as below:
public async filesListToDelete(): Promise<any> {
let record = await this.fileRepository.createQueryBuilder('file')
.where('file.last_modified > :start_at', { start_at: '2018-11-15 10:41:30.746877' })
.getMany();
return record
}
Either of these will fetch OLDER
data
with built-in TypeORM
operator (docs)
async filesListToDelete(): Promise<any> {
return await this.fileRepository.find({
where: { last_modified: LessThan('2018-11-15 10:41:30.746877') },
});
}
with PostgreSQL
operator (docs)
public async filesListToDelete(): Promise<any> {
let record = await this.fileRepository.createQueryBuilder('file')
.where('file.last_modified < :start_at', { start_at: '2018-11-15 10:41:30.746877' })
.getMany();
return record
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With