sql - ใช้ Type Orm เพื่อดึงข้อมูลล่าสุดในแต่ละหมวดหมู่
นี่คือหน่วยงานของฉัน
@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;
}
}
และนี่คือ JSON ที่ฉันส่งกลับซึ่งถูกสร้างขึ้นหลังจากเรียกใช้งานรายการนี้โดยเฉพาะ
{
"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"
}
]
}
คำถามคือฉันต้องสร้างการสืบค้นอย่างไรเพื่อให้ได้รายการล่าสุดในประเภทหนึ่งโดยใช้ TypeORM ตัวอย่างเช่น จาก JSON ข้างต้น เราจะเห็นว่าประเภท PERSONAL DOCUMENT มีสามรายการ แต่เวลาสำหรับทั้งสามต่างกัน ฉันต้องการเพียง PERSONAL DOCUMENT ล่าสุดที่มีเวลาล่าสุด และเช่นเดียวกันกับรายการอื่น ๆ เช่น EDUCATIONAL DOCUMENTS, EMPLOYEE DOCUMENT ตามลำดับ คำถามของฉันคือสามารถสร้างการสืบค้นใน TypeORM ได้หรือไม่ ถ้าได้ ทำอย่างไร?