From e250637d12865e802173c8fc247f9d61a8789d3a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Oct 2013 20:32:27 +0800 Subject: [PATCH] Document FirstOrInit, FirstOrCreate --- README.md | 18 +++++++++++++++++- gorm_test.go | 22 ++++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 82bd5b4d..b45b6e16 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Yet Another ORM library for Go, aims for developer friendly * Before/After Create/Save/Update/Delete Callbacks * Order/Select/Limit/Offset Support * Update, Updates Like Rails's update_attribute, update_attributes +* FirstOrInit, FirstOrCreate Like Rails's first_or_initialize, first_or_create * Dynamically set table name when search, update, delete... * Automatically CreatedAt, UpdatedAt * Soft Delete @@ -84,6 +85,22 @@ db.Find(&users, &User{Age: 20}) db.Find(&users, map[string]interface{}{"age": 20}) //// users -> select * from users where age = 20; +// FirstOrInit +db.FirstOrInit(&user, User{Name: "noexisting_user"}) +//// user -> User{Name: "noexisting_user"} +db.Where(User{Name: "Jinzhu"}).FirstOrInit(&user) +//// user -> User{Id: 111, Name: "Jinzhu"} +db.FirstOrInit(&user, map[string]interface{}{"name": "jinzhu", "age": 20}) +//// user -> User{Id: 111, Name: "Jinzhu", Age: 20} + +// FirstOrCreate +db.FirstOrCreate(&user, User{Name: "noexisting_user"}) +//// user -> User{Id: 112, Name: "noexisting_user"} +db.Where(User{Name: "Jinzhu"}).FirstOrCreate(&user) +//// user -> User{Id: 111, Name: "Jinzhu"} +db.FirstOrCreate(&user, map[string]interface{}{"name": "jinzhu", "age": 20}) +//// user -> User{Id: 111, Name: "Jinzhu", Age: 20} + // Select db.Select("name").Find(&users) //// users -> select name from users; @@ -251,7 +268,6 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us ``` ## TODO -* FindOrInitialize / FindOrCreate * SubStruct * Index, Unique, Valiations * Auto Migration diff --git a/gorm_test.go b/gorm_test.go index cc740c6b..ee903c4e 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -800,27 +800,37 @@ func TestSoftDelete(t *testing.T) { } func TestFindOrInitialize(t *testing.T) { - var user1, user2 User + var user1, user2, user3 User db.Where(&User{Name: "find or init", Age: 33}).FirstOrInit(&user1) if user1.Name != "find or init" || user1.Id != 0 || user1.Age != 33 { t.Errorf("user should be initialized with search value") } - db.FirstOrInit(&user2, map[string]interface{}{"name": "find or init 2"}) - if user2.Name != "find or init 2" || user2.Id != 0 { + db.Where(User{Name: "find or init", Age: 33}).FirstOrInit(&user2) + if user2.Name != "find or init" || user2.Id != 0 || user2.Age != 33 { + t.Errorf("user should be initialized with search value") + } + + db.FirstOrInit(&user3, map[string]interface{}{"name": "find or init 2"}) + if user3.Name != "find or init 2" || user3.Id != 0 { t.Errorf("user should be initialized with inline search value") } } func TestFindOrCreate(t *testing.T) { - var user1, user2 User + var user1, user2, user3 User db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user1) if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 { t.Errorf("user should be created with search value") } - db.FirstOrCreate(&user2, map[string]interface{}{"name": "find or create 2"}) - if user2.Name != "find or create 2" || user2.Id == 0 { + db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user2) + if user2.Name != "find or create" || user2.Id == 0 || user2.Age != 33 { + t.Errorf("user should be created with search value") + } + + db.FirstOrCreate(&user3, map[string]interface{}{"name": "find or create 2"}) + if user3.Name != "find or create 2" || user3.Id == 0 { t.Errorf("user should be created with inline search value") } }