We have installed the Mongoose update.
If current module into our tickets service let's now go back over to our editor we're gonna wire this
thing up to our ticket model and then start to write out a couple of tests just to make sure that it
behaves as expected.
So back inside my editor I'm gonna find the tickets service make sure inside a ticket service we've
been working inside of orders inside they're going to find the models directory and find the ticket
to yes file then at the very top.
I will import update if current plugin from Mongoose update if
we're going to wire this thing up to our ticket schema that we created a little bit lower on inside
the file so I can scroll on down a little bit to our ticket schema.
Here it is right here and then right after the ticket Schema Definition we're gonna add in two lines
of configuration by going to first say ticket schema plugin and passing the update.
If current plugin and then right above that very important do we put this right above.
We're also going to tell muggers that we want to track the version of all these different documents
using the field version so using some property named version instead of the default of underscore underscore
V which as we've seen is the default version field with Mongoose so to rename that field we're going
to write out ticket schema set version key to simply version instead and that's it now by making this
change the version key right here.
There's one other little change we have to make inside of our ticket dock interface so I can scroll
up a little bit here's the ticket dock interface and remember the goal of this interface was to list
out all the different properties that a document has or an instance of a ticket has.
So this was an interface that list out not only the properties that a ticket has though specifically
a title price and user I.D. but it also includes all the properties that a normal document has as well.
One of the properties listed inside of the Mongoose Scott document interface is underscore underscore
V and that's what would normally allow us to write out some code such as ticket dot underscore underscore
V.
Normally if we had a ticket and if you're working on the ticket document we could write this out and
typescript would be absolutely okay with that because inside of this interface definition is something
that says that a ticket has an underscore underscore the property.
So we just change that.
We are now saying that to access the version fields we want to write out Beal to write out something
such as ticket dot version this version property is not defined inside of the document interface.
Nothing inside there says that a document has a version property.
So to help typescript to understand that a instance of a ticket has a version property you and I have
to manually add it to the ticket doc.
Interface so version is going to be a number like so and that is what is going to allow us to write
out something such as ticket Doc version without touch script complaining at us get some believe it
or not that is it for wiring up this optimistic concurrency control stuff but at this point I dunno
about you I'm thinking what did that do for us and that's why I would really like to write out a quick
test or two I want to write out a test and make sure that this version field is going to be tracked
automatically and make sure that if we try to save some record out of the appropriate order we're gonna
see some kind of error inside of my model struct free again inside of my ticket service I'm going to
create a new folder of underscore underscore test underscore underscore inside of there I'll make a
new file of ticket test T.S.
and then at the top we'll immediately start to write our test I'm going to import the ticket model from
up one directory ticket and then we're going to write out for right now just one simple test and we'll
get this description of it implements optimistic concurrency control we can probably think of a better
title for this test but I just want you to kind of have this term around to see a reminder of what this
stuff is all about it rather than having this video take a really long time.
However let's take a pause right here and then write out the implementation for this test in just a
moment.