Skip to content

Commit

Permalink
refactor: Update models to use DatabaseV2 instead of DataSourceV2
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Aug 5, 2024
1 parent e57655c commit 421a069
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
30 changes: 14 additions & 16 deletions core/controllers/database_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ import (
func PostDatabase(c *gin.Context) {
// data source
var payload struct {
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Host string `json:"host"`
Port int `json:"port"`
Url string `json:"url"`
Hosts []string `json:"hosts"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"-,omitempty"`
ConnectType string `json:"connect_type"`
Status string `json:"status"`
Error string `json:"error"`
Extra map[string]string `json:"extra,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Host string `json:"host"`
Port int `json:"port"`
Url string `json:"url"`
Hosts []string `json:"hosts"`
Database string `json:"database"`
Username string `json:"username"`
Password string `json:"-,omitempty"`
ConnectType string `json:"connect_type"`
Status string `json:"status"`
Error string `json:"error"`
}
if err := c.ShouldBindJSON(&payload); err != nil {
HandleErrorBadRequest(c, err)
Expand All @@ -41,14 +40,13 @@ func PostDatabase(c *gin.Context) {
Description: payload.Description,
Host: payload.Host,
Port: payload.Port,
Url: payload.Url,
URI: payload.Url,
Database: payload.Database,
Username: payload.Username,
Password: payload.Password,
ConnectType: payload.ConnectType,
Status: payload.Status,
Error: payload.Error,
Extra: payload.Extra,
}
dataSource.SetCreated(u.Id)
dataSource.SetUpdated(u.Id)
Expand Down
49 changes: 35 additions & 14 deletions core/models/models/v2/database_v2.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
package models

import "time"

type DatabaseV2 struct {
any `collection:"databases"`
BaseModelV2[DatabaseV2] `bson:",inline"`
Name string `json:"name" bson:"name"`
DataSource string `json:"data_source" bson:"data_source"`
Type string `json:"type" bson:"type"`
Description string `json:"description" bson:"description"`
Host string `json:"host" bson:"host"`
Port int `json:"port" bson:"port"`
Url string `json:"url" bson:"url"`
Database string `json:"database" bson:"database"`
Username string `json:"username" bson:"username"`
Password string `json:"-,omitempty" bson:"password"`
ConnectType string `json:"connect_type" bson:"connect_type"`
Status string `json:"status" bson:"status"`
Error string `json:"error" bson:"error"`
Extra map[string]string `json:"extra,omitempty" bson:"extra,omitempty"`
Name string `json:"name" bson:"name"`
DataSource string `json:"data_source" bson:"data_source"`
Type string `json:"type" bson:"type"`
Description string `json:"description" bson:"description"`
Host string `json:"host" bson:"host"`
Port int `json:"port" bson:"port"`
URI string `json:"uri,omitempty" bson:"uri,omitempty"`
Database string `json:"database,omitempty" bson:"database,omitempty"`
Username string `json:"username,omitempty" bson:"username,omitempty"`
Password string `json:"-,omitempty" bson:"password,omitempty"`
ConnectType string `json:"connect_type,omitempty" bson:"connect_type,omitempty"`
Status string `json:"status" bson:"status"`
Error string `json:"error" bson:"error"`
Active bool `json:"active" bson:"active"`
ActiveAt time.Time `json:"active_ts" bson:"active_ts"`

MongoParams *struct {
AuthSource string `json:"auth_source,omitempty" bson:"auth_source,omitempty"`
AuthMechanism string `json:"auth_mechanism,omitempty" bson:"auth_mechanism,omitempty"`
} `json:"mongo_params,omitempty" bson:"mongo_params,omitempty"`
PostgresParams *struct {
SSLMode string `json:"ssl_mode,omitempty" bson:"ssl_mode,omitempty"`
} `json:"postgres_params,omitempty" bson:"postgres_params,omitempty"`
SnowflakeParams *struct {
Account string `json:"account,omitempty" bson:"account,omitempty"`
Schema string `json:"schema,omitempty" bson:"schema,omitempty"`
Warehouse string `json:"warehouse,omitempty" bson:"warehouse,omitempty"`
Role string `json:"role,omitempty" bson:"role,omitempty"`
} `json:"snowflake_params,omitempty" bson:"snowflake_params,omitempty"`
CassandraParams *struct {
Keyspace string `json:"keyspace,omitempty" bson:"keyspace,omitempty"`
}
HiveParams *struct {
Auth string `json:"auth,omitempty" bson:"auth,omitempty"`
}
RedisParams *struct {
DB int `json:"db,omitempty" bson:"db,omitempty"`
}
}
17 changes: 1 addition & 16 deletions core/utils/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,13 @@ func getMongoClientV2(ctx context.Context, ds *models2.DatabaseV2) (c *mongo2.Cl
// options
var opts []mongo.ClientOption
opts = append(opts, mongo.WithContext(ctx))
opts = append(opts, mongo.WithUri(ds.Url))
opts = append(opts, mongo.WithUri(ds.URI))
opts = append(opts, mongo.WithHost(ds.Host))
opts = append(opts, mongo.WithPort(ds.Port))
opts = append(opts, mongo.WithDb(ds.Database))
opts = append(opts, mongo.WithUsername(ds.Username))
opts = append(opts, mongo.WithPassword(ds.Password))

// extra
if ds.Extra != nil {
// auth source
authSource, ok := ds.Extra["auth_source"]
if ok {
opts = append(opts, mongo.WithAuthSource(authSource))
}

// auth mechanism
authMechanism, ok := ds.Extra["auth_mechanism"]
if ok {
opts = append(opts, mongo.WithAuthMechanism(authMechanism))
}
}

// client
return mongo.GetMongoClient(opts...)
}

0 comments on commit 421a069

Please sign in to comment.