This is my entity.
@Entity({ name: 'documents' })
export class UserDocument extends TenantBaseModel {
@Column({ nullable: false })
user_id?: number;
@Column({ nullable: true })
name: string;
@Column({ nullable: true })
type?: UserDocumentType;
@Column({ nullable: true })
s3_link?: string;
@Column({ nullable: true })
attachment_extension?: string;
@Column({ nullable: true })
attachment_size?: number;
constructor() {
super();
this.name = '';
this.s3_link = '';
this.attachment_extension = '';
this.attachment_size = 0;
}
}
And this is my return json which is getting generated after calling this particular.
{
"statusCode":200,
"requestId":"35a26c53-9fdc-4f53-b49a-161a39b144d4",
"data":[
{
"id":3,
"tenant_id":1,
"name":"PAN",
"s3_link":"",
"attachment_extension":"jpg",
"attachment_size":1000,
"created_at":"2020-09-30T06:21:51.596Z",
"user_id":21,
"type":"PERSONAL DOCUMENTS"
},
{
"id":4,
"tenant_id":1,
"name":"PAN",
"s3_link":"",
"attachment_extension":"jpg",
"attachment_size":1000,
"created_at":"2020-09-30T06:21:51.614Z",
"user_id":21,
"type":"PERSONAL DOCUMENTS"
},
{
"id":244,
"tenant_id":0,
"name":"VISITING CARD",
"s3_link":"",
"attachment_extension":"jpg",
"attachment_size":25986,
"created_at":"2021-11-18T05:03:29.079Z",
"user_id":21,
"type":"EMPLOYEE DOCUMENTS"
},
{
"id":245,
"tenant_id":0,
"name":"VISITING CARD",
"s3_link":"",
"attachment_extension":"jpg",
"attachment_size":25986,
"created_at":"2021-11-18T05:05:12.098Z",
"user_id":21,
"type":"PERSONAL DOCUMENTS"
},
{
"id":246,
"tenant_id":0,
"name":"VISITING CARD",
"s3_link":"",
"attachment_extension":"jpg",
"attachment_size":25986,
"created_at":"2021-11-18T05:05:52.682Z",
"user_id":21,
"type":"EDUCATIONAL DOCUMENTS"
}
]
}
What is the query which i have to build to get the latest entry in a particular type using type orm for example on the above json we could see that the type PERSONAL DOCUMENT has three entries but the time stamp for all three are different i just need the lates PERSONAL DOCUMENT with latest time stamp same goes for other entries as well such as EDUCATIONAL DOCUMENTS,EMPLOYEE DOCUMENT accordingly. My question is is that possible to build a query in typeorm only if so how?