# README
backends
A package that supports multiple backends( MongoDB, DynamoDB )
Use in Goa
In the main.go
:
Define the supported backends:
backendManager := backends.NewBackendSupport(map[string]*config.DBInfo{
"mongodb": &dbConf.DBInfo,
"dynamodb": &dbConf.DBInfo,
})
Get the desire backend(mongoDB or dynamoDB):
backend, err := backendManager.GetBackend(dbConf.DBName)
if err != nil {
service.LogError("Failed to configure backend. ", err)
}
Define the repositories(collections/tables):
userRepo, err := backend.DefineRepository("users", backends.RepositoryDefinitionMap{
"name": "users",
"indexes": []string{"email"},
"hashKey": "email",
"readCapacity": int64(5),
"writeCapacity": int64(5),
"GSI": map[string]interface{}{
"email": map[string]interface{}{
"readCapacity": 1,
"writeCapacity": 1,
},
},
})
if err != nil {
service.LogError("Failed to get users repo.", err)
return
}
tokenRepo, err := backend.DefineRepository("tokens", backends.RepositoryDefinitionMap{
"name": "tokens",
"indexes": []string{"token"},
"hashKey": "token",
"readCapacity": int64(5),
"writeCapacity": int64(5),
"GSI": map[string]interface{}{
"token": map[string]interface{}{
"readCapacity": 1,
"writeCapacity": 1,
},
},
"enableTtl": true,
"ttlAttribute": "created_at",
"ttl": 86400,
})
if err != nil {
service.LogError("Failed to get tokens repo.", err)
return
}
- name - is the name of the collection/table
- indexes - are the mongoDB indexs
- hashKey - is the primary key (hash key) for dynamoDB table
- rangeKey - is the sort key (range key) for dynamoDB table
- readCapacity - is the read capacity of the table. 1 unit is eqaul to 4KB
- writeCapacity - is the write capacity of the table. 1 unit is eqaul to 4KB
- GSI - are the global secondary indexes for dynamoDB
- enableTtl - set TTL
- ttlAttribute - is the TTL attribute in the collection/table
- ttl - is the TTL value in seconds
Then define the store and pass it to the controller:
// outside the main.go
// User wraps User's collections/tables. Implements backneds.Repository interface
type User struct {
Users backends.Repository
Tokens backends.Repository
}
...
// in the main.go
store := store.User{
userRepo,
tokenRepo,
}
// Mount "user" controller
c2 := NewUserController(service, store)
app.MountUserController(service, c2)
Service configuration
The service loads the configuration from a JSON. Here's an example of a JSON configuration file for DB settings:
{
"database":{
"dbName": "dynamodb",
"dbInfo": {
"credentials": "/run/secrets/aws-credentials",
"endpoint": "http://dynamo:8000",
"awsRegion": "us-east-1",
"host": "mongo:27017",
"database": "users",
"user": "restapi",
"pass": "restapi"
}
}
}
Configuration properties:
- dbName -
"dynamodb/mongodb"
- is the name of the database( it can be mongodb/dynamodb ). - dbInfo - holds informations about each database.
- credentials -
"/run/secrets/aws-credentials"
- is the full the to the AWS credentials file. - endpoint -
"http://dynamo:8000"
- is the dynamoDB endpoint. Format http://host:port - awsRegion -
us-east-1
- is the AWS region. - host -
mongo:27017
- mongoDB endpoint. Format host:port. - database -
users
- database name. Use only for mongoDB. - user - mongo database user
- pass - mongo database password
Contributing
For contributing to this repository or its documentation, see Contributing guidelines.
# Functions
AsPtr returns a pointer to the value passed as an argument to this function.
CreateNewAsExample creates a new value of the same type as the "example" passed to the function.
DynamoDBBackendBuilder returns RepositoriesBackend.
DynamoDBRepoBuilder builds new dynamo table.
ErrorClass defines a backend error class with the specified message.
InterfaceToMap converts interface type (struct or map pointer) to *map[string]interface{}.
IsConditionalCheckErr check if err is dynamoDB condition error.
IsErrAlreadyExists check of the error is of the ErrAlreadyExists class.
IsErrInvalidInput check of the error is of the ErrInvalidInput class.
IsErrNotFound check of the error is of the ErrNotFound class.
IsErrorOfType checks if the suplied err is of the same type (backend error class) as some backend error.
IterateOverSlice iterates over a slice viewed as generic itnerface{}.
MapToInterface decodes object to result.
MongoDBBackendBuilder returns RepositoriesBackend.
MongoDBRepoBuilder builds new mongo collection.
NewBackendManager returns new backend manager.
NewBackendSupport registers new backends.
NewFilter is a builder method to create new filter.
No description provided by the author
No description provided by the author
NewRepositoriesBackend sets new RepositoriesBackend.
NewSession returns a new Mongo Session.
NewSliceOfType creates new slice with len 0 and cap 0 with elements of the type passed as an example to the function.
No description provided by the author
PrepareDB ensure presence of persistent and immutable data in the DB.
# Variables
DYNAMO_CTX_KEY is dynamoDB context key.
ErrAlreadyExists is an error class that captures duplication errors.
ErrBackendError is a genering error class capturing errors that happened during processing in the backend.
ErrInvalidInput is a generic error class related to invalid input parameters specified on a backend function.
ErrNotFound is the error class for errors returned when the desired enityt is not found.
MONGO_CTX_KEY is mongoDB context key.
# Structs
BackendErrorInfo holds the info for an error that occurred in the backend.
DefaultBackendManager represents the backend store.
DynamoCollection wraps a dynamo.Table to embed methods in models.
MongoSession wraps a mgo.Session to embed methods in models.
RepositoriesBackend represents the repository store.
# Interfaces
Backend defines interface for defining the repository.
BackendManager defines interface for managing the backend.
No description provided by the author
Repository defines the interface for accessing the data.
RepositoryDefinition defines interface for accessing collection props.
# Type aliases
BackendBuilder builds the backend.
BackendCleanup is the collection/table clean up func.
BackendErrorFactory is a factory function for generating error objects.
Filter is a map property => value/pattern to match the DB entries against.
RepoBuilder builds the repo (collection or table).
RepositoryDefinitionMap is the configuration map.