-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·71 lines (57 loc) · 1.68 KB
/
main.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
68
69
70
71
package corm
import (
"context"
"github.com/flimzy/kivik"
)
// Config can be passed in corm.New(...)
type Config struct {
Host string // default "couch"
DriverName string // default "http://localhost:5984/"
DBName string
}
type ClientConfig struct {
Host string // default "couch"
DriverName string // default "http://localhost:5984/"
}
var defaultDriverName = "couch"
var defaultHostName = "http://localhost:5984/"
// New returns a db instance connection by passed DBName.
// When it fails it will return an error. Optionally "Host"
// and DriverName can be passed with Config struct.
//
// ctx := context.TODO()
// db, err := corm.New(ctx, Config{
// DBName: "honeyglass",
// })
func New(ctx context.Context, config Config) (*Orm, error) {
initDefaults(&config)
client, err := NewClient(ctx, ClientConfig{
Host: config.Host,
DriverName: config.DriverName,
})
db, err := client.DB(ctx, config.DBName)
if err != nil {
return nil, err
}
return NewOrm(db), err
}
// NewClient returns a client instance that can be used
// for e.g. CouchDb´ s build in authentication. Here are the full method list
// of the client instance: https://godoc.org/github.com/flimzy/kivik#Client
func NewClient(ctx context.Context, config ClientConfig) (*Client, error) {
client, err := kivik.New(ctx, config.DriverName, config.Host)
if err != nil {
return nil, err
}
return client, err
}
// InitDefaults sets the default values when
// Conifg struct does not contain the "Host" and "DriverName" member
func initDefaults(config *Config) {
if config.Host == "" {
config.Host = defaultHostName
}
if config.DriverName == "" {
config.DriverName = defaultDriverName
}
}