refactor: Update models to use DatabaseV2 instead of DataSourceV2

This commit is contained in:
Marvin Zhang
2024-08-05 13:14:57 +08:00
parent f38b11e7ed
commit fc28a742de
21 changed files with 44 additions and 54 deletions

View File

@@ -16,13 +16,13 @@ const (
)
const (
DefaultMongoPort = "27017"
DefaultMysqlPort = "3306"
DefaultPostgresqlPort = "5432"
DefaultMssqlPort = "1433"
DefaultCockroachdbPort = "26257"
DefaultElasticsearchPort = "9200"
DefaultKafkaPort = "9092"
DefaultMongoPort = 27017
DefaultMysqlPort = 3306
DefaultPostgresqlPort = 5432
DefaultMssqlPort = 1433
DefaultCockroachdbPort = 26257
DefaultElasticsearchPort = 9200
DefaultKafkaPort = 9092
)
const (

View File

@@ -16,7 +16,7 @@ func PostDatabase(c *gin.Context) {
Type string `json:"type"`
Description string `json:"description"`
Host string `json:"host"`
Port string `json:"port"`
Port int `json:"port"`
Url string `json:"url"`
Hosts []string `json:"hosts"`
Database string `json:"database"`
@@ -42,7 +42,6 @@ func PostDatabase(c *gin.Context) {
Host: payload.Host,
Port: payload.Port,
Url: payload.Url,
Hosts: payload.Hosts,
Database: payload.Database,
Username: payload.Username,
Password: payload.Password,

View File

@@ -56,18 +56,6 @@ func InitRoutes(app *gin.Engine) (err error) {
groups := NewRouterGroups(app)
RegisterController(groups.AuthGroup, "/data/collections", NewControllerV2[models2.DataCollectionV2]())
RegisterController(groups.AuthGroup, "/databases", NewControllerV2[models2.DatabaseV2]([]Action{
{
Method: http.MethodPost,
Path: "",
HandlerFunc: PostDatabase,
},
{
Method: http.MethodPut,
Path: "/:id",
HandlerFunc: PutDatabaseById,
},
}...))
RegisterController(groups.AuthGroup, "/environments", NewControllerV2[models2.EnvironmentV2]())
RegisterController(groups.AuthGroup, "/nodes", NewControllerV2[models2.NodeV2]())
RegisterController(groups.AuthGroup, "/projects", NewControllerV2[models2.ProjectV2]([]Action{

View File

@@ -38,7 +38,7 @@ func NewDataSourceCockroachdbService(colId primitive.ObjectID, dsId primitive.Ob
if svc.ds.Host == "" {
svc.ds.Host = constants.DefaultHost
}
if svc.ds.Port == "" {
if svc.ds.Port == 0 {
svc.ds.Port = constants.DefaultCockroachdbPort
}

View File

@@ -179,7 +179,7 @@ func NewDataSourceElasticsearchService(colId primitive.ObjectID, dsId primitive.
if svc.ds.Host == "" {
svc.ds.Host = constants.DefaultHost
}
if svc.ds.Port == "" {
if svc.ds.Port == 0 {
svc.ds.Port = constants.DefaultElasticsearchPort
}

View File

@@ -81,7 +81,7 @@ func NewDataSourceKafkaService(colId primitive.ObjectID, dsId primitive.ObjectID
if svc.ds.Host == "" {
svc.ds.Host = constants.DefaultHost
}
if svc.ds.Port == "" {
if svc.ds.Port == 0 {
svc.ds.Port = constants.DefaultKafkaPort
}

View File

@@ -71,7 +71,7 @@ func NewDataSourceMongoService(colId primitive.ObjectID, dsId primitive.ObjectID
if svc.ds.Host == "" {
svc.ds.Host = constants.DefaultHost
}
if svc.ds.Port == "" {
if svc.ds.Port == 0 {
svc.ds.Port = constants.DefaultMongoPort
}

View File

@@ -39,7 +39,7 @@ func NewDataSourceMssqlService(colId primitive.ObjectID, dsId primitive.ObjectID
if svc.ds.Host == "" {
svc.ds.Host = constants.DefaultHost
}
if svc.ds.Port == "" {
if svc.ds.Port == 0 {
svc.ds.Port = constants.DefaultMssqlPort
}

View File

@@ -38,7 +38,7 @@ func NewDataSourceMysqlService(colId primitive.ObjectID, dsId primitive.ObjectID
if svc.ds.Host == "" {
svc.ds.Host = constants.DefaultHost
}
if svc.ds.Port == "" {
if svc.ds.Port == 0 {
svc.ds.Port = constants.DefaultMysqlPort
}

View File

@@ -39,7 +39,7 @@ func NewDataSourcePostgresqlService(colId primitive.ObjectID, dsId primitive.Obj
if svc.ds.Host == "" {
svc.ds.Host = constants.DefaultHost
}
if svc.ds.Port == "" {
if svc.ds.Port == 0 {
svc.ds.Port = constants.DefaultPostgresqlPort
}

View File

@@ -11,7 +11,7 @@ type DataSource struct {
Type string `json:"type" bson:"type"`
Description string `json:"description" bson:"description"`
Host string `json:"host" bson:"host"`
Port string `json:"port" bson:"port"`
Port int `json:"port" bson:"port"`
Url string `json:"url" bson:"url"`
Hosts []string `json:"hosts" bson:"hosts"`
Database string `json:"database" bson:"database"`

View File

@@ -4,12 +4,12 @@ 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 string `json:"port" bson:"port"`
Port int `json:"port" bson:"port"`
Url string `json:"url" bson:"url"`
Hosts []string `json:"hosts" bson:"hosts"`
Database string `json:"database" bson:"database"`
Username string `json:"username" bson:"username"`
Password string `json:"-,omitempty" bson:"password"`
@@ -17,4 +17,9 @@ type DatabaseV2 struct {
Status string `json:"status" bson:"status"`
Error string `json:"error" bson:"error"`
Extra map[string]string `json:"extra,omitempty" bson:"extra,omitempty"`
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"`
}

View File

@@ -28,7 +28,7 @@ func getCockroachdbSession(ctx context.Context, ds *models.DataSource) (s db.Ses
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultCockroachdbPort
}
@@ -73,7 +73,7 @@ func getCockroachdbSessionV2(ctx context.Context, ds *models2.DatabaseV2) (s db.
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultCockroachdbPort
}

View File

@@ -34,7 +34,7 @@ func getElasticsearchClient(ctx context.Context, ds *models.DataSource) (c *elas
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultElasticsearchPort
}
@@ -97,7 +97,7 @@ func getElasticsearchClientV2(ctx context.Context, ds *models2.DatabaseV2) (c *e
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultElasticsearchPort
}

View File

@@ -27,7 +27,7 @@ func getKafkaConnection(ctx context.Context, ds *models.DataSource) (c *kafka.Co
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultKafkaPort
}
@@ -54,7 +54,7 @@ func getKafkaConnectionV2(ctx context.Context, ds *models2.DatabaseV2) (c *kafka
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultKafkaPort
}

View File

@@ -66,7 +66,7 @@ func getMongoClient(ctx context.Context, ds *models.DataSource) (c *mongo2.Clien
if ds.Host == "" {
ds.Host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
ds.Port = constants.DefaultMongoPort
}
@@ -79,7 +79,6 @@ func getMongoClient(ctx context.Context, ds *models.DataSource) (c *mongo2.Clien
opts = append(opts, mongo.WithDb(ds.Database))
opts = append(opts, mongo.WithUsername(ds.Username))
opts = append(opts, mongo.WithPassword(ds.Password))
opts = append(opts, mongo.WithHosts(ds.Hosts))
// extra
if ds.Extra != nil {
@@ -105,7 +104,7 @@ func getMongoClientV2(ctx context.Context, ds *models2.DatabaseV2) (c *mongo2.Cl
if ds.Host == "" {
ds.Host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
ds.Port = constants.DefaultMongoPort
}
@@ -118,7 +117,6 @@ func getMongoClientV2(ctx context.Context, ds *models2.DatabaseV2) (c *mongo2.Cl
opts = append(opts, mongo.WithDb(ds.Database))
opts = append(opts, mongo.WithUsername(ds.Username))
opts = append(opts, mongo.WithPassword(ds.Password))
opts = append(opts, mongo.WithHosts(ds.Hosts))
// extra
if ds.Extra != nil {

View File

@@ -28,7 +28,7 @@ func getMssqlSession(ctx context.Context, ds *models.DataSource) (s db.Session,
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultMssqlPort
}
@@ -73,7 +73,7 @@ func getMssqlSessionV2(ctx context.Context, ds *models2.DatabaseV2) (s db.Sessio
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultMssqlPort
}

View File

@@ -28,7 +28,7 @@ func getMysqlSession(ctx context.Context, ds *models.DataSource) (s db.Session,
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultMysqlPort
}
@@ -73,7 +73,7 @@ func getMysqlSessionV2(ctx context.Context, ds *models2.DatabaseV2) (s db.Sessio
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultMysqlPort
}

View File

@@ -28,7 +28,7 @@ func getPostgresqlSession(ctx context.Context, ds *models.DataSource) (s db.Sess
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultPostgresqlPort
}
@@ -73,7 +73,7 @@ func getPostgresqlSessionV2(ctx context.Context, ds *models2.DatabaseV2) (s db.S
if ds.Host == "" {
host = constants.DefaultHost
}
if ds.Port == "" {
if ds.Port == 0 {
port = constants.DefaultPostgresqlPort
}

View File

@@ -33,10 +33,10 @@ func GetMongoClient(opts ...ClientOption) (c *mongo.Client, err error) {
_opts.Host = "localhost"
}
}
if _opts.Port == "" {
_opts.Port = viper.GetString("mongo.port")
if _opts.Port == "" {
_opts.Port = "27017"
if _opts.Port == 0 {
_opts.Port = viper.GetInt("mongo.port")
if _opts.Port == 0 {
_opts.Port = 27017
}
}
if _opts.Db == "" {
@@ -123,7 +123,7 @@ func newMongoClient(ctx context.Context, _opts *ClientOptions) (c *mongo.Client,
mongoOpts.SetHosts(_opts.Hosts)
} else {
// hosts are unset
mongoOpts.ApplyURI(fmt.Sprintf("mongodb://%s:%s/%s", _opts.Host, _opts.Port, _opts.Db))
mongoOpts.ApplyURI(fmt.Sprintf("mongodb://%s:%d/%s", _opts.Host, _opts.Port, _opts.Db))
}
}

View File

@@ -8,7 +8,7 @@ type ClientOptions struct {
Context context.Context
Uri string
Host string
Port string
Port int
Db string
Hosts []string
Username string
@@ -36,7 +36,7 @@ func WithHost(value string) ClientOption {
}
}
func WithPort(value string) ClientOption {
func WithPort(value int) ClientOption {
return func(options *ClientOptions) {
options.Port = value
}