The custom datastore

A custom datastore has been implemented for the IGWEB demo website. Although we will be using Redis as the exclusive database for this book, the truth is, you are free to use just about any database your heart desires—as long as you create a custom datastore that implements the Datastore interface.

Let's examine the section where we've defined the Datastore interface in the datastore.go source file found in the common/datastore folder:

type Datastore interface {
CreateGopherTeam(team []*models.Gopher) error
GetGopherTeam() []*models.Gopher
CreateProduct(product *models.Product) error
CreateProductRegistry(products []string) error
GetProducts() []*models.Product
GetProductDetail(productTitle string) *models.Product
GetProductsInShoppingCart(cart *models.ShoppingCart) []*models.Product
CreateContactRequest(contactRrequest *models.ContactRequest) error
Close()
}

We will be going over the individual methods of the Datastore interface in their respective chapters that deal with the particular sections or features, where the method is used. Note that the final method required to implement the Datastore interface is the Close method (shown in bold). The Close method determines how the datastore closes its connection (or drains its connection pool).

Examining the RedisDatastore implementation in the redis.go source file, found in the common/datastore folder, will provide an idea of what goes into creating a custom datastore that implements the Datastore interface.

Moving further along in the datastore.go source file, we have defined the NewDatastore function, which is responsible for returning a new datastore:

const (
REDIS = iota
)

func NewDatastore(datastoreType int, dbConnectionString string) (Datastore, error) {

switch datastoreType {

case REDIS:
return NewRedisDatastore(dbConnectionString)

default:
return nil, errors.New("Unrecognized Datastore!")

}
}

Our datastore solution is flexible since we can swap out the Redis datastore with any other database, so long as our new custom datastore implements the Datastore interface. Notice that we have defined the REDIS constant in the constant grouping using the iota enumerator (shown in bold). Examine the NewDatastore function, and note that a new RedisDatastore instance is returned when the REDIS case is encountered (shown in bold), inside the switch block on the datastoreType.

If we want to add support for another database, such as MongoDB, we will simply add a new constant entry, MONGODB, to the constant grouping. In addition to that, we will add an additional case statement for the switch block in the NewDatastore function, for MongoDB, which returns a NewMongoDataStore instance, providing the connection string to the MongoDB instance as an input argument to the function. The NewMongoDBDatastore function will return an instance of our custom datastore type, MongoDBDataStore, which will implement the Datastore interface. 

A great benefit of implementing the custom datastore in this manner is that we can prevent littering our web application with database driver-specific calls for a particular database. With the custom datastore, our web application becomes agnostic to the database and provides us with greater flexibility in handling our data access and data storage needs.

The GopherFace web application, from the web programming with Go video series, implements a custom datastore for MySQL, MongoDB, and Redis. An example of the custom datastore using these databases is available at https://github.com/EngineerKamesh/gofullstack/tree/master/volume2/section5/gopherfacedb/common/datastore.
..................Content has been hidden....................

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