Birthday Sale! Up to 40% off unlimited courses & creative assets Birthday Sale! Save up to 40%!
by
Lessons:18Length:1.9 hours
Introduction to koa javascript framework 400x277
  • Overview
  • Transcript

3.5 Creating the Task Repository Interface

There is definitely something to be said for keeping things simple. But in this course, you are going to learn a little more than just that. You'll learn how to add additional flexibility into the API to handle storing and retrieving tasks from any storage mechanism you could want. This flexibility starts with a repository layer. And the first step to a successful repository is creating an interface to define it. Let's do that now.

3.5 Creating the Task Repository Interface

In the previous lesson we created a task model and I began to allude to a couple different layers that we want to add in to our application that are not only going to make it easier to read, easier to maintain, but it's absolutely gonna make it more flexible. And that's what we are going to start implementing in this lesson. So the first thing that we want to do Is identify what the structure is going to look like. So ultimately what I would like to show you is I would like to be able to go into my route and within here I would like to dig into some other layers of my application. Typically when I'm writing web applications like this, or APIs, I tend to go from a route or a controller type level of my application being the entry point for doing certain operations. And then I like to keep my controllers or my routes as thin as possible, meaning trying to keep a lot of the implementation out of there and in other places of my application. So typically what I do is I have entry points into the routes or the controllers and then they delegate off into a service. And then the service handles delegating a lot of the data operations to a repository or something like that. And then they can also handle other logic, other application business logic that needs to happen within my application. So that's kind of the basic structure that we're going for here. So ultimately we want to go from our route into a service, and then delegate all of the data access stuff to a repository. And its gonna become more obvious as to why we're gonna do that later on, but just kind of follow along with me. So but typically when I like to do applications like this I typically go down to the bottom layer, which in this case is gonna be the repository, and start to shape that around what our model is so I can build up from the bottom. So let's go ahead and begin that process now. So the first thing that I'm gonna do is I'm going to go into my source to create a new folder. And this one I'm going to call repositories. And within here I'm going to start by creating the basic look and feel or shape of what I feel like these repositories should look like. And in the beginning, the way that we do that in many languages and typescript is no different, is we're gonna define an interface. So the first thing that I'm gonna do is I'm gonna come into repositories, I'm gonna create a new repository or a new file here and I'm gonna call this the TaskRepository.ts, just like that. Okay, so in here what I want to do is I want to, once again, export, because I want to be able to make this accessible to different parts of my application. And this is going to be an interface. And then within here I'm gonna call this the iTaskRepository. And then within here all we're going to be doing is defining the methods that are going to be living in a typical task repository within this application. And as you can imagine they're gonna be fairly similar to what we did within our routes. We're gonna have the ability to get a couple different ways. Get all, gets an individual task and then the create the update and the deletes. So let's go ahead and just kind of stub those out a little bit. So let's start with a get-all. So this method is going to return a collection or an array of task objects as you might imagine. Now I'm gonna start to introduce also at this layer the concept of the asynchronous method of programming using the async and await keywords. But before we can actually get that far, what we have to understand is when we're doing the async development using the ECMAscript 2015 or above, we're still dealing with promises. We're still dealing with the same underlying fundamental concepts that we were within JavaScript and previous to that. But we're just gonna start using the promise as a return type so that we can take advantage of the asynchronous and the async key words later on. So we're not just gonna be returning an array of tasks, although we could do that. What we're actually going to do here is instead of just returning that, we're actually going to return a promise that's going to contain an array of tasks just like that. And that's the same process that we're going to follow for all the methods that are going to be defined in the iTaskRepository interface. So we have a getAll then we'll do an individual get where we pass in a task ID, which is going to be a number. And then the result of this is going to be a promise that's going to contain a task. So now we have our gets taken care of, let's go ahead and handle the create. So I'm going to have a create operation here. And the create operation is gonna take in a task. So we're going to assume that who's ever talking to this repository has already created a task object, and it's just passing it in in order to persist it wherever the implementation for this iTaskRepository happens to live. Now this is going to be of type task. And then typically with this one, I like to return a promise of a task object as well. So you could do one of two things here. You could return a full task, and that would be fine, or you could also return just the newly created ID. Either way it works, I've seen both of them out in the wild. I tend to return a full object that's populated not only with the data that was passed in, but also any other operations that my repository would have to take care of like an ID or any other maybe database generated properties if I had maybe create date, update date things like that, I would have that happen as well. So we have our create, let's go ahead and do our update. So in this case, we're also gonna be passing in a couple of different things. We're gonna be passing in a taskId, and we're gonna be passing in an actual task. Now, once again here, you could get away with just passing in the task, but I tend to break the two things apart in case in one instance of using this repository I do have a fully populated object, or maybe I don't. So that's kind of just one of the things that I like to do. And then we're going to be returning a promise once again, that's going to have the tasks, so it's going to have the full task object. And then finally we're going to have the delete. And the delete in this case is simply going to be passing in the task ID, which is going to be a number and then the return here. So once again you could get away with nothing, you could just return an empty promise with nothing in it. I don't oftentimes like to do that, there's certain instances where I feel like after you do a delete, you'd like to kind of have a record, at least know what you deleted, if you've successfully deleted it. So this case this would be the last time when I would return the full object that I've deleted just so that you know what the properties were on it, what its current status was when you deleted it. So there you go, there's our iTaskRepository interface that's going to contain our five methods, our getAll, our get, our create, update, and delete. So this is great, we've got this taken care of, but now we wanna be able to start to work with actual implementations. So in the next lesson I'm going to start to create an implementation of this interface that's going to allows us to not only uses this in development and in test mode, but also get our application up and running a little bit quicker.

Back to the top