-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoql.go
83 lines (66 loc) · 1.94 KB
/
goql.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
72
73
74
75
76
77
78
79
80
81
82
83
package goql
import (
"context"
"database/sql"
"errors"
"time"
redisv9 "github.com/redis/go-redis/v9"
"github.com/keremdokumaci/goql/constants"
"github.com/keremdokumaci/goql/internal/cache"
"github.com/keremdokumaci/goql/internal/cache/inmemory"
"github.com/keremdokumaci/goql/internal/cache/redis"
"github.com/keremdokumaci/goql/internal/cacher"
"github.com/keremdokumaci/goql/internal/whitelist"
whitelistrepository "github.com/keremdokumaci/goql/internal/whitelist/repository"
)
var (
ErrUnexpectedDBType error = errors.New("unexpected db type")
ErrDBConfigurationIsMandatory error = errors.New("db configuration is mandatory")
ErrCacheConfigurationIsMandatory error = errors.New("cache configuration is mandatory")
)
type WhiteLister interface {
QueryAllowed(ctx context.Context, queryName string) (bool, error)
}
type Cacher interface {
CacheQuery(query string, response any, ttl ...time.Duration) error
GetQueryCache(query string) any
}
type goQL struct {
cache cache.Cacher
db *sql.DB
dbName constants.DB
}
// New returns a goQL struct pointer.
func New() *goQL {
return &goQL{}
}
func (goql *goQL) ConfigureDB(dbName constants.DB, db *sql.DB) *goQL {
goql.db = db
goql.dbName = dbName
return goql
}
func (goql *goQL) ConfigureInmemoryCache() *goQL {
goql.cache = inmemory.New(nil)
return goql
}
func (goql *goQL) ConfigureRedisCache(client *redisv9.Client) *goQL {
goql.cache = redis.New(client)
return goql
}
func (goql *goQL) UseWhitelister() (WhiteLister, error) {
if goql.dbName == "" || goql.db == nil {
return nil, ErrDBConfigurationIsMandatory
}
var repo whitelistrepository.WhitelistRepository
repo, err := whitelistrepository.New(goql.dbName, goql.db)
if err != nil {
return nil, err
}
return whitelist.New(repo, goql.cache), nil
}
func (goql *goQL) UseGQLCacher() (Cacher, error) {
if goql.cache == nil {
return nil, ErrCacheConfigurationIsMandatory
}
return cacher.New(goql.cache), nil
}