/
crud.go
132 lines (112 loc) · 4.54 KB
/
crud.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package model
import "github.com/spaceuptech/space-cloud/gateway/config"
// CreateRequest is the http body received for a create request
type CreateRequest struct {
Document interface{} `json:"doc"`
Operation string `json:"op"`
IsBatch bool `json:"isBatch"`
}
// ReadRequest is the http body received for a read request
type ReadRequest struct {
GroupBy []interface{} `json:"group"`
Aggregate map[string][]string `json:"aggregate"`
Find map[string]interface{} `json:"find"`
Operation string `json:"op"`
Options *ReadOptions `json:"options"`
IsBatch bool `json:"isBatch"`
Extras map[string]interface{} `json:"extras"`
PostProcess map[string]*PostProcess `json:"postProcess"`
MatchWhere []map[string]interface{} `json:"matchWhere"`
Cache *config.ReadCacheOptions `json:"cache"`
}
// ReadOptions is the options required for a read request
type ReadOptions struct {
// Debug field is used internally to show
// _query meta data in the graphql
Debug bool `json:"debug"`
Select map[string]int32 `json:"select"`
Sort []string `json:"sort"`
Skip *int64 `json:"skip"`
Limit *int64 `json:"limit"`
Distinct *string `json:"distinct"`
Join []*JoinOption `json:"join"`
ReturnType string `json:"returnType"`
HasOptions bool `json:"hasOptions"` // used internally
}
// JoinOption describes the way a join needs to be performed
type JoinOption struct {
// Op can be either All or One
// This field decides the way the result of join is returned
// If op is all, the result is returned as an array
// If op is one, the result is returned as an object
Op string `json:"Op" mapstructure:"Op"`
Type string `json:"type" mapstructure:"type"`
Table string `json:"table" mapstructure:"table"`
As string `json:"as" mapstructure:"as"`
On map[string]interface{} `json:"on" mapstructure:"on"`
Join []*JoinOption `json:"join" mapstructure:"join"`
}
// UpdateRequest is the http body received for an update request
type UpdateRequest struct {
Find map[string]interface{} `json:"find"`
Operation string `json:"op"`
Update map[string]interface{} `json:"update"`
}
// DeleteRequest is the http body received for a delete request
type DeleteRequest struct {
Find map[string]interface{} `json:"find"`
Operation string `json:"op"`
}
// PreparedQueryRequest is the http body received for a PreparedQuery request
type PreparedQueryRequest struct {
Params map[string]interface{} `json:"params"`
// This field is used internally to show
// _query meta data in the graphql
Debug bool
}
// AggregateRequest is the http body received for an aggregate request
type AggregateRequest struct {
Pipeline interface{} `json:"pipe"`
Operation string `json:"op"`
}
// AllRequest is a union of parameters required in the various requests
type AllRequest struct {
Col string `json:"col"`
Document interface{} `json:"doc"`
Operation string `json:"op"`
Find map[string]interface{} `json:"find"`
Update map[string]interface{} `json:"update"`
Type string `json:"type"`
DBAlias string `json:"dBAlias"`
Extras map[string]interface{} `json:"extras"`
}
// SQLMetaData stores sql query information
type SQLMetaData struct {
Col string `json:"col" structs:"col"`
SQL string `json:"sql" structs:"sql"`
DbAlias string `json:"db" structs:"db"`
Args []interface{} `json:"args" structs:"args"`
QueryTime string `json:"queryTime" structs:"queryTime"`
}
// BatchRequest is the http body for a batch request
type BatchRequest struct {
Requests []*AllRequest `json:"reqs"`
}
// DBType is the type of database used for a particular crud operation
type DBType string
const (
// Mongo is the type used for MongoDB
Mongo DBType = "mongo"
// EmbeddedDB is the type used for EmbeddedDB
EmbeddedDB DBType = "embedded"
// MySQL is the type used for MySQL
MySQL DBType = "mysql"
// Postgres is the type used for PostgresQL
Postgres DBType = "postgres"
// SQLServer is the type used for MsSQL
SQLServer DBType = "sqlserver"
// DefaultValidate is used for default validation operation
DefaultValidate = "default"
// DefaultFetchLimit is the default value to be used as a limit to fetch rows/collection in each read query
DefaultFetchLimit = 1000
)