T
TanStack2mo ago
provincial-silver

Trying to select the latest of visit of a customer

I have these schemas:
const customerSchema = v.object({
id: idWithDefaultSchema,
first_name: v.pipe(v.string(), v.minLength(3)),
last_name: v.nullish(v.pipe(v.string(), v.minLength(3))),
phone_number: v.nullish(phoneNumberSchema),
organization_id: v.string(),
});

const visitSchema = v.object({
id: idWithDefaultSchema,
date: v.date(),
customer_id: idSchema,
status: visitStatusTypeSchema,
});

const visitServiceSchema = v.object({
id: idWithDefaultSchema,
service_organization_id: idSchema,
custom_service: v.nullish(v.string()),
payment: v.nullish(v.number()),
duration: v.nullish(v.string()),
customer_visit_id: idSchema,
});
const customerSchema = v.object({
id: idWithDefaultSchema,
first_name: v.pipe(v.string(), v.minLength(3)),
last_name: v.nullish(v.pipe(v.string(), v.minLength(3))),
phone_number: v.nullish(phoneNumberSchema),
organization_id: v.string(),
});

const visitSchema = v.object({
id: idWithDefaultSchema,
date: v.date(),
customer_id: idSchema,
status: visitStatusTypeSchema,
});

const visitServiceSchema = v.object({
id: idWithDefaultSchema,
service_organization_id: idSchema,
custom_service: v.nullish(v.string()),
payment: v.nullish(v.number()),
duration: v.nullish(v.string()),
customer_visit_id: idSchema,
});
I'm trying to get the last visit that the customer made but, I can't figure out how to write the query with tanstack db. My last attempt was joining the two collections and then grouping by customer id and then trying to select biggest (last) visit date but I found that max doesn't support dates 🙁 Is there another way to do this?
15 Replies
provincial-silver
provincial-silverOP2mo ago
Looking at max and min I think adding support for dates should be easy... at least that what it looks like.
stormy-gold
stormy-gold2mo ago
Yep, we need to add support for dates to max/min. You are very welcome to have a go, as you said it should be quite easy. I'll review any PR as soon as you post it. The proposed query would then work.
provincial-silver
provincial-silverOP2mo ago
I'm looking into but, I noticed that max always returns a number. Is that OK if we pass in a date and, we get back, a number? Or should I override something somewhere to make it return a date?
evident-indigo
evident-indigo2mo ago
generally you order by date and limit 1
provincial-silver
provincial-silverOP2mo ago
Well, I want all the customers and I want the latest date for all of them. I think that approach would work for a single customer, but not when I'm getting multiple.
stormy-gold
stormy-gold2mo ago
Yep, we can change max to return the type that is passed in, for for dates return the max date.
provincial-silver
provincial-silverOP2mo ago
I wonder if I have to do some work to make it work with gte and the like? Right now, I'm pretty sure it doesn't work with my changes since max and min now return the date that was passed in.
stormy-gold
stormy-gold2mo ago
If you're happy to pick that up too that would be awesome! It should be relatively simple too.
provincial-silver
provincial-silverOP2mo ago
Yep, giving it a shot.
provincial-silver
provincial-silverOP2mo ago
GitHub
feat: add support for dates to max and min functions. by MAST1999 ...
I tried not to change too many things. If something doesn't look right, or we need some tests that are missing, I'm happy to try and make those changes. BTW, I tried adding date sup...
stormy-gold
stormy-gold2mo ago
Awesome! I'll take a good look at it tomorrow. Thank you.
provincial-silver
provincial-silverOP2mo ago
Just pinging as a reminder I need a re-review on my PR.
stormy-gold
stormy-gold2mo ago
Hey. Yep, it's not dropped off the radar. We've just been a bit stacked on bug fixes the last couple of weeks. I'm planning to try and get through the backlog of PRs and issues this coming week. Really appreciate the contribution!
evident-indigo
evident-indigo2mo ago
I also was rude enough to have a baby and go on parental leave so Sam & Kevin have been short handed 😆
provincial-silver
provincial-silverOP2mo ago
No worries, just sending a message as a reminder 🙂 Thanks for all the hard work!

Did you find this page helpful?