Repository Pattern in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to non-js API tutorials in this video we will talk about repository pattern we will cover what repository pattern is its main characteristics that will explain why this pattern is useful and we will refactor our API code to use a repository pattern the repository pattern is a structural pattern that abstracts and centralizes data access operations provides a consistent and simplified interface for application to interact with the data sources in essence the repository pattern acts as a mediator or a middleman between the application's business logic and the data storage shielding the rest of the code from the details of how data is fetched stored or manipulated it encapsulates the data access logic which can include querying databases making API calls or working with any other data source into a set of defined methods and operations key characteristics and benefits of the repository pattern include abstraction it provides abstract layer for data operations allowing developers to work with the data using high-level methods rather than dealing with low level data access code centralization data access logic is centralized within the Repository making it easier to manage maintain and modify consistency it enforces a consistent way of interacting with data across the application ensuring that data related operations follow the same patterns and conventions testability the pattern facilitates unit testing because you can easily replace the actual data access logic with a mock data for testing purposes flexibility it enables the switching of data sources or storage mechanisms without affecting the rest of the application for example you can switch from a relational database to a nosql database or an external API with minimal code changes now let's go ahead and introduce a repository pattern in our code in the SRC folder let's create a new file called types.d.ts this file will let us use types and interfaces without importing them in our code let's go ahead and Define two types and two interfaces in this file so first we're going to have a torque entity type and we have the following properties ID travel ID name starting date ending date price and then we're gonna Define travel entity again ID name description slog string number of day and tours now we're going to Define interface torque attributes and again we're going to have ID travel ID name starting date ending date price created updated it and interface travel attribute again ID name description slog is public number of days tours and created an updated date travel attributes interface and tour attributes interface is what we're gonna get back from the database or rather Repository and then travel entity type and tour entity type is what we are going to be returning in the API response as you may have noticed we already have tour entity type definition and travel entity type definition in our code so let's go ahead and remove it let's go ahead to resources to our resource and let's go ahead and remove the tour entity and let's save as you can see we didn't get any errors because this type is imported automatically from the types that t.ts file now let's go ahead to travel resource and again we're gonna remove the travel entity from right over here and we can also remove it from the import and let's go ahead and save it again we didn't get any errors now let's go ahead and use interfaces with the models so right here we have a model and we can put tor attributes and in travel model we're also going to put travel attributes let's go ahead and save everything now in the SRC folder let's create a new folder called repositories and in this folder we create three files the first file will be base Repository the next file will be tour Repository and finally we're going to have a travel Repository in the base repository let's create a Biz repository class we will use inheritance pattern where we put majority of the code in the base repository and then we will use child classes that will maybe change some of the functionality of the methods of the base repository so let's go ahead and Export default abstract class base repository with a generic a attributes right and it will have a model class and will be modeled static that we imported from sqlise and modeled static is basically a static model that will be a tour or travel and less defined in the Constructor will pass that model static let's go ahead and Define get all method and we'll take options as a parameter which are the options for the sqlize and we'll check if option has property order and if it doesn't we're gonna use get default order by which we will Define later so now we're gonna return this model let's find all option with options and the next function will be get byte ID and it will take ID the string and again options and then it's going to return fine by PK method of the sqlise which will take ID and options and finally we will Define the get default order by so we'll return order created at descending all the models that we're going to be using get all method on we will be ordering by created add descending so let's go ahead and create code for tour repository we'll import tour from database models we will import the base repository and we're going to export default class to a repository it will extend base repository we'll have a Constructor we will pass the tour static model in the Constructor of the parent class and that will complete the code for tour repository so tour repository will basically proxy methods get all and get by ID to the base repository now let's go ahead and create a code for travel repository this code will be a little more complex than the tool repositories code because travel repository will include tour repository so let's go ahead and import tour import travel and we can do base repository from base repository and we're going to export class travel repository which extends the base repository in a Constructor will do the same but we will pass travel model to the Constructor of the base repository and then get all method again with best option so the signature is going to be the same as the get all method of the parent class and now we will insert includes we're going to use the method these get includes and we'll Define it later again we just return a call to the parent class get all with options and get by ID that will take ID and options and again we will get includes and then return the call to the parent class get by ID with ID and options now let's define the private method get includes and it's going to return include tour so basically the same thing that we did with a travel model now we also need to update the code into a resource and travel resources you can see we're passing tour as a generic however we need to pass a tour attribute right so let's go ahead and remove the import of tour and pass the tour attributes in here and the same in a travel resource let's go ahead the same instead of travel we need to now pass the travel attribute let's go ahead and remove travel import and pass the travel attributes as a final step of the refactoring we need to update code in the controllers method so let's go into routes we want towards controller and right here where we have con store resource collection and we're calling sqlize model tour we need to put the following code we're going to be instantiating the Repository and we're going to be using the get all method on that repository so let's go ahead and import the total repository and then in a get tour method we're also going to make some changes instead of calling find by PK method on tour model we gotta instantiate the repository to our repository and we will call get by ID method on that Repository now let's go to travel throughout we'll choose the controller we'll find the least travels method instead of travels we're just gonna instantiate the travel Repository in a get travel function instead of a find by PK Global method on a travel model we're also going to be instantiating the repository and we will make a call to get by ID method of the travel Repository and this is how we can Implement repository pattern in our node.js API project I hope you like this tutorial thank you for watching and I will see you in the next video
Info
Channel: Alex Rusin
Views: 2,307
Rating: undefined out of 5
Keywords: Repository Pattern, JavaScript Design Patterns, Data Access in JavaScript, JavaScript Best Practices, Software Architecture, JavaScript Data Management, Code Organization, JavaScript Repository Example, JavaScript Data Repository, Separation of Concerns, JavaScript Project Structure, JavaScript Data Abstraction, Code Maintainability, JavaScript Development Patterns, Object-Oriented JavaScript, JavaScript Design Patterns Tutorial, JavaScript Code Structure, Alex Rusin
Id: 8pVIYY5-_PU
Channel Id: undefined
Length: 10min 21sec (621 seconds)
Published: Sun Sep 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.