vardius / go-api-boilerplate

Go Server/API boilerplate using best practices DDD CQRS ES gRPC
https://go-api-boilerplate.local
MIT License
917 stars 135 forks source link

Other database providers #68

Closed rizzla22 closed 3 years ago

rizzla22 commented 3 years ago

Id love to use this with mongodb it would be great if there was an example or some db abstraction to make it easy to plug in another type

vardius commented 3 years ago

current architecture is flexible enough and allows you to simply swap persistence layer. to do so, all you need to do is to add another implementation of an interface

for example lets say you chose to store user current state for view models as you say using mongodb all you have to do is add/replace current mysql implementation here https://github.com/vardius/go-api-boilerplate/tree/master/cmd/user/internal/infrastructure/persistence with your own one, just make sure that you implement interface https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/internal/infrastructure/persistence/user.go#L19 then all you would have to change to start using new implementation would be an import path

-   persistence "github.com/vardius/go-api-boilerplate/cmd/user/internal/infrastructure/persistence/mysql"
+   persistence "github.com/vardius/go-api-boilerplate/cmd/user/internal/infrastructure/persistence/mongodb"

so it has an effect here https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/main.go#L105

If you wish I could do mongodb persistence layer as an alternative example, but you would have to w8 about 2 weeks till 10th-11th of October, when I would have some time to do it. Or maybe you would like to contribute and implement it yourself, I strongly encourage to contribution!

rizzla22 commented 3 years ago

Awesome, ill give it a go. I have a question, how do you start this project in development mode ? must it be run within docker containers ? is it possible to just hit some go file with go run ?

vardius commented 3 years ago

you can absolutely run it like this:

go run -race cmd/user/main.go

https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/internal/application/config/env.go it will use default values from env unless you override or set some in your system, you can do it like this:

MYSQL_HOST=mysql.go-api-boilerplate.local go run -race cmd/user/main.go
vardius commented 3 years ago

I have introduced memory service container as an example of how different persistence models could be handled. Please have a look on this example for details:

https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/internal/application/services/services.go#L22

and appropriate service containers implementation. Give solution allows for easy switch between different persistence models with just recompiling the service with appropriate build tag.

https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/internal/application/services/mysql.go#L1 https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/internal/application/services/memory.go#L1

Following this patter another persistence model can be added and used instead without knowledge or need to update the whole business logic of the infrastructure.

Usage: https://github.com/vardius/go-api-boilerplate/blob/master/cmd/user/Dockerfile#L35

Similar patter was implemented for auth service.