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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So, we saw that we could write some code like we did over here to query our measurements and we also wrote some code here to submit those.
0:11 Now, there's a two-step process that I want to go through here to make this a little bit better architecturally and performance-wise.
0:19 Architecture first, performance second. Remember, we had our navigation and we could have just crammed that all into various forms
0:26 but we have a lot more fine-grain control and we can make sure everything happens just right if we have these go_home, go_compare, go_to_accounts
0:34 and so on. So what I want to do is create a data access layer compliance side module that we're going to use to talk to the data base.
0:42 First and foremost it's just going to be to isolate that so here we just call a really simple function and we get what we want. So let's go over here
0:49 and maybe that's not the name we want how about data_access. And let's just go over here to these two we'll say import data_access
1:02 and let's just replace this part here with something like my_measurements. Now, we need to go over here and define a function again
1:14 so here's how you use it and this one's even simpler, there's really nothing to it. And I'll just grab this little bit here
1:24 measurements, and it's going to be really simple. You might be thinking, Michael, this is too simple to justify making this.
1:32 Hold tight, like I said, architecture, then performance. So the next one that we're going to do is add measurement like so
1:42 and this is going to take similar arguments as we had here so, just to save typing, let's just rob that sucker put that here like that.
1:53 And then, we'll go over to the AddMeasurementForm at the top, say import data_access way down here.
2:06 We'll go down here and we'll say add, we'll rewrite it add_measurement, we just didn't quite finish it, did we?
2:15 We'll add_measurement, and you know what? It probably takes pretty much exactly the same argument. And again, I'm just going to put this here.
2:24 Let's just make sure it still works. Now this, again, seems like a silly thing because here we just have this but we're going to expand this a ton.
2:32 It's going to get lots better and more useful. We'll just see how this works. So over here, we've pulled down our two records
2:38 when we go home, if we go here, and we come back see again, notice it's calling a function correctly and we have a bunch of output.
2:47 That's really good. Let's do one more add. Let's just work our way backwards in time I was apparently 180 and my resting heart rate was 72 that day.
2:57 So we add that in, and we broke something here. Yeah, that's right. So we get to our data access. This will be a record date
3:12 weight in pounds, and resting heart rate. Whoops, let's try one more time. Going to add, finally, let's give it a shot, is it going to work?
3:22 It does, and look at this, now we have five items. So we have this factored in a way that as we use it throughout the application
3:31 things are going to be a little bit easier. It's going to be a little bit more isolated. Anything we might want to change about data access
3:36 like hint, hint, performance we can do in that one location and have the entire app benefit from it.


Talk Python's Mastodon Michael Kennedy's Mastodon