The database layer interface

Another major part of well-designed database layers is the database layer interface. In order to fully appreciate the need for a database interface layer, let's imagine a quick scenario. Let's say you build your backend to use database X, and all your code relies on direct calls to database X. Now, what happens if database X turns out to be very expensive and you find a much cheaper and more maintainable database that you can use in your code? Let's call the new database, database Y. You now have to revisit every single piece of your code that did queries to database X and change it, which may affect much more code than just your database layer. 

So, what do we do? We simply create an interface that defines all the behaviors that we expect from the database layer. All our code outside the database layer should only use methods provided by this interface and nothing else. Now, if we want to move from database X to database Y, we can simply write a new database layer that can communicate with database Y and still support our existing database layer interface. By doing this, we ensure that the vast majority of our existing code outside of the database layer will stay the same and behave as expected. 

Our next step is to write the database layer interface for our GoMusic application. To do so, we have to first identify the behaviors we seek from our database layer, as follows:

  • Get a list of all products
  • Get a list of all promotions
  • Get a customer by the customer's first and last name
  • Get a customer by the customer's id
  • Get a product by the product's id
  • Add a user to the database
  • Mark a user in the database as signed in
  • Mark a user in the database as signed out
  • Get a list of customer orders by the customer's id

Inside the backend/src folder, let's create a new folder called dblayer. Inside this folder, we'll create a new file called dblayer.go. This is where we'll write our database layer interface. We start our code by declaring the package name and importing the models package, as follows:

package dblayer

import (
"github.com/PacktPublishing/Hands-On-Full-Stack-Development-with-Go/Chapter06/backend/src/models"
)

Next, we write our interface, which encapsulates all the behavior points we covered in this section, as follows:

type DBLayer interface{
GetAllProducts() ([]models.Product, error)
GetPromos() ([]models.Product, error)
GetCustomerByName(string, string) (models.Customer, error)
GetCustomerByID(int) (models.Customer, error)
GetProduct(uint) (models.Product, error)
AddUser(models.Customer) (models.Customer, error)
SignInUser(username, password string) (models.Customer, error)
SignOutUserById(int) error
GetCustomerOrdersByID(int) ([]models.Order, error)
}

In the next chapter, we'll dive back into the database layer to continue its implementation. But for now, let's focus on our REST API layer and its implementation using the Gin framework.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156