I have a composite primary key of uuid and version. What would be the best practice of querying only the newest version of all entities matching a certain where clause? I probably need some kind of distinct clause ... is this possible using active record pattern?
// returns all articles ordered by version, but I need only the newest version of each Article
Article.find({
where: {
company: '35fccfba-b4f8-4393-b211-c258d0a79076',
status: 'approved',
},
order: {
version: 'DESC',
});
Table looks like:
| uuid | version | status | company |
|---|---|---|---|
| 50ea6422-f56e-45e2-... | 1 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |
| 8e6d646b-4772-47c6-... | 1 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |
| 93b06cef-0d94-493b-... | 1 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |
| 0094d9da-1768-4b8b-... | 1 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |
| 0094d9da-1768-4b8b-... | 0 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |
| 3bf2a3b3-e201-4bb1-... | 0 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |
| 875c1145-a793-4243-... | 0 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |
| a2c63577-2bff-4104-... | 0 | APPROVED | 35fccfba-b4f8-4393-b211-c258d0a79076 |