From aa0bdac1ae336234083f0e7d89d62e621bc1e27a Mon Sep 17 00:00:00 2001 From: DeanRTaylor1 Date: Mon, 10 Apr 2023 16:23:53 +0700 Subject: [PATCH 01/10] refactored RunQuery function, first statement --- .../mongoqueryengine/queryengine.go | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pkg/queryengines/mongoqueryengine/queryengine.go b/pkg/queryengines/mongoqueryengine/queryengine.go index fb1e41e8..927d7592 100644 --- a/pkg/queryengines/mongoqueryengine/queryengine.go +++ b/pkg/queryengines/mongoqueryengine/queryengine.go @@ -29,6 +29,19 @@ func InitMongoQueryEngine() *MongoQueryEngine { } } +func (mqw *MongoQueryEngine) runFindOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result := db.Collection(queryType.CollectionName).FindOne(context.Background(), queryType.Args[0]) + if result.Err() != nil { + return nil, result.Err() + } + keys, data := mongoutils.MongoSingleResultToJson(result) + return map[string]interface{}{ + "keys": keys, + "data": data, + }, nil + +} + func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, config *models.QueryConfig) (map[string]interface{}, error) { port, _ := strconv.Atoi(string(dbConn.DBPort)) if dbConn.UseSSH != models.DBUSESSH_NONE { @@ -57,19 +70,13 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } if queryType.QueryType == mongoutils.QUERY_FINDONE { - result := db.Collection(queryType.CollectionName). - FindOne(context.Background(), queryType.Args[0]) - if result.Err() != nil { - return nil, result.Err() - } - keys, data := mongoutils.MongoSingleResultToJson(result) - if config.CreateLogFn != nil { - config.CreateLogFn(query) + result, err := mqe.runFindOneQuery(db, queryType) + if err != nil { + return nil, err } - return map[string]interface{}{ - "keys": keys, - "data": data, - }, nil + + return result, nil + } else if queryType.QueryType == mongoutils.QUERY_FIND { collection := db.Collection(queryType.CollectionName) opts := &options.FindOptions{Limit: queryType.Limit, Skip: queryType.Skip, Sort: queryType.Sort} From 72d93bf60bb23c1c4ffcadfbe8e93f1a166b6da4 Mon Sep 17 00:00:00 2001 From: DeanRTaylor1 Date: Mon, 10 Apr 2023 20:35:18 +0700 Subject: [PATCH 02/10] refactored queries into individual functions --- .../mongoqueryengine/queryengine.go | 530 ++++++++++++------ 1 file changed, 350 insertions(+), 180 deletions(-) diff --git a/pkg/queryengines/mongoqueryengine/queryengine.go b/pkg/queryengines/mongoqueryengine/queryengine.go index 927d7592..dabc7103 100644 --- a/pkg/queryengines/mongoqueryengine/queryengine.go +++ b/pkg/queryengines/mongoqueryengine/queryengine.go @@ -39,9 +39,295 @@ func (mqw *MongoQueryEngine) runFindOneQuery(db *mongo.Database, queryType *mong "keys": keys, "data": data, }, nil +} + +func (mqw *MongoQueryEngine) runFindQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + collection := db.Collection(queryType.CollectionName) + opts := &options.FindOptions{Limit: queryType.Limit, Skip: queryType.Skip, Sort: queryType.Sort} + if len(queryType.Args) > 1 { + opts.SetProjection(queryType.Args[1]) + } + //TODO could consider using context.WithTimeout here + cursor, err := collection. + Find(context.Background(), queryType.Args[0], opts) + if err != nil { + return nil, err + } + defer cursor.Close(context.Background()) + keys, data := mongoutils.MongoCursorToJson(cursor) + return map[string]interface{}{ + "keys": keys, + "data": data, + }, nil + +} + +func (mqw *MongoQueryEngine) runInsertOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result, err := db.Collection(queryType.CollectionName). + InsertOne(context.Background(), queryType.Args[0]) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"insertedId"}, + "data": []map[string]interface{}{ + { + "insertedId": result.InsertedID, + }, + }, + }, nil + +} + +func (mqw *MongoQueryEngine) runInsertManyQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result, err := db.Collection(queryType.CollectionName). + InsertMany(context.Background(), queryType.Args[0].(bson.A)) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"insertedIds"}, + "data": []map[string]interface{}{ + { + "insertedIds": result.InsertedIDs, + }, + }, + }, nil + +} + +func (mqw *MongoQueryEngine) runDeleteOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result, err := db.Collection(queryType.CollectionName). + DeleteOne(context.Background(), queryType.Args[0]) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"deletedCount"}, + "data": []map[string]interface{}{ + { + "deletedCount": result.DeletedCount, + }, + }, + }, nil } +func (mqw *MongoQueryEngine) runDeleteManyQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result, err := db.Collection(queryType.CollectionName). + DeleteMany(context.Background(), queryType.Args[0].(bson.D)) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"deletedCount"}, + "data": []map[string]interface{}{ + { + "deletedCount": result.DeletedCount, + }, + }, + }, nil + +} + +func (mqw *MongoQueryEngine) runUpdateOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result, err := db.Collection(queryType.CollectionName). + UpdateOne(context.Background(), queryType.Args[0], queryType.Args[1]) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"matchedCount", "modifiedCount", "upsertedCount"}, + "data": []map[string]interface{}{ + { + "matchedCount": result.MatchedCount, + "modifiedCount": result.ModifiedCount, + "upsertedCount": result.UpsertedCount, + }, + }, + }, nil + +} + +func (mwq *MongoQueryEngine) runUpdateManyQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result, err := db.Collection(queryType.CollectionName). + UpdateMany(context.Background(), queryType.Args[0], queryType.Args[1]) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"matchedCount", "modifiedCount", "upsertedCount"}, + "data": []map[string]interface{}{ + { + "matchedCount": result.MatchedCount, + "modifiedCount": result.ModifiedCount, + "upsertedCount": result.UpsertedCount, + }, + }, + }, nil + +} + +func (mqw *MongoQueryEngine) runReplaceOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result, err := db.Collection(queryType.CollectionName). + ReplaceOne(context.Background(), queryType.Args[0], queryType.Args[1]) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"matchedCount", "modifiedCount", "upsertedCount"}, + "data": []map[string]interface{}{ + { + "matchedCount": result.MatchedCount, + "modifiedCount": result.ModifiedCount, + "upsertedCount": result.UpsertedCount, + }, + }, + }, nil + +} + +func (mqw *MongoQueryEngine) runCMDQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + result := db.RunCommand(context.Background(), queryType.Args[0]) + if result.Err() != nil { + return nil, result.Err() + } + keys, data := mongoutils.MongoSingleResultToJson(result) + + return map[string]interface{}{ + "keys": keys, + "data": data, + }, + nil + +} + +func (mqw *MongoQueryEngine) runGetIndexesQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + cursor, err := db.RunCommandCursor(context.Background(), bson.M{ + "listIndexes": queryType.CollectionName, + }) + if err != nil { + return nil, err + } + defer cursor.Close(context.Background()) + keys, data := mongoutils.MongoCursorToJson(cursor) + + return map[string]interface{}{ + "keys": keys, + "data": data, + }, nil + +} + +func (mqw *MongoQueryEngine) runListCollectionsQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + list, err := db.ListCollectionNames(context.Background(), queryType.Args[0]) + if err != nil { + return nil, err + } + data := []map[string]interface{}{} + for _, name := range list { + data = append(data, map[string]interface{}{"collectionName": name}) + } + return map[string]interface{}{ + "keys": []string{"collectionName"}, + "data": data, + }, nil + +} + +func (mqw *MongoQueryEngine) runCountQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + opts := &options.CountOptions{Limit: queryType.Limit, Skip: queryType.Skip} + count, err := db.Collection(queryType.CollectionName). + CountDocuments(context.Background(), queryType.Args[0], opts) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "keys": []string{"count"}, + "data": []map[string]interface{}{ + { + "count": count, + }, + }, + }, nil +} + +func (mqw *MongoQueryEngine) runAggregateQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + cursor, err := db.Collection(queryType.CollectionName). + Aggregate(context.Background(), queryType.Args[0]) + if err != nil { + return nil, err + } + defer cursor.Close(context.Background()) + keys, data := mongoutils.MongoCursorToJson(cursor) + + return map[string]interface{}{ + "keys": keys, + "data": data, + }, nil +} + +func (mqw *MongoQueryEngine) runDropQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + err := db.Collection(queryType.CollectionName).Drop(context.Background()) + if err != nil { + return nil, err + } + return map[string]interface{}{ + "message": "droped collection: " + queryType.CollectionName, + }, nil +} + +func (mqw *MongoQueryEngine) runCreateIndexQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + opts := options.Index() + if len(queryType.Args) > 1 { + if d, ok := queryType.Args[1].(bson.D); ok { + if unique, ok := d.Map()["unique"].(bool); ok { + opts.SetUnique(unique) + } + if name, ok := d.Map()["name"].(string); ok { + opts.SetName(name) + } + } + } + indexModel := mongo.IndexModel{ + Keys: queryType.Args[0], + Options: opts, + } + idxName, err := db.Collection(queryType.CollectionName).Indexes().CreateOne(context.Background(), indexModel) + if err != nil { + return nil, err + } + + return map[string]interface{}{ + "message": "created index: " + idxName, + }, nil +} + +func (mqw *MongoQueryEngine) runDropIndexQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { + indexName, ok := queryType.Args[0].(string) + if !ok { + return nil, errors.New("invalid query") + } + if indexName == "*" { + _, err := db.Collection(queryType.CollectionName).Indexes().DropAll(context.Background(), options.DropIndexes()) + if err != nil { + return nil, err + } + + return map[string]interface{}{ + "message": "droped all indexes in collection: " + queryType.CollectionName, + }, nil + } + _, err := db.Collection(queryType.CollectionName).Indexes().DropOne(context.Background(), indexName, options.DropIndexes()) + if err != nil { + return nil, err + } + + return map[string]interface{}{ + "message": "droped index: " + indexName, + }, nil +} + func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, config *models.QueryConfig) (map[string]interface{}, error) { port, _ := strconv.Atoi(string(dbConn.DBPort)) if dbConn.UseSSH != models.DBUSESSH_NONE { @@ -78,292 +364,176 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, return result, nil } else if queryType.QueryType == mongoutils.QUERY_FIND { - collection := db.Collection(queryType.CollectionName) - opts := &options.FindOptions{Limit: queryType.Limit, Skip: queryType.Skip, Sort: queryType.Sort} - if len(queryType.Args) > 1 { - opts.SetProjection(queryType.Args[1]) - } - cursor, err := collection. - Find(context.Background(), queryType.Args[0], opts) + result, err := mqe.runFindQuery(db, queryType) if err != nil { return nil, err } - defer cursor.Close(context.Background()) - keys, data := mongoutils.MongoCursorToJson(cursor) if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": keys, - "data": data, - }, nil + + return result, nil + } else if queryType.QueryType == mongoutils.QUERY_INSERTONE { - result, err := db.Collection(queryType.CollectionName). - InsertOne(context.Background(), queryType.Args[0]) + result, err := mqe.runInsertOneQuery(db, queryType) if err != nil { return nil, err } + if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"insertedId"}, - "data": []map[string]interface{}{ - { - "insertedId": result.InsertedID, - }, - }, - }, nil + + return result, nil + } else if queryType.QueryType == mongoutils.QUERY_INSERT { - result, err := db.Collection(queryType.CollectionName). - InsertMany(context.Background(), queryType.Args[0].(bson.A)) + result, err := mqe.runInsertManyQuery(db, queryType) + if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"insertedIDs"}, - "data": []map[string]interface{}{ - { - "insertedIDs": result.InsertedIDs, - }, - }, - }, nil + + return result, nil + } else if queryType.QueryType == mongoutils.QUERY_DELETEONE { - result, err := db.Collection(queryType.CollectionName). - DeleteOne(context.Background(), queryType.Args[0]) + result, err := mqe.runDeleteOneQuery(db, queryType) if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"deletedCount"}, - "data": []map[string]interface{}{ - { - "deletedCount": result.DeletedCount, - }, - }, - }, nil + + return result, nil + } else if queryType.QueryType == mongoutils.QUERY_DELETEMANY { - result, err := db.Collection(queryType.CollectionName). - DeleteMany(context.Background(), queryType.Args[0].(bson.D)) + result, err := mqe.runDeleteManyQuery(db, queryType) if err != nil { return nil, err } + if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"deletedCount"}, - "data": []map[string]interface{}{ - { - "deletedCount": result.DeletedCount, - }, - }, - }, nil + + return result, nil + } else if queryType.QueryType == mongoutils.QUERY_UPDATEONE { - result, err := db.Collection(queryType.CollectionName). - UpdateOne(context.Background(), queryType.Args[0], queryType.Args[1]) + result, err := mqe.runUpdateOneQuery(db, queryType) + if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"matchedCount", "updatedCount", "upsertedCount"}, - "data": []map[string]interface{}{ - { - "matchedCount": result.MatchedCount, - "updatedCount": result.ModifiedCount, - "upsertedCount": result.UpsertedCount, - }, - }, - }, nil + return result, nil } else if queryType.QueryType == mongoutils.QUERY_UPDATEMANY { - result, err := db.Collection(queryType.CollectionName). - UpdateMany(context.Background(), queryType.Args[0], queryType.Args[1]) + result, err := mqe.runUpdateManyQuery(db, queryType) if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"matchedCount", "updatedCount", "upsertedCount"}, - "data": []map[string]interface{}{ - { - "matchedCount": result.MatchedCount, - "updatedCount": result.ModifiedCount, - "upsertedCount": result.UpsertedCount, - }, - }, - }, nil + + return result, nil } else if queryType.QueryType == mongoutils.QUERY_REPLACEONE { - result, err := db.Collection(queryType.CollectionName). - ReplaceOne(context.Background(), queryType.Args[0], queryType.Args[1]) + result, err := mqe.runReplaceOneQuery(db, queryType) if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"matchedCount", "updatedCount", "upsertedCount"}, - "data": []map[string]interface{}{ - { - "matchedCount": result.MatchedCount, - "updatedCount": result.ModifiedCount, - "upsertedCount": result.UpsertedCount, - }, - }, - }, nil + + return result, nil } else if queryType.QueryType == mongoutils.QUERY_RUNCMD { - result := db.RunCommand(context.Background(), queryType.Args[0]) - if result.Err() != nil { - return nil, result.Err() + result, err := mqe.runCMDQuery(db, queryType) + if err != nil { + return nil, err } - keys, data := mongoutils.MongoSingleResultToJson(result) + if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": keys, - "data": data, - }, nil + + return result, nil + } else if queryType.QueryType == mongoutils.QUERY_GETINDEXES { - cursor, err := db.RunCommandCursor(context.Background(), bson.M{ - "listIndexes": queryType.CollectionName, - }) + results, err := mqe.runGetIndexesQuery(db, queryType) if err != nil { return nil, err } - defer cursor.Close(context.Background()) - keys, data := mongoutils.MongoCursorToJson(cursor) + if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": keys, - "data": data, - }, nil + + return results, nil } else if queryType.QueryType == mongoutils.QUERY_LISTCOLLECTIONS { - list, err := db.ListCollectionNames(context.Background(), queryType.Args[0]) + list, err := mqe.runListCollectionsQuery(db, queryType) if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - data := []map[string]interface{}{} - for _, name := range list { - data = append(data, map[string]interface{}{"collectionName": name}) - } - return map[string]interface{}{ - "keys": []string{"collectionName"}, - "data": data, - }, nil + + return list, nil + } else if queryType.QueryType == mongoutils.QUERY_COUNT { - opts := &options.CountOptions{Limit: queryType.Limit, Skip: queryType.Skip} - count, err := db.Collection(queryType.CollectionName). - CountDocuments(context.Background(), queryType.Args[0], opts) + + count, err := mqe.runCountQuery(db, queryType) if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": []string{"count"}, - "data": []map[string]interface{}{ - { - "count": count, - }, - }, - }, nil + + return count, nil + } else if queryType.QueryType == mongoutils.QUERY_AGGREGATE { - cursor, err := db.Collection(queryType.CollectionName). - Aggregate(context.Background(), queryType.Args[0]) + cursor, err := mqe.runAggregateQuery(db, queryType) if err != nil { return nil, err } - defer cursor.Close(context.Background()) - keys, data := mongoutils.MongoCursorToJson(cursor) if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "keys": keys, - "data": data, - }, nil + + return cursor, nil } else if queryType.QueryType == mongoutils.QUERY_DROP { - err := db.Collection(queryType.CollectionName).Drop(context.Background()) + result, err := mqe.runDropQuery(db, queryType) if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "message": "droped collection: " + queryType.CollectionName, - }, nil + return result, nil } else if queryType.QueryType == mongoutils.QUERY_CREATEINDEX { - opts := options.Index() - if len(queryType.Args) > 1 { - if d, ok := queryType.Args[1].(bson.D); ok { - if unique, ok := d.Map()["unique"].(bool); ok { - opts.SetUnique(unique) - } - if name, ok := d.Map()["name"].(string); ok { - opts.SetName(name) - } - } - } - indexModel := mongo.IndexModel{ - Keys: queryType.Args[0], - Options: opts, - } - idxName, err := db.Collection(queryType.CollectionName).Indexes().CreateOne(context.Background(), indexModel) + index, err := mqe.runCreateIndexQuery(db, queryType) if err != nil { return nil, err } if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "message": "created index: " + idxName, - }, nil + + return index, nil + } else if queryType.QueryType == mongoutils.QUERY_DROPINDEX { - indexName, ok := queryType.Args[0].(string) - if !ok { - return nil, errors.New("invalid query") - } - if indexName == "*" { - _, err := db.Collection(queryType.CollectionName).Indexes().DropAll(context.Background(), options.DropIndexes()) - if err != nil { - return nil, err - } - if config.CreateLogFn != nil { - config.CreateLogFn(query) - } - return map[string]interface{}{ - "message": "droped all indexes in collection: " + queryType.CollectionName, - }, nil - } - _, err := db.Collection(queryType.CollectionName).Indexes().DropOne(context.Background(), indexName, options.DropIndexes()) - if err != nil { - return nil, err - } + result, err := mqe.runDropIndexQuery(db, queryType) + if config.CreateLogFn != nil { config.CreateLogFn(query) } - return map[string]interface{}{ - "message": "droped index: " + indexName, - }, nil + return result, err } return nil, errors.New("unknown query") } From 95c2c44315836aea9fdc59f719d803c87f94e4d7 Mon Sep 17 00:00:00 2001 From: DeanRTaylor1 Date: Mon, 10 Apr 2023 20:57:08 +0700 Subject: [PATCH 03/10] refactored to switch-case statement --- .../mongoqueryengine/queryengine.go | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/pkg/queryengines/mongoqueryengine/queryengine.go b/pkg/queryengines/mongoqueryengine/queryengine.go index dabc7103..d565595c 100644 --- a/pkg/queryengines/mongoqueryengine/queryengine.go +++ b/pkg/queryengines/mongoqueryengine/queryengine.go @@ -355,15 +355,16 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, return nil, errors.New("not allowed run this query") } - if queryType.QueryType == mongoutils.QUERY_FINDONE { + switch queryType.QueryType { + + case mongoutils.QUERY_FINDONE: result, err := mqe.runFindOneQuery(db, queryType) if err != nil { return nil, err } return result, nil - - } else if queryType.QueryType == mongoutils.QUERY_FIND { + case mongoutils.QUERY_FIND: result, err := mqe.runFindQuery(db, queryType) if err != nil { return nil, err @@ -374,7 +375,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, return result, nil - } else if queryType.QueryType == mongoutils.QUERY_INSERTONE { + case mongoutils.QUERY_INSERTONE: result, err := mqe.runInsertOneQuery(db, queryType) if err != nil { return nil, err @@ -385,8 +386,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return result, nil - - } else if queryType.QueryType == mongoutils.QUERY_INSERT { + case mongoutils.QUERY_INSERT: result, err := mqe.runInsertManyQuery(db, queryType) if err != nil { @@ -398,7 +398,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, return result, nil - } else if queryType.QueryType == mongoutils.QUERY_DELETEONE { + case mongoutils.QUERY_DELETEONE: result, err := mqe.runDeleteOneQuery(db, queryType) if err != nil { return nil, err @@ -409,7 +409,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, return result, nil - } else if queryType.QueryType == mongoutils.QUERY_DELETEMANY { + case mongoutils.QUERY_DELETEMANY: result, err := mqe.runDeleteManyQuery(db, queryType) if err != nil { return nil, err @@ -420,8 +420,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return result, nil - - } else if queryType.QueryType == mongoutils.QUERY_UPDATEONE { + case mongoutils.QUERY_UPDATEONE: result, err := mqe.runUpdateOneQuery(db, queryType) if err != nil { @@ -431,7 +430,8 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, config.CreateLogFn(query) } return result, nil - } else if queryType.QueryType == mongoutils.QUERY_UPDATEMANY { + + case mongoutils.QUERY_UPDATEMANY: result, err := mqe.runUpdateManyQuery(db, queryType) if err != nil { return nil, err @@ -441,7 +441,8 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return result, nil - } else if queryType.QueryType == mongoutils.QUERY_REPLACEONE { + + case mongoutils.QUERY_REPLACEONE: result, err := mqe.runReplaceOneQuery(db, queryType) if err != nil { return nil, err @@ -451,7 +452,8 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return result, nil - } else if queryType.QueryType == mongoutils.QUERY_RUNCMD { + + case mongoutils.QUERY_RUNCMD: result, err := mqe.runCMDQuery(db, queryType) if err != nil { return nil, err @@ -462,8 +464,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return result, nil - - } else if queryType.QueryType == mongoutils.QUERY_GETINDEXES { + case mongoutils.QUERY_GETINDEXES: results, err := mqe.runGetIndexesQuery(db, queryType) if err != nil { return nil, err @@ -474,7 +475,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return results, nil - } else if queryType.QueryType == mongoutils.QUERY_LISTCOLLECTIONS { + case mongoutils.QUERY_LISTCOLLECTIONS: list, err := mqe.runListCollectionsQuery(db, queryType) if err != nil { return nil, err @@ -484,8 +485,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return list, nil - - } else if queryType.QueryType == mongoutils.QUERY_COUNT { + case mongoutils.QUERY_COUNT: count, err := mqe.runCountQuery(db, queryType) if err != nil { @@ -496,8 +496,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return count, nil - - } else if queryType.QueryType == mongoutils.QUERY_AGGREGATE { + case mongoutils.QUERY_AGGREGATE: cursor, err := mqe.runAggregateQuery(db, queryType) if err != nil { return nil, err @@ -507,7 +506,8 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, } return cursor, nil - } else if queryType.QueryType == mongoutils.QUERY_DROP { + + case mongoutils.QUERY_DROP: result, err := mqe.runDropQuery(db, queryType) if err != nil { return nil, err @@ -516,7 +516,7 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, config.CreateLogFn(query) } return result, nil - } else if queryType.QueryType == mongoutils.QUERY_CREATEINDEX { + case mongoutils.QUERY_CREATEINDEX: index, err := mqe.runCreateIndexQuery(db, queryType) if err != nil { return nil, err @@ -527,15 +527,18 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, return index, nil - } else if queryType.QueryType == mongoutils.QUERY_DROPINDEX { + case mongoutils.QUERY_DROPINDEX: result, err := mqe.runDropIndexQuery(db, queryType) if config.CreateLogFn != nil { config.CreateLogFn(query) } return result, err + + default: + return nil, errors.New("unknown query") } - return nil, errors.New("unknown query") + } func (mqe *MongoQueryEngine) TestConnection(dbConn *models.DBConnection, config *models.QueryConfig) error { From c629c8d5ad6e12e031c4353c524b353da8c7d05f Mon Sep 17 00:00:00 2001 From: DeanRTaylor1 Date: Tue, 11 Apr 2023 14:00:23 +0700 Subject: [PATCH 04/10] added missing config.createLogFn --- pkg/queryengines/mongoqueryengine/queryengine.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/queryengines/mongoqueryengine/queryengine.go b/pkg/queryengines/mongoqueryengine/queryengine.go index d565595c..b975be0d 100644 --- a/pkg/queryengines/mongoqueryengine/queryengine.go +++ b/pkg/queryengines/mongoqueryengine/queryengine.go @@ -47,7 +47,7 @@ func (mqw *MongoQueryEngine) runFindQuery(db *mongo.Database, queryType *mongout if len(queryType.Args) > 1 { opts.SetProjection(queryType.Args[1]) } - //TODO could consider using context.WithTimeout here + cursor, err := collection. Find(context.Background(), queryType.Args[0], opts) if err != nil { @@ -363,6 +363,9 @@ func (mqe *MongoQueryEngine) RunQuery(dbConn *models.DBConnection, query string, return nil, err } + if config.CreateLogFn != nil { + config.CreateLogFn(query) + } return result, nil case mongoutils.QUERY_FIND: result, err := mqe.runFindQuery(db, queryType) From 70e6aa72ef3b1b548361812a022be529e528052b Mon Sep 17 00:00:00 2001 From: DeanRTaylor1 Date: Tue, 11 Apr 2023 14:26:04 +0700 Subject: [PATCH 05/10] renamed receiver variables in new functions to mqe for consistency --- .../mongoqueryengine/queryengine.go | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/queryengines/mongoqueryengine/queryengine.go b/pkg/queryengines/mongoqueryengine/queryengine.go index b975be0d..e79d97be 100644 --- a/pkg/queryengines/mongoqueryengine/queryengine.go +++ b/pkg/queryengines/mongoqueryengine/queryengine.go @@ -29,7 +29,7 @@ func InitMongoQueryEngine() *MongoQueryEngine { } } -func (mqw *MongoQueryEngine) runFindOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runFindOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result := db.Collection(queryType.CollectionName).FindOne(context.Background(), queryType.Args[0]) if result.Err() != nil { return nil, result.Err() @@ -41,7 +41,7 @@ func (mqw *MongoQueryEngine) runFindOneQuery(db *mongo.Database, queryType *mong }, nil } -func (mqw *MongoQueryEngine) runFindQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runFindQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { collection := db.Collection(queryType.CollectionName) opts := &options.FindOptions{Limit: queryType.Limit, Skip: queryType.Skip, Sort: queryType.Sort} if len(queryType.Args) > 1 { @@ -62,7 +62,7 @@ func (mqw *MongoQueryEngine) runFindQuery(db *mongo.Database, queryType *mongout } -func (mqw *MongoQueryEngine) runInsertOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runInsertOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result, err := db.Collection(queryType.CollectionName). InsertOne(context.Background(), queryType.Args[0]) if err != nil { @@ -79,7 +79,7 @@ func (mqw *MongoQueryEngine) runInsertOneQuery(db *mongo.Database, queryType *mo } -func (mqw *MongoQueryEngine) runInsertManyQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runInsertManyQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result, err := db.Collection(queryType.CollectionName). InsertMany(context.Background(), queryType.Args[0].(bson.A)) if err != nil { @@ -96,7 +96,7 @@ func (mqw *MongoQueryEngine) runInsertManyQuery(db *mongo.Database, queryType *m } -func (mqw *MongoQueryEngine) runDeleteOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runDeleteOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result, err := db.Collection(queryType.CollectionName). DeleteOne(context.Background(), queryType.Args[0]) if err != nil { @@ -113,7 +113,7 @@ func (mqw *MongoQueryEngine) runDeleteOneQuery(db *mongo.Database, queryType *mo } -func (mqw *MongoQueryEngine) runDeleteManyQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runDeleteManyQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result, err := db.Collection(queryType.CollectionName). DeleteMany(context.Background(), queryType.Args[0].(bson.D)) if err != nil { @@ -130,7 +130,7 @@ func (mqw *MongoQueryEngine) runDeleteManyQuery(db *mongo.Database, queryType *m } -func (mqw *MongoQueryEngine) runUpdateOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runUpdateOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result, err := db.Collection(queryType.CollectionName). UpdateOne(context.Background(), queryType.Args[0], queryType.Args[1]) if err != nil { @@ -168,7 +168,7 @@ func (mwq *MongoQueryEngine) runUpdateManyQuery(db *mongo.Database, queryType *m } -func (mqw *MongoQueryEngine) runReplaceOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runReplaceOneQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result, err := db.Collection(queryType.CollectionName). ReplaceOne(context.Background(), queryType.Args[0], queryType.Args[1]) if err != nil { @@ -187,7 +187,7 @@ func (mqw *MongoQueryEngine) runReplaceOneQuery(db *mongo.Database, queryType *m } -func (mqw *MongoQueryEngine) runCMDQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runCMDQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { result := db.RunCommand(context.Background(), queryType.Args[0]) if result.Err() != nil { return nil, result.Err() @@ -202,7 +202,7 @@ func (mqw *MongoQueryEngine) runCMDQuery(db *mongo.Database, queryType *mongouti } -func (mqw *MongoQueryEngine) runGetIndexesQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runGetIndexesQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { cursor, err := db.RunCommandCursor(context.Background(), bson.M{ "listIndexes": queryType.CollectionName, }) @@ -219,7 +219,7 @@ func (mqw *MongoQueryEngine) runGetIndexesQuery(db *mongo.Database, queryType *m } -func (mqw *MongoQueryEngine) runListCollectionsQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runListCollectionsQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { list, err := db.ListCollectionNames(context.Background(), queryType.Args[0]) if err != nil { return nil, err @@ -235,7 +235,7 @@ func (mqw *MongoQueryEngine) runListCollectionsQuery(db *mongo.Database, queryTy } -func (mqw *MongoQueryEngine) runCountQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runCountQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { opts := &options.CountOptions{Limit: queryType.Limit, Skip: queryType.Skip} count, err := db.Collection(queryType.CollectionName). CountDocuments(context.Background(), queryType.Args[0], opts) @@ -252,7 +252,7 @@ func (mqw *MongoQueryEngine) runCountQuery(db *mongo.Database, queryType *mongou }, nil } -func (mqw *MongoQueryEngine) runAggregateQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runAggregateQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { cursor, err := db.Collection(queryType.CollectionName). Aggregate(context.Background(), queryType.Args[0]) if err != nil { @@ -267,7 +267,7 @@ func (mqw *MongoQueryEngine) runAggregateQuery(db *mongo.Database, queryType *mo }, nil } -func (mqw *MongoQueryEngine) runDropQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runDropQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { err := db.Collection(queryType.CollectionName).Drop(context.Background()) if err != nil { return nil, err @@ -277,7 +277,7 @@ func (mqw *MongoQueryEngine) runDropQuery(db *mongo.Database, queryType *mongout }, nil } -func (mqw *MongoQueryEngine) runCreateIndexQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runCreateIndexQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { opts := options.Index() if len(queryType.Args) > 1 { if d, ok := queryType.Args[1].(bson.D); ok { @@ -303,7 +303,7 @@ func (mqw *MongoQueryEngine) runCreateIndexQuery(db *mongo.Database, queryType * }, nil } -func (mqw *MongoQueryEngine) runDropIndexQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { +func (mqe *MongoQueryEngine) runDropIndexQuery(db *mongo.Database, queryType *mongoutils.MongoQuery) (map[string]interface{}, error) { indexName, ok := queryType.Args[0].(string) if !ok { return nil, errors.New("invalid query") From 26f251071f3697132151c9493a4a58934b9b93a3 Mon Sep 17 00:00:00 2001 From: Nitin Date: Sun, 16 Apr 2023 01:43:26 +0530 Subject: [PATCH 06/10] refactor: jsontable, showdata and sidebar Currenlty, the way types of Row and Column are defined using any, it's not possible to get the `useTable` hook work without error. --- .../dbfragments/jsontable/jsontable.tsx | 4 ++-- frontend/src/components/dbfragments/showdata.tsx | 6 +----- frontend/src/components/layouts/sidebar.tsx | 16 +++++++++------- frontend/src/styles/globals.css | 1 - 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/frontend/src/components/dbfragments/jsontable/jsontable.tsx b/frontend/src/components/dbfragments/jsontable/jsontable.tsx index 78484cd0..fd100056 100644 --- a/frontend/src/components/dbfragments/jsontable/jsontable.tsx +++ b/frontend/src/components/dbfragments/jsontable/jsontable.tsx @@ -62,7 +62,7 @@ const JsonTable = ({ queryData, dbConnection, mName, isInteractive, showHeader, const changeFilter = () => { let filter: string[] | undefined = undefined - let filterText = filterRef.current!.value.trim() + const filterText = filterRef.current!.value.trim() if (filterText !== '' && filterText.startsWith("{") && filterText.endsWith("}")) { filter = [filterText] } @@ -74,7 +74,7 @@ const JsonTable = ({ queryData, dbConnection, mName, isInteractive, showHeader, return } let sort: string[] | undefined = undefined - let sortText = sortRef.current!.value.trim() + const sortText = sortRef.current!.value.trim() if (sortText !== '' && sortText.startsWith("{") && sortText.endsWith("}")) { sort = [sortText] } diff --git a/frontend/src/components/dbfragments/showdata.tsx b/frontend/src/components/dbfragments/showdata.tsx index 608bf4aa..92061de2 100644 --- a/frontend/src/components/dbfragments/showdata.tsx +++ b/frontend/src/components/dbfragments/showdata.tsx @@ -11,11 +11,7 @@ import JsonTable from './jsontable/jsontable' import { getDBDataInDataModel, selectIsFetchingQueryData, selectQueryData } from '../../redux/dataModelSlice' import TabContext from '../layouts/tabcontext' -type DBShowDataPropType = { - -} - -const DBShowDataFragment = (_: DBShowDataPropType) => { +const DBShowDataFragment = () => { const dispatch = useAppDispatch() diff --git a/frontend/src/components/layouts/sidebar.tsx b/frontend/src/components/layouts/sidebar.tsx index c56f08c7..1abca861 100644 --- a/frontend/src/components/layouts/sidebar.tsx +++ b/frontend/src/components/layouts/sidebar.tsx @@ -16,17 +16,19 @@ enum SidebarViewType { SETTINGS = "SETTINGS" // Used to show elements of settings screen } -type SidebarPropType = {} +// type SidebarPropType = {} -const Sidebar = (_: SidebarPropType) => { +const Sidebar = () => { const location = useLocation() - const { queryId } = useParams() - const [searchParams] = useSearchParams() - const mschema = searchParams.get("mschema") - const mname = searchParams.get("mname") - let sidebarView: SidebarViewType = + // Commenting them as they might be neded in future + // const { queryId } = useParams() + // const [searchParams] = useSearchParams() + // const mschema = searchParams.get("mschema") + // const mname = searchParams.get("mname") + + const sidebarView: SidebarViewType = (location.pathname.startsWith("/db")) ? SidebarViewType.DATABASE : (location.pathname.startsWith("/settings")) ? SidebarViewType.SETTINGS : SidebarViewType.HOME diff --git a/frontend/src/styles/globals.css b/frontend/src/styles/globals.css index c135387a..0a4d6a00 100644 --- a/frontend/src/styles/globals.css +++ b/frontend/src/styles/globals.css @@ -110,7 +110,6 @@ code { display: flex; justify-content: center; align-items: center; - display: inline-flex; border-radius: 100px; } From 8383464564ad964bf0ab20a90bd3676fc48ffade Mon Sep 17 00:00:00 2001 From: Nitin Date: Sun, 16 Apr 2023 02:13:10 +0530 Subject: [PATCH 07/10] feat: added types to resolve issue with useTable hook Refer: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-table/Readme.md --- .../dbfragments/jsontable/jsontable.tsx | 7 +- .../components/dbfragments/table/table.tsx | 13 +- frontend/src/types/react-table-config.d.ts | 130 ++++++++++++++++++ 3 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 frontend/src/types/react-table-config.d.ts diff --git a/frontend/src/components/dbfragments/jsontable/jsontable.tsx b/frontend/src/components/dbfragments/jsontable/jsontable.tsx index fd100056..72ba5211 100644 --- a/frontend/src/components/dbfragments/jsontable/jsontable.tsx +++ b/frontend/src/components/dbfragments/jsontable/jsontable.tsx @@ -107,10 +107,11 @@ const JsonTable = ({ queryData, dbConnection, mName, isInteractive, showHeader, rows, prepareRow, state, - } = useTable({ + } = useTable({ columns, data, defaultColumn, + initialState: { selectedRowIds : {}}, ...{ editingCellIndex, startEditing, onSaveCell } }, useRowSelect, hooks => { if (isInteractive && isEditing) @@ -127,7 +128,7 @@ const JsonTable = ({ queryData, dbConnection, mName, isInteractive, showHeader, const newState: any = state // temporary typescript hack const selectedRows: number[] = Object.keys(newState.selectedRowIds).map(x => parseInt(x)) - const selectedUnderscoreIDs = rows.filter((_, i) => selectedRows.includes(i)).map(x => x.original['_id']).filter(x => x) + const selectedUnderscoreIDs = rows.filter((_, i) => selectedRows.includes(i)).map(x => (x.original as any)['_id']).filter(x => x) const deleteRows = async () => { if (selectedUnderscoreIDs.length > 0) { @@ -275,4 +276,4 @@ const CellSelectionComponent = ({ row }: any) => ( ) -export default JsonTable \ No newline at end of file +export default JsonTable diff --git a/frontend/src/components/dbfragments/table/table.tsx b/frontend/src/components/dbfragments/table/table.tsx index fdbfe3b9..cc64571c 100644 --- a/frontend/src/components/dbfragments/table/table.tsx +++ b/frontend/src/components/dbfragments/table/table.tsx @@ -100,10 +100,11 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isInteractive, showHea rows, prepareRow, state, - } = useTable({ + } = useTable({ columns, data, defaultColumn, + initialState: { selectedRowIds: {}}, ...{ editCell, resetEditCell, onSaveCell } }, useRowSelect, @@ -121,12 +122,10 @@ const Table = ({ queryData, dbConnection, mSchema, mName, isInteractive, showHea } ) - const newState: any = state // temporary typescript hack - const selectedRowIds: any = newState.selectedRowIds - const selectedRows: number[] = Object.keys(selectedRowIds).map(x => parseInt(x)) + const selectedRows: number[] = Object.keys(state.selectedRowIds).map(x => parseInt(x)) const selectedIDs = dbConnection.type === DBConnType.POSTGRES ? - rows.filter((_, i) => selectedRows.includes(i)).map(x => x.original['0']).filter(x => x) - : rows.filter((_, i) => selectedRows.includes(i)).map(x => queryData.pkeys!.map((pkey) => ({ [pkey]: x.original[queryData.columns.findIndex(x => x === pkey)] }))).map(x => x.reduce(((r, c) => Object.assign(r, c)), {})).map(x => JSON.stringify(x)) + rows.filter((_, i) => selectedRows.includes(i)).map(x => (x.original as any)['0']).filter(x => x) + : rows.filter((_, i) => selectedRows.includes(i)).map(x => queryData.pkeys!.map((pkey) => ({ [pkey]: (x.original as any)[queryData.columns.findIndex(x => x === pkey)] }))).map(x => x.reduce(((r, c) => Object.assign(r, c)), {})).map(x => JSON.stringify(x)) const deleteRows = async () => { if (dbConnection.type === DBConnType.MYSQL && queryData.pkeys?.length === 0) { @@ -353,4 +352,4 @@ const CellSelectionComponent = ({ row }: any) => ( ) -export default Table \ No newline at end of file +export default Table diff --git a/frontend/src/types/react-table-config.d.ts b/frontend/src/types/react-table-config.d.ts new file mode 100644 index 00000000..a9ad8b9d --- /dev/null +++ b/frontend/src/types/react-table-config.d.ts @@ -0,0 +1,130 @@ +import { + UseColumnOrderInstanceProps, + UseColumnOrderState, + UseExpandedHooks, + UseExpandedInstanceProps, + UseExpandedOptions, + UseExpandedRowProps, + UseExpandedState, + UseFiltersColumnOptions, + UseFiltersColumnProps, + UseFiltersInstanceProps, + UseFiltersOptions, + UseFiltersState, + UseGlobalFiltersColumnOptions, + UseGlobalFiltersInstanceProps, + UseGlobalFiltersOptions, + UseGlobalFiltersState, + UseGroupByCellProps, + UseGroupByColumnOptions, + UseGroupByColumnProps, + UseGroupByHooks, + UseGroupByInstanceProps, + UseGroupByOptions, + UseGroupByRowProps, + UseGroupByState, + UsePaginationInstanceProps, + UsePaginationOptions, + UsePaginationState, + UseResizeColumnsColumnOptions, + UseResizeColumnsColumnProps, + UseResizeColumnsOptions, + UseResizeColumnsState, + UseRowSelectHooks, + UseRowSelectInstanceProps, + UseRowSelectOptions, + UseRowSelectRowProps, + UseRowSelectState, + UseRowStateCellProps, + UseRowStateInstanceProps, + UseRowStateOptions, + UseRowStateRowProps, + UseRowStateState, + UseSortByColumnOptions, + UseTableRowProps, + UseSortByColumnProps, + UseSortByHooks, + UseSortByInstanceProps, + UseSortByOptions, + UseSortByState, +} from "react-table"; + +declare module "react-table" { + // take this file as-is, or comment out the sections that don't apply to your plugin configuration + + export interface TableOptions> + extends UseExpandedOptions, + UseFiltersOptions, + UseGlobalFiltersOptions, + UseGroupByOptions, + UsePaginationOptions, + UseResizeColumnsOptions, + UseRowSelectOptions, + UseRowStateOptions, + UseSortByOptions, + // note that having Record here allows you to add anything to the options, this matches the spirit of the + // underlying js library, but might be cleaner if it's replaced by a more specific type that matches your + // feature set, this is a safe default. + Record {} + + export interface Hooks< + D extends Record = Record + > extends UseExpandedHooks, + UseGroupByHooks, + UseRowSelectHooks, + UseSortByHooks {} + + export interface TableInstance< + D extends Record = Record + > extends UseColumnOrderInstanceProps, + UseExpandedInstanceProps, + UseFiltersInstanceProps, + UseGlobalFiltersInstanceProps, + UseGroupByInstanceProps, + UsePaginationInstanceProps, + UseRowSelectInstanceProps, + UseRowStateInstanceProps, + UseSortByInstanceProps {} + + export interface TableState< + D extends Record = Record + > extends UseColumnOrderState, + UseExpandedState, + UseFiltersState, + UseGlobalFiltersState, + UseGroupByState, + UsePaginationState, + UseResizeColumnsState, + UseRowSelectState, + UseRowStateState, + UseSortByState {} + + export interface ColumnInterface< + D extends Record = Record + > extends UseFiltersColumnOptions, + UseGlobalFiltersColumnOptions, + UseGroupByColumnOptions, + UseResizeColumnsColumnOptions, + UseSortByColumnOptions {} + + export interface ColumnInstance< + D extends Record = Record + > extends UseFiltersColumnProps, + UseGroupByColumnProps, + UseResizeColumnsColumnProps, + UseSortByColumnProps {} + + export interface Cell< + D extends Record = Record, + V = any + > extends UseGroupByCellProps, + UseRowStateCellProps {} + + export interface Row< + D extends Record = Record + > extends UseExpandedRowProps, + UseGroupByRowProps, + UseRowSelectRowProps, + UseRowStateRowProps, + UseTableRowProps {} +} From 62843ea182548016b9b914bc83574e2dedd1f725 Mon Sep 17 00:00:00 2001 From: Sanyam Jain Date: Sun, 16 Apr 2023 18:00:25 +0530 Subject: [PATCH 08/10] added refresh functionality to refresh the query history. --- .../src/components/dbfragments/history.tsx | 175 ++++++++++-------- 1 file changed, 97 insertions(+), 78 deletions(-) diff --git a/frontend/src/components/dbfragments/history.tsx b/frontend/src/components/dbfragments/history.tsx index 9cf74941..8adca9a8 100644 --- a/frontend/src/components/dbfragments/history.tsx +++ b/frontend/src/components/dbfragments/history.tsx @@ -1,87 +1,106 @@ -import React, { useContext, useEffect } from 'react' -import { DBConnection, Tab } from '../../data/models' -import { selectDBConnection } from '../../redux/dbConnectionSlice' -import { useAppDispatch, useAppSelector } from '../../redux/hooks' -import toast from 'react-hot-toast' -import InfiniteScroll from 'react-infinite-scroll-component' -import dateformat from 'dateformat' -import { getDBQueryLogs, reset, selectDBQueryLogs, selectDBQueryLogsNext } from '../../redux/dbHistorySlice' -import TabContext from '../layouts/tabcontext' +import { useContext, useEffect } from "react"; +import { DBConnection, Tab } from "../../data/models"; +import { selectDBConnection } from "../../redux/dbConnectionSlice"; +import { useAppDispatch, useAppSelector } from "../../redux/hooks"; +import toast from "react-hot-toast"; +import InfiniteScroll from "react-infinite-scroll-component"; +import dateformat from "dateformat"; +import { + getDBQueryLogs, + reset, + selectDBQueryLogs, + selectDBQueryLogsNext, +} from "../../redux/dbHistorySlice"; +import TabContext from "../layouts/tabcontext"; +type DBHistoryPropType = {}; -type DBHistoryPropType = { -} +const DBHistoryFragment = ({}: DBHistoryPropType) => { + const dispatch = useAppDispatch(); -const DBHistoryFragment = ({ }: DBHistoryPropType) => { + const currentTab: Tab = useContext(TabContext)!; - const dispatch = useAppDispatch() + const dbConnection: DBConnection | undefined = + useAppSelector(selectDBConnection); + const dbQueryLogs = useAppSelector(selectDBQueryLogs); + const dbQueryLogsNext = useAppSelector(selectDBQueryLogsNext); - const currentTab: Tab = useContext(TabContext)! + useEffect(() => { + if (dbConnection) { + (async () => { + dispatch(reset()); + })(); + fetchDBQueryLogs(); + } + }, [dispatch, dbConnection]); - const dbConnection: DBConnection | undefined = useAppSelector(selectDBConnection) - const dbQueryLogs = useAppSelector(selectDBQueryLogs) - const dbQueryLogsNext = useAppSelector(selectDBQueryLogsNext) + const fetchDBQueryLogs = async () => { + const result = await dispatch( + getDBQueryLogs({ dbConnId: dbConnection!.id }) + ).unwrap(); + if (!result.success) { + toast.error(result.error!); + } + }; - useEffect(() => { - if (dbConnection) { - (async () => { - dispatch(reset()) - })() - fetchDBQueryLogs() - } - }, [dispatch, dbConnection]) + function refreshHandler() { + dispatch(reset()); + fetchDBQueryLogs(); + } - const fetchDBQueryLogs = async () => { - const result = await dispatch(getDBQueryLogs({ dbConnId: dbConnection!.id })).unwrap() - if (!result.success) { - toast.error(result.error!) - } - } + return ( +
+ {dbConnection && ( + <> +
+

Showing History in {dbConnection.name}

+ +
+
+ Loading...

} + endMessage={ +

+ You have seen it all! +

+ } + scrollableTarget="maincontent" + > + + + {dbQueryLogs.map((log) => ( + + + + + ))} + +
+ {log.query} + + {dateformat( + log.createdAt, + "mmm dd, yyyy HH:MM:ss" + )} +
+
+ + )} +
+ ); +}; - return ( -
- {dbConnection && - -

Showing History in {dbConnection.name}

-
- - Loading... -

- } - endMessage={ -

- You have seen it all! -

- } - scrollableTarget="maincontent" - > - - - {dbQueryLogs.map((log) => { - return ( - - - - - ) - })} - -
- {log.query} - - {dateformat(log.createdAt, "mmm dd, yyyy HH:MM:ss")} -
-
-
- } -
- ) -} - - -export default DBHistoryFragment \ No newline at end of file +export default DBHistoryFragment; From 3d07d1cd4b51b7c35a792ce16fec67a38074a829 Mon Sep 17 00:00:00 2001 From: Sanyam Jain Date: Sun, 16 Apr 2023 18:31:39 +0530 Subject: [PATCH 09/10] remove changes by prettier vs-code extension --- .../src/components/dbfragments/history.tsx | 182 +++++++++--------- 1 file changed, 87 insertions(+), 95 deletions(-) diff --git a/frontend/src/components/dbfragments/history.tsx b/frontend/src/components/dbfragments/history.tsx index 8adca9a8..65633297 100644 --- a/frontend/src/components/dbfragments/history.tsx +++ b/frontend/src/components/dbfragments/history.tsx @@ -1,106 +1,98 @@ -import { useContext, useEffect } from "react"; -import { DBConnection, Tab } from "../../data/models"; -import { selectDBConnection } from "../../redux/dbConnectionSlice"; -import { useAppDispatch, useAppSelector } from "../../redux/hooks"; -import toast from "react-hot-toast"; -import InfiniteScroll from "react-infinite-scroll-component"; -import dateformat from "dateformat"; -import { - getDBQueryLogs, - reset, - selectDBQueryLogs, - selectDBQueryLogsNext, -} from "../../redux/dbHistorySlice"; -import TabContext from "../layouts/tabcontext"; +import { useContext, useEffect } from 'react' +import { DBConnection, Tab } from '../../data/models' +import { selectDBConnection } from '../../redux/dbConnectionSlice' +import { useAppDispatch, useAppSelector } from '../../redux/hooks' +import toast from 'react-hot-toast' +import InfiniteScroll from 'react-infinite-scroll-component' +import dateformat from 'dateformat' +import { getDBQueryLogs, reset, selectDBQueryLogs, selectDBQueryLogsNext } from '../../redux/dbHistorySlice' +import TabContext from '../layouts/tabcontext' -type DBHistoryPropType = {}; -const DBHistoryFragment = ({}: DBHistoryPropType) => { - const dispatch = useAppDispatch(); +type DBHistoryPropType = { +} - const currentTab: Tab = useContext(TabContext)!; +const DBHistoryFragment = ({ }: DBHistoryPropType) => { - const dbConnection: DBConnection | undefined = - useAppSelector(selectDBConnection); - const dbQueryLogs = useAppSelector(selectDBQueryLogs); - const dbQueryLogsNext = useAppSelector(selectDBQueryLogsNext); + const dispatch = useAppDispatch() - useEffect(() => { - if (dbConnection) { - (async () => { - dispatch(reset()); - })(); - fetchDBQueryLogs(); - } - }, [dispatch, dbConnection]); + const currentTab: Tab = useContext(TabContext)! - const fetchDBQueryLogs = async () => { - const result = await dispatch( - getDBQueryLogs({ dbConnId: dbConnection!.id }) - ).unwrap(); - if (!result.success) { - toast.error(result.error!); - } - }; + const dbConnection: DBConnection | undefined = useAppSelector(selectDBConnection) + const dbQueryLogs = useAppSelector(selectDBQueryLogs) + const dbQueryLogsNext = useAppSelector(selectDBQueryLogsNext) - function refreshHandler() { + useEffect(() => { + if (dbConnection) { + (async () => { + dispatch(reset()) + })() + fetchDBQueryLogs() + } + }, [dispatch, dbConnection]) + + const fetchDBQueryLogs = async () => { + const result = await dispatch(getDBQueryLogs({ dbConnId: dbConnection!.id })).unwrap() + if (!result.success) { + toast.error(result.error!) + } + } + + function refreshHandler() { dispatch(reset()); fetchDBQueryLogs(); - } + } - return ( -
- {dbConnection && ( - <> -
-

Showing History in {dbConnection.name}

- -
-
- Loading...

} - endMessage={ -

- You have seen it all! -

- } - scrollableTarget="maincontent" - > - - - {dbQueryLogs.map((log) => ( - - - - - ))} - -
- {log.query} - - {dateformat( - log.createdAt, - "mmm dd, yyyy HH:MM:ss" - )} -
-
- - )} -
- ); -}; + return ( +
+ {dbConnection && + <> +
+

Showing History in {dbConnection.name}

+ +
+
+ + Loading... +

+ } + endMessage={ +

+ You have seen it all! +

+ } + scrollableTarget="maincontent" + > + + + {dbQueryLogs.map((log) => ( + + + + + ) + )} + +
+ {log.query} + + {dateformat(log.createdAt, "mmm dd, yyyy HH:MM:ss")} +
+
+ + } +
+ ) +} -export default DBHistoryFragment; +export default DBHistoryFragment From ae3e36483831dcbd172606bc5ff1be63dc8eee6d Mon Sep 17 00:00:00 2001 From: Sanyam Jain Date: Sun, 16 Apr 2023 20:38:20 +0530 Subject: [PATCH 10/10] improve code quality remove unnecessary constant, interfaced, types and unused imports --- .../src/components/dbfragments/cheatsheet/cheatsheet.tsx | 1 - frontend/src/components/dbfragments/query.tsx | 7 ++----- frontend/src/components/dbfragments/showmodel.tsx | 6 +----- frontend/src/components/dbfragments/table/addmodal.tsx | 2 +- frontend/src/components/layouts/applayout.tsx | 8 ++------ frontend/src/components/layouts/footer.tsx | 5 +---- frontend/src/components/layouts/header.tsx | 6 +----- frontend/src/constants.ts | 2 +- frontend/src/redux/allDBConnectionsSlice.ts | 4 ++-- frontend/src/redux/projectsSlice.ts | 2 +- 10 files changed, 12 insertions(+), 31 deletions(-) diff --git a/frontend/src/components/dbfragments/cheatsheet/cheatsheet.tsx b/frontend/src/components/dbfragments/cheatsheet/cheatsheet.tsx index c407b828..14fc19e2 100644 --- a/frontend/src/components/dbfragments/cheatsheet/cheatsheet.tsx +++ b/frontend/src/components/dbfragments/cheatsheet/cheatsheet.tsx @@ -1,4 +1,3 @@ -import styles from './cheatsheet.module.scss' import React, { useRef, useState } from 'react' import { DBConnType } from '../../../data/defaults' import CheatsheetCommand from './command' diff --git a/frontend/src/components/dbfragments/query.tsx b/frontend/src/components/dbfragments/query.tsx index 438a9270..03e3ee76 100644 --- a/frontend/src/components/dbfragments/query.tsx +++ b/frontend/src/components/dbfragments/query.tsx @@ -1,7 +1,7 @@ import styles from './query.module.scss' import React, { useContext, useEffect, useState } from 'react' import toast from 'react-hot-toast' -import { DBConnection, DBQuery, DBQueryData, DBQueryResult, Tab } from '../../data/models' +import { DBConnection, DBQueryData, DBQueryResult, Tab } from '../../data/models' import QueryEditor from './queryeditor/queryeditor' import { selectDBConnection } from '../../redux/dbConnectionSlice' import { useAppDispatch, useAppSelector } from '../../redux/hooks' @@ -14,10 +14,7 @@ import { closeTab, updateActiveTab } from '../../redux/tabsSlice' import TabContext from '../layouts/tabcontext' -type DBQueryPropType = { -} - -const DBQueryFragment = (_: DBQueryPropType) => { +const DBQueryFragment = () => { const dispatch = useAppDispatch() diff --git a/frontend/src/components/dbfragments/showmodel.tsx b/frontend/src/components/dbfragments/showmodel.tsx index a6bc5c7f..c4bde6e0 100644 --- a/frontend/src/components/dbfragments/showmodel.tsx +++ b/frontend/src/components/dbfragments/showmodel.tsx @@ -5,11 +5,7 @@ import { useAppSelector } from '../../redux/hooks' import TabContext from '../layouts/tabcontext' import DataModel from './datamodel/datamodel' -type DBShowModelPropType = { - -} - -const DBShowModelFragment = (_: DBShowModelPropType) => { +const DBShowModelFragment = () => { const dbConnection: DBConnection | undefined = useAppSelector(selectDBConnection) const currentTab: Tab = useContext(TabContext)! diff --git a/frontend/src/components/dbfragments/table/addmodal.tsx b/frontend/src/components/dbfragments/table/addmodal.tsx index 7a2477b3..d26874f0 100644 --- a/frontend/src/components/dbfragments/table/addmodal.tsx +++ b/frontend/src/components/dbfragments/table/addmodal.tsx @@ -2,7 +2,7 @@ import styles from './table.module.scss' import React, { useContext, useState } from 'react' import { ApiResult, AddDataResponse, DBConnection, DBQueryData, Tab } from '../../../data/models' import toast from 'react-hot-toast' -import { useAppDispatch, useAppSelector } from '../../../redux/hooks' +import { useAppDispatch } from '../../../redux/hooks' import { addDBData, setQueryData } from '../../../redux/dataModelSlice' import { DBConnType } from '../../../data/defaults' import TabContext from '../../layouts/tabcontext' diff --git a/frontend/src/components/layouts/applayout.tsx b/frontend/src/components/layouts/applayout.tsx index 99602565..0e42c132 100644 --- a/frontend/src/components/layouts/applayout.tsx +++ b/frontend/src/components/layouts/applayout.tsx @@ -1,4 +1,4 @@ -import React, { FunctionComponent, useEffect } from 'react' +import React, { FunctionComponent } from 'react' import Header from './header' import Footer from './footer' import Sidebar from './sidebar' @@ -7,11 +7,7 @@ import { useAppSelector } from '../../redux/hooks'; import { selectIsShowingSidebar } from '../../redux/configSlice'; import TabsBar from './tabsbar'; -type PageLayoutPropType = { - -} - -const AppLayout: FunctionComponent = () => { +const AppLayout: FunctionComponent = () => { const location = useLocation() diff --git a/frontend/src/components/layouts/footer.tsx b/frontend/src/components/layouts/footer.tsx index 0f598a56..d4efbc38 100644 --- a/frontend/src/components/layouts/footer.tsx +++ b/frontend/src/components/layouts/footer.tsx @@ -5,10 +5,7 @@ import Constants from '../../constants' import { useEffect } from 'react' import { checkConnection, selectDBConnection, selectIsDBConnected, getDBDataModels, resetDBDataModels } from '../../redux/dbConnectionSlice' - -type FooterPropType = {} - -const Footer = (_: FooterPropType) => { +const Footer = () => { const navigate = useNavigate() const location = useLocation() diff --git a/frontend/src/components/layouts/header.tsx b/frontend/src/components/layouts/header.tsx index 23339aab..5fc8c0cd 100644 --- a/frontend/src/components/layouts/header.tsx +++ b/frontend/src/components/layouts/header.tsx @@ -10,11 +10,7 @@ import { selectProjects } from '../../redux/projectsSlice' import { selectDBConnection } from '../../redux/dbConnectionSlice' import utils from '../../lib/utils' -declare var window: any; - -type HeaderPropType = {} - -const Header = (_: HeaderPropType) => { +const Header = () => { let location = useLocation() const navigate = useNavigate() diff --git a/frontend/src/constants.ts b/frontend/src/constants.ts index 7f2b9b05..3bcbb51c 100644 --- a/frontend/src/constants.ts +++ b/frontend/src/constants.ts @@ -15,7 +15,7 @@ interface ConstantsType { } declare global { - var CONFIG: { + const CONFIG: { API_HOST: string; } } diff --git a/frontend/src/redux/allDBConnectionsSlice.ts b/frontend/src/redux/allDBConnectionsSlice.ts index dc29128a..86d3f402 100644 --- a/frontend/src/redux/allDBConnectionsSlice.ts +++ b/frontend/src/redux/allDBConnectionsSlice.ts @@ -42,7 +42,7 @@ export const getAllDBConnections = createAsyncThunk( export const addNewDBConn = createAsyncThunk( 'allDBConnections/addNewDBConn', - async (payload: AddDBConnPayload, { rejectWithValue, getState }: any) => { + async (payload: AddDBConnPayload, { rejectWithValue }: any) => { const response = await eventService.addNewDBConn(payload) if (response.success) { const dbConn = response.success ? response.data : null @@ -60,7 +60,7 @@ export const allDBConnectionSlice = createSlice({ name: 'allDBConnections', initialState, reducers: { - reset: (state) => initialState + reset: () => initialState }, extraReducers: (builder) => { builder diff --git a/frontend/src/redux/projectsSlice.ts b/frontend/src/redux/projectsSlice.ts index d9a92bd2..1172b258 100644 --- a/frontend/src/redux/projectsSlice.ts +++ b/frontend/src/redux/projectsSlice.ts @@ -113,7 +113,7 @@ export const projectsSlice = createSlice({ name: 'projects', initialState, reducers: { - reset: (state) => initialState + reset: () => initialState }, extraReducers: (builder) => { builder