-
Notifications
You must be signed in to change notification settings - Fork 9
/
db.go
67 lines (57 loc) · 1.45 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package dynamodb
import (
"context"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// NewDB creates new database connection.
func NewDB(endpoint string, region string) (*DB, error) {
config := &aws.Config{
Region: aws.String(region),
Endpoint: aws.String(endpoint),
}
sess, err := session.NewSession(config)
if err != nil {
log.Println(err)
return nil, err
}
return &DB{C: dynamodb.New(sess)}, nil
}
// DB represents connection to AWS DynamoDB service or local instance.
type DB struct {
C *dynamodb.DynamoDB
}
// IsTableExists checks if table exists.
func (db *DB) IsTableExists(table string) (bool, error) {
input := &dynamodb.DescribeTableInput{
TableName: aws.String(table),
}
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := db.C.DescribeTableWithContext(timeoutCtx, input)
if AwsErrorErrorNotFound(err) {
return false, nil
//if table not exists - create table
}
if err != nil {
log.Println(err)
return false, err
}
return true, nil
}
// AwsErrorErrorNotFound checks if error has type dynamodb.ErrCodeResourceNotFoundException.
func AwsErrorErrorNotFound(err error) bool {
if err == nil {
return false
}
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() == dynamodb.ErrCodeResourceNotFoundException {
return true
}
}
return false
}