Anvil: Web Apps with Nothing but Python Transcripts
Chapter: Databases: Storing and querying data
Lecture: Concept: Data-access layer

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's quickly review the two concepts around our data access layer isolating data access so we don't have to worry
0:08 about how that's done all over our app just in the one data access layer and the other is performance in caching.
0:15 In terms of isolation, this was pretty simple. We wrote a simple function called my_measurements and that found its way over to the server
0:22 and it called my_measurements thing there. Now this is really, really simple. Probably in a real app, maybe there's two different calls
0:30 or something more interesting is happening or there's some kind of validation, things like that but even this is not terrible because it is a place
0:38 where we can extend other things. We can put logging, or we can put other validation or we can put performance improvement, things like that.
0:46 Speaking of performance improvement we saw that if we want to cache stuff on the client side this will go much, much faster.
0:54 Remember, Anvil web apps are Single Page Applications. That means they pull down all their data and they run without refreshing the page.
1:01 They also don't go back and talk to the server unless you call functions, things like anvil.server.call. So if we can avoid doing those
1:09 we can make our app run nearly instantaneously at least instantaneous as far as the user's concerned.
1:15 So we can expand on this and have a little local variable called measurements, and then we can check and see if that's been set.
1:22 If it has, we don't need to go back and ask again. We're assuming this is the only copy of the app working with the data, and maybe we can have
1:29 some kind of refresh mechanism. But for now we're assuming this is the one instance of the app working with the data.
1:34 So if it hasn't changed within the app, it hasn't changed. So we just check. If we have measurements, just give 'em that.
1:40 Otherwise, go to the server, get the measurements store 'em in this local variable, and give it back.
1:46 You saw that when we did things like add a new measurement we just nulled this out. We just set this to none.
1:51 That erased the measurements that were there and the next time someone asked it fell through to the server and got the fresh copy.
1:58 Super easy, super fast, and really really useful for the users.


Talk Python's Mastodon Michael Kennedy's Mastodon