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
0:02 around our data access layer
0:04 isolating data access so we don't have to worry
0:07 about how that's done all over our app
0:09 just in the one data access layer
0:11 and the other is performance in caching.
0:14 In terms of isolation, this was pretty simple.
0:16 We wrote a simple function called my_measurements
0:19 and that found its way over to the server
0:21 and it called my_measurements thing there.
0:24 Now this is really, really simple.
0:26 Probably in a real app, maybe there's two different calls
0:29 or something more interesting is happening
0:31 or there's some kind of validation, things like that
0:34 but even this is not terrible because it is a place
0:37 where we can extend other things.
0:39 We can put logging, or we can put other validation
0:42 or we can put performance improvement, things like that.
0:45 Speaking of performance improvement
0:47 we saw that if we want to cache stuff on the client side
0:51 this will go much, much faster.
0:53 Remember, Anvil web apps are Single Page Applications.
0:55 That means they pull down all their data
0:57 and they run without refreshing the page.
1:00 They also don't go back and talk to the server
1:02 unless you call functions, things like
1:06 So if we can avoid doing those
1:08 we can make our app run nearly instantaneously
1:11 at least instantaneous as far as the user's concerned.
1:14 So we can expand on this and have a little local variable
1:17 called measurements, and then we can check
1:20 and see if that's been set.
1:21 If it has, we don't need to go back and ask again.
1:23 We're assuming this is the only copy of the app
1:25 working with the data, and maybe we can have
1:28 some kind of refresh mechanism.
1:29 But for now we're assuming this is the one instance
1:31 of the app working with the data.
1:33 So if it hasn't changed within the app, it hasn't changed.
1:36 So we just check.
1:37 If we have measurements, just give 'em that.
1:39 Otherwise, go to the server, get the measurements
1:42 store 'em in this local variable, and give it back.
1:45 You saw that when we did things like add a new measurement
1:47 we just nulled this out.
1:49 We just set this to none.
1:50 That erased the measurements that were there
1:52 and the next time someone asked
1:54 it fell through to the server and got the fresh copy.
1:57 Super easy, super fast, and really
1:59 really useful for the users.