First of all, if the project is applied a good pattern when we start the coding. It is very useful for maintaining, updating code and building new features. You can re-use the functions that you created. Also, if you define a good pattern at the beginning, you will avoid duplicated code and save a lot of time when you update or change functions.
In this article, you will learn a sample pattern. It is a simple project that I built. This project uses express js, a simple web application framework. You can find the instruction for installing at below link https://expressjs.com/en/starter/installing.html
The libraries that were used for the project:
This is the architecture that we will use for my project.
In this folder structure, we organize each module. By this way, I think it works out pretty well and finding the target that you want to code is easier. We will put the model, repository or request filter to each module like in the image below.
Now, we will talk about folder infrastructure, it will share all functions for each module.
First, the folder repositories contain all functions that connect to the database. The general functions query the records in each table of the database such as all models find a specific record, all records, create new item or update, delete an existing item.
The above image is the interface of GenericRepository class, you can find the code that executes on each function on my github https://github.com/luan4637/node-vue-blog
We move on to the module that will use the GenericRepository class. We will take the post module for example to extend the GenericRepository class. Also, there are categories and user module in the source code, they are similar together.
This is a post module that extends GenericRepository. It is placed on the core folder. Also, there are a few functions that use other logic that is different on the GenericRepository, they just use only this module.
In this module, I created a class PostFilter for filtering the query string on the request. They will find records that match with conditions on the table.
Also, you will see the function getFindOptions in this PostFilter class. It was implemented in the interface FilterInterface class. This function will be used in the GenericRepository class. So, this function getFindOptions must be required in all filter classes.
In every module, you will need an api for list all the records with pagination of a particular table. You will find the function paging on the GenericRepository class. It will return the class PaginationResult that has a specific key name of response json. It is a standard for frontend developers to use the response json.
You will see on each function of the controller, there is very small line code. Because, they were processed on each module. So, it is very helpful when you build a large project that is maintained easily.
You can find the source code in the article on my github at below link https://github.com/luan4637/node-vue-blog
© Copyright 2024. Email: luan4637@gmail.com
All rights reserved. No part of this publication can be reproduced without permission of the publisher.