Skip to content

Commit 1f707ed

Browse files
author
Jayesh Choudhary
authored
Merge pull request #616 from spaceuptech/v0.15.3
V0.15.3
2 parents 25eb271 + 86a06da commit 1f707ed

33 files changed

+317
-145
lines changed

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ require (
4444
github.com/rs/cors v1.6.0
4545
github.com/satori/go.uuid v1.2.0
4646
github.com/segmentio/ksuid v1.0.2
47+
github.com/sirupsen/logrus v1.4.2
4748
github.com/soheilhy/cmux v0.1.4 // indirect
4849
github.com/stretchr/objx v0.2.0 // indirect
4950
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51 // indirect

Diff for: mission-control

Diff for: model/crud.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ReadRequest struct {
1616
// ReadOptions is the options required for a read request
1717
type ReadOptions struct {
1818
Select map[string]int32 `json:"select"`
19-
Sort map[string]int32 `json:"sort"`
19+
Sort []string `json:"sort"`
2020
Skip *int64 `json:"skip"`
2121
Limit *int64 `json:"limit"`
2222
Distinct *string `json:"distinct"`

Diff for: model/eventing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ type EventIntent struct {
5555
type DatabaseEventMessage struct {
5656
DBType string `json:"db" mapstructure:"db"`
5757
Col string `json:"col" mapstructure:"col"`
58-
DocID string `json:"docId" mapstructure:"docId"`
5958
Doc interface{} `json:"doc" mapstructure:"doc"`
59+
Find interface{} `json:"find" mapstructure:"find"`
6060
}

Diff for: model/realtime.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package model
33
// FeedData is the format to send realtime data
44
type FeedData struct {
55
QueryID string `json:"id" structs:"id"`
6-
DocID string `json:"docId" structs:"docId"`
76
Type string `json:"type" structs:"type"`
87
Payload interface{} `json:"payload" structs:"payload"`
98
TimeStamp int64 `json:"time" structs:"time"`
109
Group string `json:"group" structs:"group"`
1110
DBType string `json:"dbType" structs:"dbType"`
1211
TypeName string `json:"__typename,omitempty" structs:"__typename,omitempty"`
12+
Find interface{} `json:"find" structs:"find"`
1313
}
1414

1515
// RealtimeRequest is the object sent for realtime requests

Diff for: modules/auth/handle_crud.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (m *Module) IsUpdateOpAuthorised(ctx context.Context, project, dbType, col,
8383
return http.StatusForbidden, err
8484
}
8585

86-
if err := m.schema.ValidateUpdateOperation(dbType, col, req.Update); err != nil {
86+
if err := m.schema.ValidateUpdateOperation(dbType, col, req.Operation, req.Update, req.Find); err != nil {
8787
return http.StatusBadRequest, err
8888
}
8989

Diff for: modules/crud/mgo/read.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package mgo
22

33
import (
44
"context"
5+
"strings"
56

7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/bson/primitive"
69
"go.mongodb.org/mongo-driver/mongo/options"
710

811
"github.com/spaceuptech/space-cloud/model"
@@ -35,7 +38,15 @@ func (m *Mongo) Read(ctx context.Context, project, col string, req *model.ReadRe
3538
return 0, nil, err
3639
}
3740

38-
return int64(len(result)), result, nil
41+
// convert result []string to []map[string]interface
42+
finalResult := []interface{}{}
43+
for _, value := range result {
44+
doc := map[string]interface{}{}
45+
doc[*distinct] = value
46+
finalResult = append(finalResult, doc)
47+
}
48+
49+
return int64(len(result)), finalResult, nil
3950

4051
case utils.All:
4152
findOptions := options.Find()
@@ -54,7 +65,7 @@ func (m *Mongo) Read(ctx context.Context, project, col string, req *model.ReadRe
5465
}
5566

5667
if req.Options.Sort != nil {
57-
findOptions = findOptions.SetSort(req.Options.Sort)
68+
findOptions = findOptions.SetSort(generateSortOptions(req.Options.Sort))
5869
}
5970
}
6071

@@ -101,7 +112,7 @@ func (m *Mongo) Read(ctx context.Context, project, col string, req *model.ReadRe
101112
}
102113

103114
if req.Options.Sort != nil {
104-
findOneOptions = findOneOptions.SetSort(req.Options.Sort)
115+
findOneOptions = findOneOptions.SetSort(generateSortOptions(req.Options.Sort))
105116
}
106117
}
107118

@@ -117,3 +128,16 @@ func (m *Mongo) Read(ctx context.Context, project, col string, req *model.ReadRe
117128
return 0, nil, utils.ErrInvalidParams
118129
}
119130
}
131+
132+
func generateSortOptions(array []string) bson.D {
133+
sort := bson.D{}
134+
for _, value := range array {
135+
if strings.HasPrefix(value, "-") {
136+
sort = append(sort, primitive.E{Key: strings.TrimPrefix(value, "-"), Value: -1})
137+
} else {
138+
sort = append(sort, primitive.E{Key: value, Value: 1})
139+
}
140+
}
141+
142+
return sort
143+
}

Diff for: modules/crud/sql/collections.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (s *SQL) GetCollections(ctx context.Context, project string) ([]utils.Datab
2828
if err != nil {
2929
return nil, err
3030
}
31-
defer rows.Close()
31+
defer func() { _ = rows.Close() }()
3232

3333
result := make([]utils.DatabaseCollections, 0)
3434
for rows.Next() {

Diff for: modules/crud/sql/delete.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (s *SQL) generateDeleteQuery(ctx context.Context, project, col string, req
6363
return sqlString, args, nil
6464
}
6565

66+
// DeleteCollection drops a table
6667
func (s *SQL) DeleteCollection(ctx context.Context, project, col string) error {
6768
query := "DROP TABLE " + project + "." + col
6869
_, err := s.client.ExecContext(ctx, query, []interface{}{}...)

Diff for: modules/crud/sql/describe.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ WHERE C.TABLE_SCHEMA=@p2 AND C.table_name = @p1`
7777
if err != nil {
7878
return nil, err
7979
}
80-
defer rows.Close()
80+
defer func() { _ = rows.Close() }()
8181

8282
result := []utils.FieldType{}
8383
count := 0
@@ -135,7 +135,7 @@ func (s *SQL) getForeignKeyDetails(ctx context.Context, project, col string) ([]
135135
if err != nil {
136136
return nil, err
137137
}
138-
defer rows.Close()
138+
defer func() { _ = rows.Close() }()
139139

140140
result := []utils.ForeignKeysType{}
141141
for rows.Next() {
@@ -207,7 +207,7 @@ func (s *SQL) getIndexDetails(ctx context.Context, project, col string) ([]utils
207207
if err != nil {
208208
return nil, err
209209
}
210-
defer rows.Close()
210+
defer func() { _ = rows.Close() }()
211211

212212
result := []utils.IndexType{}
213213
for rows.Next() {

Diff for: modules/crud/sql/raw.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ func (s *SQL) RawBatch(ctx context.Context, queries []string) error {
2323
for _, query := range queries {
2424
_, err := tx.ExecContext(ctx, query)
2525
if err != nil {
26-
tx.Rollback()
26+
_ = tx.Rollback()
2727
return err
2828
}
2929
}
3030
if err := tx.Commit(); err != nil {
31-
tx.Rollback()
31+
_ = tx.Rollback()
3232
return err
3333
}
3434

@@ -53,6 +53,7 @@ func (s *SQL) GetConnectionState(ctx context.Context) bool {
5353
return err == nil
5454
}
5555

56+
// CreateProjectIfNotExist creates a schema / database
5657
func (s *SQL) CreateProjectIfNotExist(ctx context.Context, project string) error {
5758
var sql string
5859
switch utils.DBType(s.dbType) {

Diff for: modules/crud/sql/read.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,20 @@ func (s *SQL) generateReadQuery(ctx context.Context, project, col string, req *m
5757

5858
if req.Options.Sort != nil {
5959
// Format the order array to a suitable type
60-
orderMap := req.Options.Sort
61-
62-
orderBys := []exp.OrderedExpression{}
60+
orderBys := make([]exp.OrderedExpression, len(req.Options.Sort))
6361

6462
// Iterate over order array
65-
for k, value := range orderMap {
63+
for i, value := range req.Options.Sort {
6664
// Add order type based on type attribute of order element
67-
var exp exp.OrderedExpression
68-
if value < 0 {
69-
exp = goqu.I(k).Desc()
65+
var e exp.OrderedExpression
66+
if strings.HasPrefix(value, "-") {
67+
e = goqu.I(strings.TrimPrefix(value, "-")).Desc()
7068
} else {
71-
exp = goqu.I(k).Asc()
69+
e = goqu.I(value).Asc()
7270
}
7371

7472
// Append the order expression to the order expression array
75-
orderBys = append(orderBys, exp)
73+
orderBys[i] = e
7674
}
7775
query = query.Order(orderBys...)
7876
}
@@ -131,13 +129,13 @@ func (s *SQL) read(ctx context.Context, project, col string, req *model.ReadRequ
131129
if err != nil {
132130
return 0, nil, err
133131
}
134-
defer stmt.Close()
132+
defer func() { _ = stmt.Close() }()
135133

136134
rows, err := stmt.QueryxContext(ctx, args...)
137135
if err != nil {
138136
return 0, nil, err
139137
}
140-
defer rows.Close()
138+
defer func() { _ = rows.Close() }()
141139

142140
var rowTypes []*sql.ColumnType
143141

Diff for: modules/crud/sql/sql.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (s *SQL) Close() error {
5959
return nil
6060
}
6161

62-
// GetDBAlias returns the dbType of the crud block
62+
// GetDBType returns the dbType of the crud block
6363
func (s *SQL) GetDBType() utils.DBType {
6464
switch s.dbType {
6565
case "postgres":
@@ -114,7 +114,7 @@ func doExecContext(ctx context.Context, query string, args []interface{}, execut
114114
if err != nil {
115115
return nil, err
116116
}
117-
defer stmt.Close()
117+
defer func() { _ = stmt.Close() }()
118118

119119
return stmt.ExecContext(ctx, args...)
120120
}

Diff for: modules/crud/sql/update.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ func (s *SQL) update(ctx context.Context, project, col string, req *model.Update
114114
}
115115

116116
return res.RowsAffected()
117-
} else {
118-
req.Operation = utils.All
119-
return s.update(ctx, project, col, req, executor)
120117
}
118+
req.Operation = utils.All
119+
return s.update(ctx, project, col, req, executor)
121120
default: // (case utils.One)
122121
return 0, utils.ErrInvalidParams
123122
}

Diff for: modules/eventing/crud.go

+33-45
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (m *Module) HookDBUpdateIntent(ctx context.Context, dbType, col string, req
112112
return &model.EventIntent{Invalid: true}, nil
113113
}
114114

115-
return m.HookDBUpdateDeleteIntent(ctx, utils.EventDBUpdate, dbType, col, req.Find)
115+
return m.hookDBUpdateDeleteIntent(ctx, utils.EventDBUpdate, dbType, col, req.Find)
116116
}
117117

118118
// HookDBDeleteIntent handles the delete intent requests
@@ -125,11 +125,11 @@ func (m *Module) HookDBDeleteIntent(ctx context.Context, dbType, col string, req
125125
return &model.EventIntent{Invalid: true}, nil
126126
}
127127

128-
return m.HookDBUpdateDeleteIntent(ctx, utils.EventDBDelete, dbType, col, req.Find)
128+
return m.hookDBUpdateDeleteIntent(ctx, utils.EventDBDelete, dbType, col, req.Find)
129129
}
130130

131-
// HookDBUpdateDeleteIntent is used as the hook for update and delete events
132-
func (m *Module) HookDBUpdateDeleteIntent(ctx context.Context, eventType, dbType, col string, find map[string]interface{}) (*model.EventIntent, error) {
131+
// hookDBUpdateDeleteIntent is used as the hook for update and delete events
132+
func (m *Module) hookDBUpdateDeleteIntent(ctx context.Context, eventType, dbType, col string, find map[string]interface{}) (*model.EventIntent, error) {
133133
// Create a unique batch id and token
134134
batchID := ksuid.New().String()
135135
token := rand.Intn(utils.MaxEventTokens)
@@ -190,13 +190,9 @@ func (m *Module) HookStage(ctx context.Context, intent *model.EventIntent, err e
190190
log.Println("Eventing Staging Error:", err)
191191
continue
192192
}
193-
dbType, err := m.crud.GetDBType(dbEvent.DBType)
194-
if err != nil {
195-
return
196-
}
197193

198194
req := &model.ReadRequest{
199-
Find: map[string]interface{}{utils.GetIDVariable(dbType): dbEvent.DocID},
195+
Find: dbEvent.Find.(map[string]interface{}),
200196
Operation: utils.One,
201197
}
202198

@@ -237,33 +233,27 @@ func (m *Module) HookStage(ctx context.Context, intent *model.EventIntent, err e
237233

238234
func (m *Module) processCreateDocs(token int, batchID, dbAlias, col string, rows []interface{}) []*model.EventDocument {
239235
// Get event listeners
240-
actualDbType, err := m.crud.GetDBType(dbAlias)
241-
if err != nil {
236+
rules := m.getMatchingRules(utils.EventDBCreate, map[string]string{"col": col, "db": dbAlias})
237+
238+
// Return if length of rules is zero
239+
if len(rules) == 0 {
242240
return nil
243241
}
244242

245-
rules := m.getMatchingRules(utils.EventDBCreate, map[string]string{"col": col, "db": dbAlias})
246-
247243
eventDocs := make([]*model.EventDocument, 0)
248244
for _, doc := range rows {
249245

250-
// Skip the doc if id isn't present
251-
idTemp, p := doc.(map[string]interface{})[utils.GetIDVariable(actualDbType)]
252-
if !p {
253-
continue
246+
findForCreate, possible := m.schema.CheckIfEventingIsPossible(dbAlias, col, doc.(map[string]interface{}), false)
247+
if !possible {
248+
return nil
254249
}
255250

256-
// Skip the doc if id isn't of type string
257-
docID, ok := idTemp.(string)
258-
if !ok {
259-
continue
260-
}
261251
// Iterate over all rules
262252
for _, rule := range rules {
263253
eventDocs = append(eventDocs, m.generateQueueEventRequest(token, rule.Retries,
264254
batchID, utils.EventStatusIntent, rule.Url, &model.QueueEventRequest{
265255
Type: utils.EventDBCreate,
266-
Payload: model.DatabaseEventMessage{DBType: dbAlias, Col: col, Doc: doc, DocID: docID},
256+
Payload: model.DatabaseEventMessage{DBType: dbAlias, Col: col, Doc: doc, Find: findForCreate},
267257
}))
268258
}
269259
}
@@ -274,34 +264,32 @@ func (m *Module) processCreateDocs(token int, batchID, dbAlias, col string, rows
274264
func (m *Module) processUpdateDeleteHook(token int, eventType, batchID, dbType, col string, find map[string]interface{}) ([]*model.EventDocument, bool) {
275265
// Get event listeners
276266
rules := m.getMatchingRules(eventType, map[string]string{"col": col, "db": dbType})
277-
actualDBType, err := m.crud.GetDBType(dbType)
278-
if err != nil {
267+
268+
// Return if length of rules is zero
269+
if len(rules) == 0 {
279270
return nil, false
280271
}
281272

282-
// Check if id field is valid
283-
if idTemp, p := find[utils.GetIDVariable(actualDBType)]; p {
284-
if id, ok := utils.AcceptableIDType(idTemp); ok {
273+
findForUpdate, possible := m.schema.CheckIfEventingIsPossible(dbType, col, find, true)
274+
if !possible {
275+
return nil, false
276+
}
285277

286-
eventDocs := make([]*model.EventDocument, len(rules))
278+
eventDocs := make([]*model.EventDocument, len(rules))
287279

288-
for i, rule := range rules {
289-
// Create an event doc
290-
eventDocs[i] = m.generateQueueEventRequest(token, rule.Retries,
291-
batchID, utils.EventStatusIntent, rule.Url, &model.QueueEventRequest{
292-
Type: eventType,
293-
Payload: model.DatabaseEventMessage{DBType: dbType, Col: col, DocID: id},
294-
})
295-
}
296-
297-
// Mark event as invalid if no events are generated
298-
if len(eventDocs) == 0 {
299-
return nil, false
300-
}
280+
for i, rule := range rules {
281+
// Create an event doc
282+
eventDocs[i] = m.generateQueueEventRequest(token, rule.Retries,
283+
batchID, utils.EventStatusIntent, rule.Url, &model.QueueEventRequest{
284+
Type: eventType,
285+
Payload: model.DatabaseEventMessage{DBType: dbType, Col: col, Find: findForUpdate}, // The doc here contains the where clause
286+
})
287+
}
301288

302-
return eventDocs, true
303-
}
289+
// Mark event as invalid if no events are generated
290+
if len(eventDocs) == 0 {
291+
return nil, false
304292
}
305293

306-
return nil, false
294+
return eventDocs, true
307295
}

0 commit comments

Comments
 (0)