Overview
In the past I had written some articles about how we can interact with non-relational databases, more specifically MongoDB. However, each of these articles was only geared towards the JavaScript and TypeScript community, but had never done a similar article for Golang.
For that same reason I decided to create a simple REST API using my favorite Go framework, Fiber and this time I decided to use the MongoDB Driver.
Let's code
In today's example we are going to install only two dependencies, which are as follows:
go get github.com/gofiber/fiber/v2
go get go.mongodb.org/mongo-driver/mongo
Now let's start by defining our Entity, which will have a total of four properties:
-
Name
- the name of the dog -
Age
- the age of our four legged friend -
Breed
- our friend's race -
IsGoodBoy
- whether our friend has been a good boy or not
Similar to the following:
// @/entities/dog.go
package entities
type IDogs struct {
ID string `json:"id,omitempty" bson:"_id,omitempty"`
Name string `json:"name" bson:"name"`
Breed string `json:"breed" bson:"breed"`
Age int `json:"age" bson:"age"`
IsGoodBoy bool `json:"isGoodBoy" bson:"isGoodBoy"`
}
Now we can proceed to the database configuration and here I will share my approach. First I like to define the struct of the list of collections that we will have in our database, in today's article we will only have one, which I'll name Dogs
.
// @/config/database.go
package config
type DatabaseCollections struct {
Dogs *mongo.Collection
}
// ...
Now we are going to create two important variables, one will help us to interact with our database collections to perform the necessary operations. The other will help us to deal with the database client, such as establishing a connection and disconnecting from it.
// @/config/database.go
package config
type DatabaseCollections struct {
Dogs *mongo.Collection
}
var Collections DatabaseCollections
var Client *mongo.Client
// ...
Now let's create the function that will be responsible for establishing the connection with the database, which I'll give the name of ConnectDatabase()
. In this same function I will also define the name of the database (which I nicknamed DEV) and define the dogs collection.
// @/config/database.go
package config
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DatabaseCollections struct {
Dogs *mongo.Collection
}
var Collections DatabaseCollections
var Client *mongo.Client
func ConnectDatabase() error {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://root:root@localhost:27017/"))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err = client.Connect(ctx)
db := client.Database("DEV")
dogsCollection := db.Collection("dogs")
if err != nil {
return err
}
Collections = DatabaseCollections{
Dogs: dogsCollection,
}
Client = client
return nil
}
Now that we have our Entity defined and the connection to our database configured, we can start working on our handlers. Each of our handlers will correspond to a route from our API, so each one will be responsible for executing only one operation. First let's get all the records we have in our database table.
// @/handlers/dog.go
package handlers
import (
"github.com/FranciscoMendes10866/fiber-mgo/config"
"github.com/FranciscoMendes10866/fiber-mgo/entities"
"github.com/gofiber/fiber/v2"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/bson"
)
func GetDogs(c *fiber.Ctx) error {
query := bson.D{}
data, err := config.Collections.Dogs.Find(c.Context(), query)
if err != nil {
return c.Status(500).SendString(err.Error())
}
var allDogs []entities.IDogs = make([]entities.IDogs, 0)
if err := data.All(c.Context(), &allDogs); err != nil {
return c.Status(500).JSON(err)
}
return c.Status(200).JSON(&allDogs)
}
// ...
Now let's get just one record according to the id
parameter that will be sent in the request parameters.
// @/handlers/dog.go
package handlers
// ...
func GetDog(c *fiber.Ctx) error {
dogId, err := primitive.ObjectIDFromHex(c.Params("id"))
if err != nil {
return c.Status(500).JSON(err)
}
var singleDog bson.M
query := bson.D{{"_id", dogId}}
if err := config.Collections.Dogs.FindOne(c.Context(), query).Decode(&singleDog); err != nil {
return c.Status(500).JSON(err)
}
return c.Status(200).JSON(singleDog)
}
// ...
Now that we can get all the records and also just one record. We lack the functionality to insert a new one in the database table.
// @/handlers/dog.go
package handlers
// ...
func AddDog(c *fiber.Ctx) error {
dog := new(entities.IDogs)
c.BodyParser(dog)
response, err := config.Collections.Dogs.InsertOne(c.Context(), dog)
if err != nil {
return c.Status(500).JSON(err)
}
var insertedDog bson.M
query := bson.D{{"_id", response.InsertedID}}
if err := config.Collections.Dogs.FindOne(c.Context(), query).Decode(&insertedDog); err != nil {
return c.Status(500).JSON(err)
}
return c.Status(200).JSON(insertedDog)
}
// ...
We still need to add the functionality to update an existing record in our database. And similar to what we've already implemented, let's use the id
parameter to update the specific record.
// @/handlers/dog.go
package handlers
// ...
func UpdateDog(c *fiber.Ctx) error {
dogId, err := primitive.ObjectIDFromHex(
c.Params("id"),
)
if err != nil {
return c.Status(500).JSON(err)
}
dog := new(entities.IDogs)
if err := c.BodyParser(dog); err != nil {
return c.Status(500).JSON(err)
}
query := bson.D{{"_id", dogId}}
body := bson.D{
{Key: "$set",
Value: bson.D{
{"name", dog.Name},
{"breed", dog.Breed},
{"age", dog.Age},
{"isGoodBoy", dog.IsGoodBoy},
},
},
}
if _, err := config.Collections.Dogs.UpdateOne(c.Context(), &query, &body); err != nil {
return c.Status(500).JSON(err)
}
var updatedDog bson.M
if err := config.Collections.Dogs.FindOne(c.Context(), &query).Decode(&updatedDog); err != nil {
return c.Status(500).JSON(err)
}
return c.Status(200).JSON(updatedDog)
}
// ...
Last but not least, we need to delete a specific record, again we will use the id
parameter to remove the specific record from our database.
// @/handlers/dog.go
package handlers
// ...
func RemoveDog(c *fiber.Ctx) error {
dogId, err := primitive.ObjectIDFromHex(
c.Params("id"),
)
if err != nil {
return c.Status(500).JSON(err)
}
query := bson.D{{"_id", dogId}}
var dog bson.M
if err := config.Collections.Dogs.FindOneAndDelete(c.Context(), &query).Decode(&dog); err != nil {
return c.Status(500).JSON(err)
}
return c.Status(200).JSON(dog)
}
As you may have noticed in the add and update handlers, after completing these actions, we will fetch the inserted/updated document from the database (if you don't need it you can always remove it).
Now we just need to create our main file that will be responsible for initializing the connection to the database and where our API routes will be defined and a handler will be associated to each one of them.
// @/main.go
package main
import (
"context"
"github.com/FranciscoMendes10866/fiber-mgo/config"
"github.com/FranciscoMendes10866/fiber-mgo/handlers"
"github.com/gofiber/fiber/v2"
)
type Person struct {
Name string
Phone string
}
func main() {
app := fiber.New()
if err := config.ConnectDatabase(); err != nil {
panic(err)
}
defer config.Client.Disconnect(context.Background())
app.Get("/dogs", handlers.GetDogs)
app.Get("/dogs/:id", handlers.GetDog)
app.Post("/dogs", handlers.AddDog)
app.Put("/dogs/:id", handlers.UpdateDog)
app.Delete("/dogs/:id", handlers.RemoveDog)
app.Listen(":3000")
}
Conclusion
As always, I hope you found it interesting. If you noticed any errors in this article, please mention them in the comments. 🍭
Hope you have a great day! 🍬
Top comments (1)
Is there need to setup a database DEV before run GO?