WEBVTT 00:01.140 --> 00:06.660 So this section of the course is all about pagination, which is sometimes called chunking data, and 00:06.660 --> 00:12.870 if you recall a while ago here in Models Don't Go, we created a function that allows us to get all 00:12.870 --> 00:15.370 orders from the database and it works really well. 00:16.170 --> 00:19.570 The problem, of course, is that it works really well with tiny bits of data. 00:19.860 --> 00:24.110 What happens if we have five thousand or ten thousand or one hundred thousand orders? 00:24.570 --> 00:30.420 Well, every time we call this function, it will cheerfully attempt to populate a slice of pointers 00:30.420 --> 00:33.300 to orders and put one hundred thousand records in there. 00:33.330 --> 00:39.660 And it might work, but at some point, if nothing else, our contacts will exceed three seconds and 00:39.660 --> 00:45.930 this function will fail in the same way if we try to display a hundred thousand records all at once 00:45.930 --> 00:46.770 on a Web page. 00:46.800 --> 00:51.960 Well, that's not exactly an ideal user experience because it's going to take a long time to render 00:51.960 --> 00:52.620 that table. 00:53.340 --> 00:58.880 So what we want to do instead is to chunk our data up, to break it into manageable slices of data or 00:58.900 --> 01:01.370 manageable sections of the overall results. 01:01.980 --> 01:02.830 So to make that happen. 01:02.850 --> 01:08.580 What I'm going to do, say some time, is copy the entire get all order function and I'll leave the 01:08.580 --> 01:09.810 existing one there. 01:09.810 --> 01:10.970 Even though we're not going to use it. 01:11.730 --> 01:12.900 It's there for you to refer to it. 01:13.560 --> 01:20.500 And I'll paste it right below, get our orders and go to the top and change this to get all orders patinated. 01:24.660 --> 01:32.700 And I'll change the comment returns a slice of all orders of a subset of orders. 01:34.080 --> 01:37.270 And to make this work, we're going to have to know two things. 01:37.320 --> 01:41.220 First of all, how many records do you want for a given query? 01:41.640 --> 01:44.480 And secondly, what page are we currently on? 01:44.970 --> 01:48.350 So to do that, what we're going to do is accept a couple of parameters here. 01:48.990 --> 01:50.760 We're going to accept page size. 01:51.540 --> 01:56.520 In other words, how many orders per page of data do you want and what page are we currently on? 01:57.150 --> 01:57.900 Which will be an end. 01:58.110 --> 02:02.030 OK, so these are two parts to this function. 02:02.220 --> 02:09.090 Now down here, before I declare my orders, let's figure out what our offset is going to be and I'll 02:09.090 --> 02:10.500 explain what offset is in a minute. 02:10.980 --> 02:11.650 Offset. 02:11.670 --> 02:21.270 It's nothing more than the current page minus one times page size that would tell us how far from the 02:21.270 --> 02:24.680 beginning of the results we should offset what we're looking for. 02:24.870 --> 02:31.410 And of course, page size tells us how much to limit our query for and to make this work will come down 02:31.410 --> 02:36.600 here to the bottom of our query and add another directive limit. 02:36.720 --> 02:37.440 Questionmark. 02:39.860 --> 02:42.890 And offset questionmark with no comment. 02:43.460 --> 02:44.450 You don't want to call it. 02:46.190 --> 02:55.370 And then we modify this query to pass in page size, and I said, now if I take this query right now, 02:55.370 --> 03:07.280 for example, and go to my database and paste it in there and change this to say limit to offset zero. 03:07.700 --> 03:10.790 OK, and hit, run, run current. 03:11.420 --> 03:14.000 And that gives me two possible results. 03:14.030 --> 03:21.000 OK, now if I get rid of that limit statement, limit and offset and run it again, now I have three. 03:21.170 --> 03:26.360 So you can see that I correctly patinated this data and that's all there is to it for that part. 03:27.350 --> 03:28.730 So there's more work to happen. 03:28.730 --> 03:34.390 Of course, after we get all of this information down here, we get all of our orders populated. 03:34.760 --> 03:38.890 We need to send back a little bit more information because we're going to need it on the front end. 03:39.200 --> 03:44.600 We want to send back, for example, the total number of records so we know what page wrong. 03:44.600 --> 03:46.010 We know what limit we're going to be. 03:46.010 --> 03:48.560 But what is the total number of records? 03:48.590 --> 03:49.420 What we're going to need that. 03:49.990 --> 03:57.530 So I'm still going to modify my query variable and make it now say these back checks here. 03:58.130 --> 04:01.490 I'll just say select Count Odon ID. 04:01.670 --> 04:05.740 And this is pretty much the same as what we had before from orders. 04:05.770 --> 04:11.150 OK, and then I'll join it to widgets because I need that for the where clause left. 04:11.150 --> 04:29.240 Join widgets W on O widget ID is equal to ID and we'll say where W is recurring equals zero and that 04:29.240 --> 04:33.560 will get us all of the orders for widgets instead of subscriptions. 04:35.300 --> 04:44.890 So now we'll just execute that query or create a variable var total records, which will be an end and 04:44.890 --> 04:52.640 now we'll say Kotaro is a sign the value of and we're just getting one row from the database IMDB that 04:53.150 --> 05:04.540 query row context and the context and handed our query and then we'll see error equals Kotaro dot scan 05:04.790 --> 05:07.520 and we want to scan it into total records 05:10.640 --> 05:13.840 and we'll check for an error if error is not equal to nil. 05:15.620 --> 05:21.020 And I'm not going to populate that right now because I want to actually get another return value. 05:21.050 --> 05:22.250 And we'll fix this up in a minute. 05:22.460 --> 05:25.070 We also need to know what is the last page. 05:25.070 --> 05:26.500 We're going to need that in the front end. 05:26.510 --> 05:27.260 And that's really easy. 05:27.440 --> 05:35.660 Last page is a sign the value of our total records, which we just got back up there on line for sixty 05:35.660 --> 05:36.050 three. 05:37.360 --> 05:42.590 Divided by page size, which we received as a parameter to this function. 05:43.600 --> 05:51.940 Now I'm going to return orders and I'm going to return last page and I'm going to return total records 05:53.710 --> 05:56.400 and no error, which means I'm here. 05:56.650 --> 06:04.150 I can now return nil zero zero and the error and I'll copy that. 06:05.260 --> 06:07.750 And everywhere else I'm returning an error. 06:07.840 --> 06:12.010 I pasted in there and I'll paste it in there. 06:14.360 --> 06:21.230 And finally, I'll change this to return a slice of pointers to order an entry and an error, and now 06:21.230 --> 06:22.140 all my errors go away. 06:22.970 --> 06:28.510 So that is our query or our method to actually get patinated results for all orders. 06:28.910 --> 06:34.940 So the next step is to go update the handler and the front end, of course, to use this query instead 06:34.940 --> 06:38.300 of the one that is currently using, which is just get all orders. 06:38.300 --> 06:40.010 So we'll change the functions that's called. 06:40.010 --> 06:42.740 And we'll have to make some modifications to the front end code as well. 06:43.280 --> 06:45.470 And we'll get started on that in the next lecture.