Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Mar 11, 2022
1 parent a62cc35 commit 3a8ee5c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ user, err := orm.Find[User](1)
You can also use custom queries to get entities from database.
```go

user, err := orm.Query[User]().Where("id", 1).One()
user, err := orm.Query[User]().WherePK(1).One()
user, err := orm.Query[User]().Where("id", 1).First()
user, err := orm.Query[User]().WherePK(1).First()
```
GolobbyORM contains a powerful query builder which you can use to build `Select`, `Update` and `Delete` queries, but if you want to write a raw sql query you can.
```go
Expand Down Expand Up @@ -238,7 +238,7 @@ func (c Comment) ConfigureEntity(e *orm.EntityConfigurator) {
As you can see we are defining a `Comment` entity which has a `BelongsTo` relation with `Post` that we saw earlier. You can configure how GolobbyORM queries `BelongsTo` relation with `orm.BelongsToConfig` object, by default it will infer all fields for you.
now you can use this relationship anywhere in your code.
```go
post, err := orm.BelongsTo[Post](comment)
post, err := orm.BelongsTo[Post](comment).First()
```
#### BelongsToMany
```go
Expand All @@ -257,7 +257,7 @@ func(c Category) ConfigureEntity(r *orm.EntityConfigurator) {
we are defining a `Post` entity and also a `Category` entity which have a many2many relationship, as you can see it's mandatory for us to configure IntermediateTable name which GolobbyORM cannot infer by itself now.
now you can use this relationship anywhere in your code.
```go
categories, err := orm.BelongsToMany[Category](post)
categories, err := orm.BelongsToMany[Category](post).All()
```
#### Saving with relation
You may need to save an entity which has some kind of relation with another entity, in that case you can use `Add` method.
Expand Down
22 changes: 1 addition & 21 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func Update(obj Entity) error {
// Delete given Entity from database
func Delete(obj Entity) error {
s := getSchemaFor(obj)

genericSet(obj, "deleted_at", sql.NullTime{Time: time.Now(), Valid: true})
query, args, err := NewQueryBuilder[Entity]().SetDialect(s.getDialect()).Table(s.Table).Where(s.pkName(), genericGetPKValue(obj)).SetDelete().ToSql()
if err != nil {
return err
Expand Down Expand Up @@ -456,26 +456,6 @@ func addBelongsToMany(to Entity, items ...Entity) error {
return nil
}

//func Query[OUTPUT Entity](s *QueryBuilder[OUTPUT]) ([]OUTPUT, error) {
// o := new(OUTPUT)
// sch := getSchemaFor(*o)
// s.SetDialect(sch.dialect).Table(sch.Table).SetSelect()
// q, args, err := s.ToSql()
// if err != nil {
// return nil, err
// }
// rows, err := getSchemaFor(*o).getSQLDB().Query(q, args...)
// if err != nil {
// return nil, err
// }
// var output []OUTPUT
// err = getSchemaFor(*o).bind(rows, &output)
// if err != nil {
// return nil, err
// }
// return output, nil
//}

func Query[E Entity]() *QueryBuilder[E] {
q := NewQueryBuilder[E]()
s := getSchemaFor(*new(E))
Expand Down

0 comments on commit 3a8ee5c

Please sign in to comment.