Skip to content

Commit

Permalink
more smart table name
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Nov 19, 2021
1 parent 6559d10 commit 22dcff7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ if err != nil {
panic(err)
}
```
##### Custom queries
#### Custom queries
Sometimes you need custom queries but you want the power of orm with you.
##### Binding query result to model
```go
var models []*Model
err = modelRepository.Bind(qb.NewSelect().
Expand Down
9 changes: 1 addition & 8 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,11 @@ type AddressContent struct {
Content string `orm:"name=content"`
}

func (a AddressContent) Table() string {
return "address_contents"
}

type Address struct {
UserID string `orm:"name=user_id"`
AddressContent AddressContent `orm:"in_rel=true with=address_content has=one left=id right=address_id"`
}

func (a Address) Table() string {
return "addresses"
}
func TestExampleRepositoriesWithRelationHasOne(t *testing.T) {
type User struct {
Id int64 `orm:"name=id pk=true"`
Expand All @@ -99,4 +92,4 @@ func TestExampleRepositoriesWithRelationHasOne(t *testing.T) {
assert.Equal(t, "amirreza", firstUser.Name)
assert.Equal(t, "ahvaz", firstUser.Address.AddressContent.Content)

}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/gertd/go-pluralize v0.1.7 // indirect
github.com/iancoleman/strcase v0.2.0
github.com/lib/pq v1.10.2
github.com/stretchr/testify v1.7.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20O
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gertd/go-pluralize v0.1.7 h1:RgvJTJ5W7olOoAks97BOwOlekBFsLEyh00W48Z6ZEZY=
github.com/gertd/go-pluralize v0.1.7/go.mod h1:O4eNeeIf91MHh1GJ2I47DNtaesm66NYvjYgAahcqSDQ=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
5 changes: 4 additions & 1 deletion obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package orm

import (
"fmt"
"github.com/gertd/go-pluralize"
"reflect"
"strings"
"unsafe"
Expand Down Expand Up @@ -66,8 +67,10 @@ func tableName(v interface{}) string {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}

parts := strings.Split(t.Name(), ".")
return strings.ToLower(parts[len(parts)-1]) + "s"
name := parts[len(parts)-1]
return strcase.ToSnake(pluralize.NewClient().Plural(name))
}

func (r *Repository) pkName(v interface{}) string {
Expand Down

0 comments on commit 22dcff7

Please sign in to comment.