FREELessons: 18Length: 1.9 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.6 Creating the In-Memory Task Repository

Since the focus of this course is not databases or how the Tasks are stored, we don't want to spend too much time on those details. Because of that, we'll stick with a very simple in-memory repository that will be easy to write and quick for testing. In this lesson, you'll start with the basic read operations.

3.6 Creating the In-Memory Task Repository

With our iTask repository interface in place, we can now create a concrete implementation for this particular application. Now what we're gonna be doing here is we're gonna be dealing with a very simple example here. Like I said before, using a database and things like that are not really the focus of this course. So I'm gonna create a very simple implementation of this interface that's going to store these tasks just in memory. So basically that means that every time you restart your application, everything that you had saved or created is going to be wiped away and you're gonna be starting from scratch again. Which in this case is fine because we just wanna be able to verify the functionality and get used to using TypeScript and the async program model with a Koa web-based application. So what we're gonna do now is we are gonna go into our repository's folder. We're gonna create a new file, and this is going to be the inmemorytaskrepository.ts. And now in here, we're simply going to do another export. We're going to export a class called InMemory task repository. And this is going to implement the ITaskRepository. And we're gonna be getting the red squiggleys here because it knows that we don't have all the implementations that are necessary for the ITaskRepository. So let's just go ahead and cheat. We'll come over here and we'll grab these here like this, copy them, bring them over. And obviously, these don't have implementations yet. So we'll just go ahead and add in our curly brackets here to denote that these are methods. And then the problem here is still that it's not gonna like some of these things because we don't have any return values. But that's okay, we're gonna get there at some point or another. So it's also wanting to bring in task, so we need to make sure we have a reference to our model here. All right, so now we're ready to kinda get started. So what we wanna do is be able to create a very simple in-memory repository, which, like I said before, just means that we're gonna be storing this data in memory during the operation of this application. And when it stops, everything is gone. And it's really just that simple. So what we're gonna do is we're gonna need a place to store these tasks. So we're just gonna create a little private variable here. We'll call this _tasks. And this is going to be an array of tasks, and we'll just initialize it to empty. So ultimately, that's where we're gonna be storing all of this information. And that's where we're storing our tasks. It's just going to be in this variable right here. So now what we're going to do is let's go ahead and focus on the gets in this lesson. We'll go through the process of writing this, and then we'll come back and finish it up in the next lesson. So the first one that we wanna handle here is the getAll. So at this point, I really want to go into my store, wherever that is, in this case it's just in this variable right here, and I wanna return everything. So let's go ahead and do that. So all we're gonna do here is say return this._tasks. Now, you're gonna notice here that it doesn't like this. And this is another one of those kind of early on stumbling points when you're getting into this asynchronous programming model. Well, if you take a look at this, what we're returning here in this statement is a task array. And that's fine, but the method here, the definition of the method says we're going to be returning a promise of task. And the way to make this work is simply by adding the async keyword before the method name. So as you can see here now, we have async getAll, and it's gonna return that promise. So basically what that does is that this async keyword is basically gonna do the unwrapping or the wrapping of this return value in this promise. So that's kind of the way the async keyword is going to work. Now, how do we get the asynchronous functionality when we're calling this method is by using the await keyword. So that's kind of like the yin to the yang of the async key word here, but we'll see that when we get into the rest of the application. So that's gonna be our getAll. It's gonna be pretty simple, not a problem. And now for the getIndividual, we're gonna do something a little bit similar. We're gonna need to add that async keyword again, and I wanna get a specific task that has a particular ID. Well, how are we gonna do that with this implementation? Well, we'll simply going to say, a const task, and that's gonna be equal to and what we wanna do is, is we wanna look into our tasks. And then we want to find a particular task that meets a certain criteria that is true for a particular predicate that we're gonna pass in here. And that's going to be, we're gonna reference all of the tasks individually as t. And then the implementation is simply gonna be where is equal to, so make sure you use the triple equals cuz that's gonna make sure it's equal and of the same type. Where it's equal to the taskId that was passed in. So this is either going to find something or it's ultimately gonna be undefined. Either way is fine, because it is possible to search for an individual task in this case, or any object, depending on your implementation, by an ID and have it not exist. That is a valid scenario. So this task could either be a task object or it could be undefined. So now we have that task, or whatever value it is. We're simply going to return the task. And we'll go ahead and save that. So now, as you can see, we've begun our InMemoryTaskRepository implementation. We have the getAll and the getOperations handled. And in the next lesson we're gonna finish up with the create the update and the delete.

Back to the top