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