|
| 1 | +# Injecting Database Drivers |
| 2 | +Keeping in mind the size of the framework in the final build, it felt counter-productive to keep all the database drivers within |
| 3 | +the framewrork itself. Keeping only the most used MySQL and Redis within the framework, users can now inject databases |
| 4 | +in the server that staisfies the base interface defined by GoFr. This helps in reducing the build size and in turn build time |
| 5 | +as unnecessary database drivers are not being compiled and added to the build. |
| 6 | + |
| 7 | +> We are planning to provide custom drivers for most common databases, and is in the pipeline for upcoming releases! |
| 8 | +
|
| 9 | +## Mongo DB |
| 10 | +Gofr supports injecting Mongo DB that supports the following interface. Any driver that implements the interface can be added |
| 11 | +using `app.UseMongo()` method, and user's can use MonogDB across application with `gofr.Context`. |
| 12 | +```go |
| 13 | +type Mongo interface { |
| 14 | + Find(ctx context.Context, collection string, filter interface{}, results interface{}) error |
| 15 | + |
| 16 | + FindOne(ctx context.Context, collection string, filter interface{}, result interface{}) error |
| 17 | + |
| 18 | + InsertOne(ctx context.Context, collection string, document interface{}) (interface{}, error) |
| 19 | + |
| 20 | + InsertMany(ctx context.Context, collection string, documents []interface{}) ([]interface{}, error) |
| 21 | + |
| 22 | + DeleteOne(ctx context.Context, collection string, filter interface{}) (int64, error) |
| 23 | + |
| 24 | + DeleteMany(ctx context.Context, collection string, filter interface{}) (int64, error) |
| 25 | + |
| 26 | + UpdateByID(ctx context.Context, collection string, id interface{}, update interface{}) (int64, error) |
| 27 | + |
| 28 | + UpdateOne(ctx context.Context, collection string, filter interface{}, update interface{}) error |
| 29 | + |
| 30 | + UpdateMany(ctx context.Context, collection string, filter interface{}, update interface{}) (int64, error) |
| 31 | + |
| 32 | + CountDocuments(ctx context.Context, collection string, filter interface{}) (int64, error) |
| 33 | + |
| 34 | + Drop(ctx context.Context, collection string) error |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +User's can easily inject a driver that supports this interface, this provides usability without |
| 39 | +compromising the extensability to use multiple databases. |
| 40 | +### Example |
| 41 | +```go |
| 42 | +package main |
| 43 | + |
| 44 | +import ( |
| 45 | + mongo "github.com/vipul-rawat/gofr-mongo" |
| 46 | + "go.mongodb.org/mongo-driver/bson" |
| 47 | + |
| 48 | + "gofr.dev/pkg/gofr" |
| 49 | +) |
| 50 | + |
| 51 | +type Person struct { |
| 52 | + Name string `bson:"name" json:"name"` |
| 53 | + Age int `bson:"age" json:"age"` |
| 54 | + City string `bson:"city" json:"city"` |
| 55 | +} |
| 56 | + |
| 57 | +func main() { |
| 58 | + app := gofr.New() |
| 59 | + |
| 60 | + // using the mongo driver from `vipul-rawat/gofr-mongo` |
| 61 | + db := mongo.New(app.Config, app.Logger(), app.Metrics()) |
| 62 | + |
| 63 | + // inject the mongo into gofr to use mongoDB across the application |
| 64 | + // using gofr context |
| 65 | + app.UseMongo(db) |
| 66 | + |
| 67 | + app.POST("/mongo", Insert) |
| 68 | + app.GET("/mongo", Get) |
| 69 | + |
| 70 | + app.Run() |
| 71 | +} |
| 72 | + |
| 73 | +func Insert(ctx *gofr.Context) (interface{}, error) { |
| 74 | + var p Person |
| 75 | + err := ctx.Bind(&p) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + res, err := ctx.Mongo.InsertOne(ctx, "collection", p) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + return res, nil |
| 86 | +} |
| 87 | + |
| 88 | +func Get(ctx *gofr.Context) (interface{}, error) { |
| 89 | + var result Person |
| 90 | + |
| 91 | + p := ctx.Param("name") |
| 92 | + |
| 93 | + err := ctx.Mongo.FindOne(ctx, "collection", bson.D{{"name", p}} /* valid filter */, &result) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + return result, nil |
| 99 | +} |
| 100 | +``` |
0 commit comments