Xano - Lazy Daily Updating

Build a Message of the Day system in Xano that rotates daily without repeating, using lazy updates to work on free plans. The endpoint checks if today's message already showed, picks an unshown message if available, or falls back to the oldest one, then updates the lastshown date to ensure it doesn't repeat tomorrow.
Chapters
So one thing that can be useful is to have things that change over time in Xano. And there are sort of two ways you can do this. You can do this very explicitly on a paid plan using a background task where you can have a task run every day and start updating things. You can have new information that's going along. But you can also do this implicitly and you can do this even on free plans. And I thought that would be a useful thing for us to talk about for a minute. The, um, let's just say we want to have a message of the day, right? And you have days and you have opinions about days. I can have another day about Thursday, you know, today is the day of Thor, god of thunder. And you know, you can have these lots of them and they will. And the idea is you want them to be showing up and you're sort of insensitive to when they show up. You just want to make sure that like you go through the whole list and you get to the bottom of the list before you start doing the next one, right? You don't want them to repeat all too frequently. And so to do this, we have, we create a little table called MOTD and we can populate through the API. That's subject for other videos. They always come with an ID and a created_at and you can add message, right? Which is a text field, right? You can do the plus here and say, I want to add text. And then we also have something called last_shown, which is going to be the date that goes along with that message of the day. And for that, we're going to create a little date field, right? So we have date field, which also has the attribute of being nullable that we don't need to set that immediately. And we have the message that goes along with it. And so we want to make sure that we're able to send one message then whenever someone's asking for it. And to do that, we're going to create an API endpoint in Xano. And I have a little group here called MOTD for message of the day, and it's going to have a little piece here called MOTD. So whenever we're calling this, we want to get the current message of the day. We're also going to make it lazily select a new message of the day if there isn't one already, right? Because we just saw there was no dates associated with any of those. So when I come in, it says, okay, please give me my message. And I want the message for today. And today is based on formatting the timestamp of now as YMD. We're just gonna say that YMD is our standard and that's the way we're gonna be working with things. Cool. And then we're going to get the record from MOTD. And we're going to ask, hey, do you have a record that matches today? And if you do, Hooray, we're going to go get information. And if you do not, well, then maybe not. Because what happens is in the event there is something for today, well, then it will say that the— we can ask about this very one in particular. If MOTD1 is empty, i.e., there was nothing for today, so we use the is empty filter, which would just reply is the thing I just asked for empty or is there something there? And if it's empty, i.e., there was nothing that matches today, then that would be considered true. So that's why we're using a true operator, but we're using isEmpty filter to ask that question. Now, if it is empty, we're going to do some business. But if it's not empty, there is a message for the day. Great. We skip over all the rest of this and we say that we're going to fetch the message associated with that message of the day, right? Because it was one field in there called message. And that way we get the text. So that's what's going to happen the vast majority of time. I ask for the current message and I return it. But once per day, there's going to be a situation where, you know what, I don't have a message today yet. And so in the event that in fact it was empty, that nothing's associated with today, then I'm going to ask questions a couple of different ways. Now, the first is what about a message that's never been shown before? Cause that's going to be maximum time since it was last shown, right? Cause never infinite. And so I'm going to run query all records from MOTD.. And I'm going to ask, look for everything where the last shown field in there is null, right? Because when it's blank, it's null. That's what we're looking for. Okay, that's pretty cool. And then I'm going to alter the output. And the output is useful because I can say I want to sort it. I'll give it a little pencil here. And I can say I want to sort it by last own shown ascending, which isn't all that interesting because it shall be blank anyway. And I'm going to enable paging and limit my results to 1. That way I just get the top result and I'm not slowing things down in case I have thousands, thousands, thousands of messages in there, right? It scales well. Fantastic. And I return it as nullDates. Now, even though it's only going to return one record, it's going to return technically an array of that one record. So I'm going to need to unwrap that if in fact data came back. So in the event that nullDates is empty, I'm going to go do another thing. We'll come back to that in a second. But if the nullDates is not empty, then I'm going to update update MOTD1, right, which is the one I first asked about that. And I'm going to say it's equal to NULLDATES with a filter of shift. And shift is a really useful array-based filter that will take the first element in that array and return it. So whatever was at the top of the list, pulls it off. Now since it only returned one element, that's fine. First element is all I care about and I'm done. By the way, as a point of interest, there's another one that in this case would have the exact same effect called pop. And in the event you wanted like the last element in an array, you could always do that. It would actually have the same effect. It doesn't really matter for the purpose of this particular demonstration. Now, that's situation number 2, right? There was nothing for today, but like one of these other things that we were looking at would be able to come back. And that would actually probably work out pretty well for us right now. I can do a run and debug. And I could do run. It says Mondays are awesome. Great. Let's go run again. And it'll tell me that Mondays are awesome. Why are Mondays so awesome? What is, what is your thing about Mondays in here, man? And I go back into my database and I go back into my MOTD and it tells me that Mondays are awesome because it picked one pretty much at random, right? That was presently blank. All right. Well, that, and as a result of this being set to the current day, it's always going to return this all day. So it will be the message for the day. Now, if I were to double-click on this, change it to 27th, right now it no longer is associated with today. And when I go back in asking for a message of the day, I'm clicking on the memo TD and I do run and debug. I do run and it tells me Mondays are the worst. Why are Mondays the worst? Mondays are the worst because when I went into the database, it picked once again something pretty much at random. And it told me, and it found that one, right? So once again, let's put this into a previous day. And let's just say for the sake of argument that we just did all of these, right? That we've now seen them all and we've done them all over the course of days of the week. And I have one that might be for the 22nd, right? And one that might be for the 24th and all that's fine. Now, I don't want these things to repeat unnecessarily, right? So it would stand to reason like what I really want is in this situation, be one that is the oldest, right? If I, if I've already shown these at some point in the past, then really I want the one that nobody's seen for the longest period of time. And in this particular example, the one that nobody's seen for the longest period of time is today's day of Thor: God of Thunder. And that was because last shown on the 22nd, which is earlier than any of those dates. So now let's go back to our code and let's talk about how we would solve that problem. Because that's a situation we haven't talked about yet. That's the one I said I'll get back to in a minute. So if I have, okay, so said if there's anything for today, I can return it, but there's nothing for today. We just saw that today is 28th and none of those dates were in there. I'm going to look for everything that's null that's never been shown before, but we've also already established that everything has a date in it. So this will never hit. So if the, if there was, so if there is nothing in null dates, right? And this is how you do that. You say if null dates, adding is empty filter, uh, and you just say if that is true, right? Just like the MOTD one, right? And since that had nothing in it, similarly, if there's nothing in null dates, i.e., didn't come back with anything, well then we gotta try something else, right? So now we're going to go ask for everything from MOTD, uh, based on a new kind of criteria. And that criteria is going to be sorting it by last shown. And you— we saw this in the previous one, right? Just didn't matter then because everything was null, but now they're not null, right? And we want to do order by ascending. See, by doing order by ascending, it's going to put the oldest one, the one with the smallest date first. And by making sure I then only pull off the very first one on that particular page, on that list, then I'm always only picking the first message and going to return that and not return any of the others. And now I can, um, And then I do basically the same thing because just like before with that null date thing, it's returning to me one item in the message of the day. And that only has a single, only one record, but it's wrapped in an array. And I'm going to unwrap it using the shift thing like we talked about before. And same as before, I could also use that pop filter. Done. All right. Now I've got all these. And the, our expectation is that should I do this, it should identify that today is Thursday, the day of the God of Thunder. And it should return that message. And then if I do it again, and then it should also update my list of messages of the day to indicate that's the one that was for today so that it gets repeatedly used. Today is Thursday, God of Thunder, great. And now if I go back over to my database, I have this set to the 28th. Great. And that means tomorrow on the 29th, it will tell me it is Wednesday, my dudes, which of course from my perspective will be incorrect because it's actually Friday. But that in general is how you would set yourself up for lazily incrementing the current value as you go from day to day to day for a message of the day type use case.










