1 00:00:00,900 --> 00:00:04,640 In this video we're going to write out an implementation for our order model. 2 00:00:04,650 --> 00:00:08,890 We're going to write out some description of the order model using some Mongoose related code. 3 00:00:08,910 --> 00:00:11,980 Let's get to it right away back inside of my editor. 4 00:00:12,050 --> 00:00:14,800 I want to very quickly give you just a very fast reminder. 5 00:00:14,810 --> 00:00:16,630 We've been over this twice downside of course. 6 00:00:16,670 --> 00:00:22,010 If you go back to our OTT service and find the models directory and then user t inside there. 7 00:00:22,030 --> 00:00:26,210 So remember whenever we're getting a mongoose and type script to work together a good way of doing this 8 00:00:26,210 --> 00:00:28,910 is to write out those three interfaces at the top of the file. 9 00:00:29,090 --> 00:00:34,210 One describes the properties that are used to create a user or create a record. 10 00:00:34,230 --> 00:00:39,570 We've got another interface that describes all the properties that a saved document has and then a third 11 00:00:39,570 --> 00:00:43,310 interface to describe all the properties that the overall model has. 12 00:00:43,320 --> 00:00:50,040 Remember the model represents the overall collection we build that interface to Scott the model to specifically 13 00:00:50,040 --> 00:00:54,660 add in that one extra method right there at the build method the whole reason that we're putting to 14 00:00:54,810 --> 00:00:59,700 that build method is just to allow typescript to do some validation or type checking of the arguments 15 00:00:59,700 --> 00:01:06,580 or are using to create a new document so that we got that reminder out of the way back to our order 16 00:01:06,580 --> 00:01:08,640 service inside of our SRT directory. 17 00:01:08,650 --> 00:01:17,180 We'll make a new folder called models and then inside there a new file called order that T.S. at the 18 00:01:17,180 --> 00:01:26,810 very top let's get mongers will then create those three interfaces we don't need to enter or export 19 00:01:26,820 --> 00:01:27,690 that my mistake. 20 00:01:27,690 --> 00:01:37,950 So interface order matters interface order duck extending Mongoose stop document 21 00:01:41,420 --> 00:01:46,400 and then order model extending Mongoose dot model. 22 00:01:46,530 --> 00:01:48,410 And this is a generic type. 23 00:01:48,720 --> 00:01:56,570 So we will put in order duck like so let's down provide some types for each these different interfaces. 24 00:01:56,570 --> 00:01:57,050 First off. 25 00:01:57,070 --> 00:01:58,190 Order adders. 26 00:01:58,210 --> 00:02:03,760 So this thing is going to have a user I.D. That is a string a status for the order which is give me 27 00:02:03,760 --> 00:02:04,710 a string as well. 28 00:02:04,810 --> 00:02:07,330 We're going to come back and update the type on string and just a little bit. 29 00:02:07,330 --> 00:02:12,610 But right now we'll just leave it as a string then expires at. 30 00:02:12,860 --> 00:02:18,300 Which will be a data object and then finally and this one is going to add a little bit surprising because 31 00:02:18,300 --> 00:02:23,910 back on this diagram over here I said that in order was going to have a ticket I.D. and so that implies 32 00:02:23,910 --> 00:02:27,790 that well we're going to have a ticket I.D. that has a string or something like that. 33 00:02:27,840 --> 00:02:32,370 But remember in the last video we just discussed that to actually relate an order with a ticket. 34 00:02:32,490 --> 00:02:36,750 We're going to somehow embed a reference over to a ticket document. 35 00:02:36,850 --> 00:02:40,950 So to do so and we'll go further into this feature in just a little bit we're going to say that order 36 00:02:40,950 --> 00:02:49,270 adders has to have a ticket property not ticket i.e. a ticket property that is an instance of a ticket. 37 00:02:49,290 --> 00:02:51,450 So we're going to write in ticket doc right there. 38 00:02:51,510 --> 00:02:56,280 We do not have a ticket Doc type inside of our orders service just yet. 39 00:02:56,280 --> 00:03:00,260 So right now that's going to result in an error and we're not going to fix that for a little bit. 40 00:03:00,330 --> 00:03:01,710 So we're going to leave that for right now. 41 00:03:01,710 --> 00:03:06,090 We'll come back to it and discuss a bit more about what's going on with this ref population feature 42 00:03:06,090 --> 00:03:08,570 in a moment. 43 00:03:08,570 --> 00:03:10,520 Next up is order duck. 44 00:03:10,520 --> 00:03:14,960 So order documents are going to have the exact same set of properties that are required to create an 45 00:03:14,960 --> 00:03:15,610 order. 46 00:03:15,620 --> 00:03:19,880 Remember the reason we create these two separate interfaces is because the properties that are required 47 00:03:19,880 --> 00:03:24,770 to create an order might be different than the properties that actually end up on an order. 48 00:03:24,770 --> 00:03:27,470 So that's why we've got these two separate interfaces. 49 00:03:27,470 --> 00:03:32,180 This project here or this service right here is finding the first time where they are going to end up 50 00:03:32,180 --> 00:03:34,150 with a different set of properties. 51 00:03:34,160 --> 00:03:36,440 That's why we have the two separate ones. 52 00:03:36,620 --> 00:03:39,170 But right now they are going to be temporarily identical. 53 00:03:39,170 --> 00:03:43,760 So going to take all the properties off of order adders will be just added and paste them down into 54 00:03:43,760 --> 00:03:47,060 order duck as well. 55 00:03:47,110 --> 00:03:49,090 Then after that let's do our order model. 56 00:03:49,240 --> 00:03:54,520 So all we're doing here is saying that a model has this one extra little method on it called built. 57 00:03:55,140 --> 00:04:01,170 It's going to take an argument called adders that is of type order adders and out of that we will get 58 00:04:01,170 --> 00:04:04,740 an order document. 59 00:04:04,830 --> 00:04:07,900 So there's are three interfaces. 60 00:04:08,010 --> 00:04:12,300 So now let's build out our schema that actually describes the different properties in some rules about 61 00:04:12,300 --> 00:04:17,640 them that we're gonna have on an instance of in order to create order schema. 62 00:04:17,710 --> 00:04:24,100 This will be a new Mongoose start schema on the first argument right here. 63 00:04:24,110 --> 00:04:27,670 We'll have an object where we'll provide these different properties. 64 00:04:27,680 --> 00:04:29,990 So we want Mongoose to worry about on in order. 65 00:04:30,410 --> 00:04:35,900 So first off there's going to be a user I.D. its type is going to be string. 66 00:04:36,110 --> 00:04:38,210 Remember the type right here of string. 67 00:04:38,210 --> 00:04:43,970 This is actual kind of javascript code that really gets executed whereas the interfaces up here are 68 00:04:43,970 --> 00:04:48,080 typescript interfaces that get stripped out before our code is executed. 69 00:04:48,080 --> 00:04:50,870 The types we're listing right here are typescript types. 70 00:04:50,930 --> 00:04:57,070 So this is a lowercase string whereas down here this is an actual value that will be consumed by mongoose. 71 00:04:57,140 --> 00:05:03,170 This right here has a capital S string as that is the string constructor we definitely want to make 72 00:05:03,170 --> 00:05:07,550 sure that every order that gets created has a user tied to it. 73 00:05:07,550 --> 00:05:15,270 So put on a required true property next up we're gonna say that every order has a status it's going 74 00:05:15,270 --> 00:05:20,840 to be of type String and that will also be required. 75 00:05:20,940 --> 00:05:25,840 So every order that gets created at all points in time it must have a status to describe if it is awaiting 76 00:05:25,950 --> 00:05:30,660 payment if it's been canceled if it's been purchased and so on. 77 00:05:30,720 --> 00:05:32,790 Next up expires at. 78 00:05:32,880 --> 00:05:38,060 So this will be of type Mongoose dot schema dot types dot dates. 79 00:05:38,070 --> 00:05:41,960 That's how we say to Mongoose that something is going to be a date property or a date. 80 00:05:41,970 --> 00:05:43,440 Object. 81 00:05:43,540 --> 00:05:47,950 Now this one is not going to you will be required because there might be scenarios where we do not actually 82 00:05:47,950 --> 00:05:49,770 want in order to ever expire. 83 00:05:49,840 --> 00:05:54,070 For example after a ticket has actually been or an order has actually been paid for it's own pace for 84 00:05:54,070 --> 00:05:59,890 an order we no longer want to expire it so we might eventually set expires out to be null or undefined 85 00:05:59,920 --> 00:06:02,440 or something like that. 86 00:06:02,500 --> 00:06:07,330 And then finally here's where we're going to set up our actual reference between the order document 87 00:06:07,360 --> 00:06:08,430 and a ticket. 88 00:06:08,470 --> 00:06:17,350 So we're gonna say that there is a ticket property its type is going to be a mongoose schema types object 89 00:06:17,470 --> 00:06:23,970 I.D. and then we're going to put on here as well ref of ticket. 90 00:06:23,970 --> 00:06:28,860 So again in a time back into this whole reference system that we just discussed in the last video we're 91 00:06:28,860 --> 00:06:33,830 going to get a deeper dive on that whole system in just a moment. 92 00:06:33,830 --> 00:06:34,090 All right. 93 00:06:34,100 --> 00:06:38,790 So there is our Mongoose schema or least the first argument we're not going to put in the options argument. 94 00:06:38,810 --> 00:06:44,250 So a second argument to this thing that is also give you an object inside of here we're going to set 95 00:06:44,250 --> 00:06:51,490 that to Jason property we'll define a transform function and remember the whole goal the thing is to 96 00:06:51,490 --> 00:06:54,660 take in some value that we're trying to turn into Jason. 97 00:06:54,760 --> 00:06:59,320 We're gonna delete off that underscore I.D. property and set it on the I.D. property instead. 98 00:06:59,370 --> 00:07:06,010 So we'll say red dot I.D. is red dot underscore I.D. then delete red underscore i.e. 99 00:07:09,070 --> 00:07:16,250 get just a little bit more after all the schema stuff will then define the build method on the order 100 00:07:16,250 --> 00:07:18,020 schema statics object. 101 00:07:18,020 --> 00:07:24,170 So this is what is going to give us the build method on the actual order model so just like we've done 102 00:07:24,200 --> 00:07:30,280 on our other services we'll say order schema statics dot build. 103 00:07:30,420 --> 00:07:37,030 This is going to be a function that takes in some argument of adders and it must be of type order adders. 104 00:07:37,140 --> 00:07:44,330 Now we're going to return a new order and throw the attributes inside there we can air out of order 105 00:07:44,330 --> 00:07:50,390 right here but that will go away in just a moment right after we say const order is our actual Mongoose 106 00:07:50,390 --> 00:07:57,040 model remember model is a generic function so going to put on those angular brackets and then through 107 00:07:57,040 --> 00:08:06,080 in order duck ordered model then the name of the collection which is going to be order and then our 108 00:08:06,080 --> 00:08:17,170 order schema then finally at the very bottom export order in curly braces OK so that's it now at this 109 00:08:17,170 --> 00:08:21,280 point there's really two big points of discussion or two things we need to kind of figure out we need 110 00:08:21,280 --> 00:08:26,620 to understand a little bit more about how that ref system with Mongoose works because what is this type 111 00:08:26,620 --> 00:08:32,350 right here exactly why is the type object I.D. as opposed to say type ticket or something like that 112 00:08:32,690 --> 00:08:37,410 we've got to figure out a little bit more on that side we need to make sure that we define these ticket 113 00:08:37,440 --> 00:08:42,900 Doc interfaces somewhere inside of a project and then also I mentioned that we're going to give a better 114 00:08:42,900 --> 00:08:45,980 type 2 status right here to describe the status of an order. 115 00:08:46,110 --> 00:08:47,550 So we gotta figure that out as well. 116 00:08:47,550 --> 00:08:49,170 So just a little bit work left. 117 00:08:49,170 --> 00:08:51,980 Let's take a pause right here and tackle these problems in just a moment.