From 3a8ee5c117ffa443622139a6de2d274a23d88851 Mon Sep 17 00:00:00 2001 From: amirrezaask Date: Fri, 11 Mar 2022 21:50:50 +0330 Subject: [PATCH] README --- README.md | 8 ++++---- orm.go | 22 +--------------------- 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 8e6be5c..7b8eeb6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. diff --git a/orm.go b/orm.go index a7a02e9..8a2acbf 100644 --- a/orm.go +++ b/orm.go @@ -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 @@ -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))