1 00:00:01,820 --> 00:00:05,840 OUR FIRST ON MESSAGE function has been put together but as I mentioned there's a little issue with the 2 00:00:05,840 --> 00:00:10,910 code implementation to take note of how inside of here we are taking the title and the price of the 3 00:00:10,910 --> 00:00:14,270 ticket using it to build a new ticket and just saving that directly. 4 00:00:14,270 --> 00:00:19,700 I want you to really think about what is going to be happening to the Mongo DV record that gets created 5 00:00:19,700 --> 00:00:21,360 to reflect this ticket. 6 00:00:21,380 --> 00:00:23,780 Let me give you a little hint. 7 00:00:23,840 --> 00:00:24,230 All right. 8 00:00:24,270 --> 00:00:28,890 So this diagram right here we have created a ticket inside of our ticket service and we're going to 9 00:00:28,890 --> 00:00:33,840 imagine that when this ticket right here is created we emit an event that is then received by the order 10 00:00:33,840 --> 00:00:37,350 service and the order service wants to receive that information. 11 00:00:37,350 --> 00:00:41,880 Take the title and the price and persisted into its own local tickets collection. 12 00:00:41,880 --> 00:00:47,400 Just think about what would happen when we insert this ticket with the code that we have currently. 13 00:00:47,400 --> 00:00:53,940 So with this code right here without a doubt we would get the exact same title and the same price no 14 00:00:53,940 --> 00:00:54,980 two ways about it. 15 00:00:55,080 --> 00:01:01,730 But remember whenever we insert a record into Mongo DV It gets assigned a random I.D.. 16 00:01:01,730 --> 00:01:08,480 And so this thing would be given an idea of well who knows what this randomly generated idea right here 17 00:01:08,600 --> 00:01:08,900 is. 18 00:01:08,900 --> 00:01:14,600 Without a doubt going to be different than the I.D. that was assigned back inside the ticket service. 19 00:01:14,870 --> 00:01:20,460 That means that we now have two records that are supposed to be mirrors or absolute copies of each other. 20 00:01:20,600 --> 00:01:24,390 But we have zero consistent identifier between them. 21 00:01:24,410 --> 00:01:31,580 So if I now ask you to give me the I.D. or the ticket for I.D. ABC from the order service you would 22 00:01:31,580 --> 00:01:34,250 look at your collection of tickets and you'd say you know what. 23 00:01:34,280 --> 00:01:41,290 I have no idea which of these records matches up with ticket ABC from the ticket service so whenever 24 00:01:41,290 --> 00:01:46,960 we are replicating data across services we want to try to make sure that we are using identical or consistent 25 00:01:47,050 --> 00:01:48,370 I.D. between them. 26 00:01:48,450 --> 00:01:53,270 That's gonna make sure that we can identify these unique records between all these different services. 27 00:01:53,290 --> 00:01:57,700 So long story short we want to make sure it inside the order service that this ticket we are creating 28 00:01:57,730 --> 00:02:03,450 has the exact same I.D. of ABC does give me the first thing that we're gonna fix up. 29 00:02:03,860 --> 00:02:04,810 So how are we going to do that. 30 00:02:05,350 --> 00:02:11,050 Well unfortunately as usual just a little bit complicated. 31 00:02:11,180 --> 00:02:14,710 The first thing we need to do is go and adjust our build function. 32 00:02:14,840 --> 00:02:20,630 Right now our build function does not expect to receive an I.D. our data object right here has the I.D. 33 00:02:20,750 --> 00:02:21,920 we get to pull that out. 34 00:02:22,220 --> 00:02:24,260 That's the idea the ticket that was created. 35 00:02:24,350 --> 00:02:28,850 But if we tried to pass that in like so we're gonna get an error because the build function does not 36 00:02:28,850 --> 00:02:34,030 expect to receive an I.D. let's go back over to our ticket model file. 37 00:02:34,240 --> 00:02:40,400 We're going to adjust the build function and make sure that it can receive an I.D. Here's my ticket 38 00:02:40,400 --> 00:02:44,850 model file I'll find my build method. 39 00:02:45,040 --> 00:02:51,790 Here it is right here and remember the argument of Adam is right here must conform to the ticket Adam's 40 00:02:51,790 --> 00:02:54,160 interface to make sure everything lines up. 41 00:02:54,160 --> 00:02:57,810 All we really have to do is add the I.T. property to the ticket address interface. 42 00:02:57,860 --> 00:03:03,130 So at the top of the file of ticket outers I'll say that whenever we try to build a new ticket we must 43 00:03:03,130 --> 00:03:08,380 provide an I.D. and it must be a string so we'll make the air go away. 44 00:03:08,800 --> 00:03:11,380 But unfortunately that's not going to really solve the issue. 45 00:03:12,830 --> 00:03:14,200 So let me tell you why that is. 46 00:03:14,210 --> 00:03:16,420 One more quick diagram. 47 00:03:16,660 --> 00:03:22,270 So you might recall that inside of Mongo DV we store all of our records with an underscore I.D. property 48 00:03:23,290 --> 00:03:27,620 so when we eventually load this information up into our ticket service from Mongo DB. 49 00:03:27,740 --> 00:03:33,620 It still has underscore I.D. It is only when we take this record and turn it into Jason to be transmitted 50 00:03:33,680 --> 00:03:39,290 over an event that that I.D. gets converted into normal I.D. of one two three instead 51 00:03:42,180 --> 00:03:47,400 right now when we receive that I.D. inside of our order service inside that event it shows up as I.D. 52 00:03:47,460 --> 00:03:54,530 of one two three and we would like to make sure that when this record gets saved to our orders database 53 00:03:54,710 --> 00:03:56,270 it uses that same I.D.. 54 00:03:56,690 --> 00:04:01,970 Unfortunately whenever we save a record with just I.D. by itself Mongoose ignores that I.D. property 55 00:04:02,060 --> 00:04:05,030 instead Mongoose says oh this thing doesn't have an underscore I.D.. 56 00:04:05,210 --> 00:04:11,180 Well then I'm going to give it a randomly generated I.D. and assign it to the underscore I.D. property. 57 00:04:11,180 --> 00:04:14,900 So if we were to take this record right now and save it we would end up inside of our database with 58 00:04:15,260 --> 00:04:24,070 I.D. of one two three title of concerts and then underscore I.D. of some randomly generated I.D. So 59 00:04:24,100 --> 00:04:29,290 all we really have to do here as the final step need to make sure that when we go and create the new 60 00:04:29,290 --> 00:04:36,920 instance of the ticket inside the build method we take that 80 and assign it to the new record as underscore 61 00:04:37,040 --> 00:04:41,350 I.D. to back inside of our ticket schema file. 62 00:04:41,630 --> 00:04:44,180 Here's the model's directory ticket to yes. 63 00:04:44,240 --> 00:04:49,720 Here's our build method rather than just passing through that entire adders object and just allowing 64 00:04:49,720 --> 00:04:55,150 it to assign the idea property we're going to instead assign all these properties one by one and as 65 00:04:55,150 --> 00:05:01,230 we're doing that we're going to rename the underscore I.D. property so I can put in an object and we're 66 00:05:01,230 --> 00:05:10,010 going to assign underscore I.D. from adders dot I.D. then the title from actors that title and the price 67 00:05:10,010 --> 00:05:13,280 from adders dot price like so. 68 00:05:13,510 --> 00:05:18,610 Now the way that I just did this to rename the underscore I.D. property or I.D. to underscore I.D. not 69 00:05:18,610 --> 00:05:24,320 super ideal because now we are relying upon listing out each property in great detail so in other words 70 00:05:24,320 --> 00:05:28,670 if we start to add some different properties to the ticket adders interface we're going to also have 71 00:05:28,670 --> 00:05:32,600 to come down to our build method and make sure we assign these properties through as well. 72 00:05:32,600 --> 00:05:37,190 And so that's just a little bit of technical debt just something to keep in mind. 73 00:05:37,220 --> 00:05:37,520 All right. 74 00:05:37,550 --> 00:05:41,240 So that should be it for massaging this idea around. 75 00:05:41,380 --> 00:05:44,050 Let's take a quick pause right here and continue it in just a minute.