add targets to init

This commit is contained in:
Nam Huynh 2021-08-17 14:29:47 +07:00
parent 335eaf02d4
commit 46d210ad26
3 changed files with 5 additions and 21 deletions

View File

@ -17,6 +17,8 @@ type MongoDBConfig struct {
type Config struct {
// Source of server, e.g: selly
Source string
// Targets: staff, article, ...
Targets []string
// MongoDB config, for save documents
MongoDB MongoDBConfig
}
@ -31,8 +33,8 @@ var s *Service
// NewInstance ...
func NewInstance(config Config) error {
if config.Source == "" || config.MongoDB.Host == "" {
return errors.New("please provide all necessary information: source, mongodb")
if config.Source == "" || len(config.Targets) == 0 || config.MongoDB.Host == "" {
return errors.New("please provide all necessary information: source, targets, mongodb")
}
// Connect MongoDB

View File

@ -10,15 +10,3 @@ const (
ActionUpdate = "update"
ActionUpdatePermissions = "update-permissions"
)
// List targets
const (
TargetSellyStaff = "staffs"
TargetSellyStaffRole = "staff-roles"
)
// SellyTargets ...
var SellyTargets = []string{
TargetSellyStaff,
TargetSellyStaffRole,
}

View File

@ -5,17 +5,11 @@ import (
)
func (s Service) indexDB() {
// Get list targets
var targets = make([]string, 0)
if s.Source == SourceSelly {
targets = SellyTargets
}
// Index key
commonIndex := mongodb.NewIndexKey("source", "target", "targetId")
// Index all allowed sources
for _, target := range targets {
for _, target := range s.Targets {
mongodb.CreateIndex(getColName(target), commonIndex)
}
}