Skip to content

Commit

Permalink
remove tags, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Mar 21, 2022
1 parent d105776 commit b560a0b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 67 deletions.
47 changes: 34 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ As you see, our user entity is nothing else than a simple struct and two methods
Entities in GoLobby ORM are implementations of `Entity` interface, which defines two methods:
- ConfigureEntity: configures table, fields, and also relations to other entities.
#### Conventions
We have standard conventions and we encourage you to follow, but if you want to change them for any reason you can use `Field` method to customize how ORM
inferres meta data from your `Entity`.

##### Column names
GoLobby ORM for each struct field(except slice, arrays, maps, and other nested structs) assumes a respective column named using snake case syntax.
If you want a custom column name, you should specify it in `ConfigureEntity` method using `Field()` method.
```go
package main

type User struct {
Name string
}

func (u User) ConfigureEntity(e *orm.EntityConfigurator) {
e.Field("Name").ColumnName("custom_name_for_column")

e.Table("users")
}
```
##### Timestamps
for having `created_at`, `updated_at`, `deleted_at` timestamps in your entities you can embed `orm.Timestamps` struct in your entity,
```go
Expand All @@ -159,29 +178,31 @@ type User struct {
Name string
LastName string
Email string
MyCreatedAt sql.NullTime `orm:"created_at=true"`
MyUpdatedAt sql.NullTime `orm:"updated_at=true"`
MyDeletedAt sql.NullTime `orm:"deleted_at=true"`
MyCreatedAt sql.NullTime
MyUpdatedAt sql.NullTime
MyDeletedAt sql.NullTime
}
```
You can use the `orm` struct tag, and there is a key for each timestamp that you can use on any time-compatible struct field.
##### Column names
GoLobby ORM for each struct field(except slice, arrays, maps, and other nested structs) assumes a respective column named using snake case syntax.
If you want a custom column name, you should specify it in the entity struct.
```go
package main
func (u User) ConfigureEntity(e *orm.EntityConfigurator) {
e.Field("MyCreatedAt").IsCreatedAt() // this will make ORM to use MyCreatedAt as created_at column
e.Field("MyUpdatedAt").IsUpdatedAt() // this will make ORM to use MyUpdatedAt as created_at column
e.Field("MyDeletedAt").IsDeletedAt() // this will make ORM to use MyDeletedAt as created_at column

type User struct {
Name string `orm:"column=username"` // now this field will be mapped to `username` column in sql database.
e.Table("users")
}
```
As always you use `Field` method for configuring how ORM behaves to your struct field.

##### Primary Key
GoLobby ORM assumes that each entity has a primary key named `id`; if you want a custom primary key called, you need to specify it in entity struct.
```go
package main

type User struct {
PK int64 `orm:"pk=true"`
PK int64
}
func (u User) ConfigureEntity(e *orm.EntityConfigurator) {
e.Field("PK").IsPrimaryKey() // this will make ORM use PK field as primary key.
e.Table("users")
}
```

Expand Down
39 changes: 6 additions & 33 deletions configurators.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,8 @@ func (ec *EntityConfigurator) BelongsToMany(owner Entity, config BelongsToManyCo
return ec
}

type FieldsConfigurator struct {
ec *EntityConfigurator
}

func (ec *EntityConfigurator) Fields() *FieldsConfigurator {
return &FieldsConfigurator{ec: ec}
}

type FieldConfigurator struct {
fieldName string
fc *FieldsConfigurator
nullable sql.NullBool
primaryKey bool
column string
Expand All @@ -162,20 +153,11 @@ type FieldConfigurator struct {
isDeletedAt bool
}

// func (fc *FieldConfigurator) CanBeNull() *FieldConfigurator {
// fc.nullable = sql.NullBool{
// Bool: true,
// Valid: true,
// }
// return fc
//}
// func (fc *FieldConfigurator) CannotBeNull() *FieldConfigurator {
// fc.nullable = sql.NullBool{
// Bool: false,
// Valid: true,
// }
// return fc
//}
func (ec *EntityConfigurator) Field(name string) *FieldConfigurator {
cc := &FieldConfigurator{fieldName: name}
ec.columnConstraints = append(ec.columnConstraints, cc)
return cc
}

func (fc *FieldConfigurator) IsPrimaryKey() *FieldConfigurator {
fc.primaryKey = true
Expand All @@ -200,13 +182,4 @@ func (fc *FieldConfigurator) IsDeletedAt() *FieldConfigurator {
func (fc *FieldConfigurator) ColumnName(name string) *FieldConfigurator {
fc.column = name
return fc
}

func (fc *FieldsConfigurator) Field(name string) *FieldConfigurator {
cc := &FieldConfigurator{fc: fc, fieldName: name}
fc.ec.columnConstraints = append(fc.ec.columnConstraints, cc)
return cc
}
func (fc *FieldConfigurator) Also() *FieldsConfigurator {
return fc.fc
}
}
20 changes: 9 additions & 11 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,32 @@ func getFieldConfiguratorFor(fieldConfigurators []*FieldConfigurator, name strin
}

func fieldMetadata(ft reflect.StructField, fieldConfigurators []*FieldConfigurator) []*field {
tagParsed := fieldMetadataFromTag(ft.Tag.Get("orm"))
// tagParsed := fieldMetadataFromTag(ft.Tag.Get("orm"))
var fms []*field
fc := getFieldConfiguratorFor(fieldConfigurators, ft.Name)
baseFm := &field{}
baseFm.Type = ft.Type
fms = append(fms, baseFm)
if tagParsed.Name != "" {
baseFm.Name = tagParsed.Name
} else if fc.column != "" {
if fc.column != "" {
baseFm.Name = fc.column
} else {
baseFm.Name = strcase.ToSnake(ft.Name)
}
if tagParsed.PK || strings.ToLower(ft.Name) == "id" || fc.primaryKey {
if strings.ToLower(ft.Name) == "id" || fc.primaryKey {
baseFm.IsPK = true
}
if tagParsed.IsCreatedAt || strings.ToLower(ft.Name) == "createdat" || fc.isCreatedAt {
if strings.ToLower(ft.Name) == "createdat" || fc.isCreatedAt {
baseFm.IsCreatedAt = true
}
if tagParsed.IsUpdatedAt || strings.ToLower(ft.Name) == "updatedat" || fc.isUpdatedAt {
if strings.ToLower(ft.Name) == "updatedat" || fc.isUpdatedAt {
baseFm.IsUpdatedAt = true
}
if tagParsed.IsDeletedAt || strings.ToLower(ft.Name) == "deletedat" || fc.isDeletedAt {
if strings.ToLower(ft.Name) == "deletedat" || fc.isDeletedAt {
baseFm.IsDeletedAt = true
}
if tagParsed.Virtual {
baseFm.Virtual = true
}
// if tagParsed.Virtual {
// baseFm.Virtual = true
// }
if ft.Type.Kind() == reflect.Struct || ft.Type.Kind() == reflect.Ptr {
t := ft.Type
if ft.Type.Kind() == reflect.Ptr {
Expand Down
18 changes: 8 additions & 10 deletions orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type AuthorEmail struct {
ID int64
Email string `orm:"field=email"`
Email string
}

func (a AuthorEmail) ConfigureEntity(e *orm.EntityConfigurator) {
Expand All @@ -21,7 +21,7 @@ func (a AuthorEmail) ConfigureEntity(e *orm.EntityConfigurator) {
}

type HeaderPicture struct {
ID int64 `orm:"field=id pk=true"`
ID int64
PostID int64
Link string
}
Expand All @@ -33,22 +33,20 @@ func (h HeaderPicture) ConfigureEntity(e *orm.EntityConfigurator) {
type Post struct {
ID int64
BodyText string
CreatedAt sql.NullTime `orm:"created_at=true"`
UpdatedAt sql.NullTime `orm:"updated_at=true"`
DeletedAt sql.NullTime `orm:"deleted_at=true"`
CreatedAt sql.NullTime
UpdatedAt sql.NullTime
DeletedAt sql.NullTime
}

func (p Post) ConfigureEntity(e *orm.EntityConfigurator) {
e.Field("BodyText").ColumnName("body")
e.Field("ID").ColumnName("id")
e.
Table("posts").
HasMany(Comment{}, orm.HasManyConfig{}).
HasOne(HeaderPicture{}, orm.HasOneConfig{}).
HasOne(AuthorEmail{}, orm.HasOneConfig{}).
BelongsToMany(Category{}, orm.BelongsToManyConfig{IntermediateTable: "post_categories"}).
Fields().
Field("ID").IsPrimaryKey().ColumnName("id").
Also().
Field("BodyText").ColumnName("body")
BelongsToMany(Category{}, orm.BelongsToManyConfig{IntermediateTable: "post_categories"})

}

Expand Down

0 comments on commit b560a0b

Please sign in to comment.