Browse Source

添加支持url去除表前缀的支持

lxg 3 years ago
parent
commit
b2976558aa
2 changed files with 13 additions and 7 deletions
  1. 6 2
      entity.go
  2. 7 5
      options.go

+ 6 - 2
entity.go

@@ -720,8 +720,12 @@ func newEntity(model Model, opts *Options) *Entity {
 	}
 	entity.lruCache, _ = lru.New(50)
 	entity.reflectType = entity.reflectValue.Type()
-	entity.singularName = inflector.Singularize(model.TableName())
-	entity.pluralizeName = inflector.Pluralize(model.TableName())
+	tableName := model.TableName()
+	if opts.RemoveTablePrefix && opts.TablePrefix != "" {
+		tableName = strings.TrimLeft(tableName, opts.TablePrefix)
+	}
+	entity.singularName = inflector.Singularize(tableName)
+	entity.pluralizeName = inflector.Pluralize(tableName)
 
 	val := reflect.New(entity.reflectType).Interface()
 

+ 7 - 5
options.go

@@ -7,11 +7,13 @@ import (
 )
 
 type Options struct {
-	EnableNamespace bool
-	DB              *gorm.DB
-	Prefix          string
-	Formatter       *Formatter
-	Middleware      []http.Middleware
+	EnableNamespace   bool
+	DB                *gorm.DB
+	Prefix            string
+	TablePrefix       string
+	RemoveTablePrefix bool
+	Formatter         *Formatter
+	Middleware        []http.Middleware
 }
 
 type Option func(o *Options)