Update README

This commit is contained in:
Jinzhu 2013-11-03 10:49:09 +08:00
parent bd54a03505
commit 8e3b1fd206
1 changed files with 25 additions and 24 deletions

View File

@ -20,8 +20,6 @@ Yet Another ORM library for Go, aims for developer friendly
* Database Pool * Database Pool
* Convention Over Configuration * Convention Over Configuration
## Basic Usage
## Opening a Database ## Opening a Database
```go ```go
@ -45,14 +43,14 @@ db.SetPool(100)
## Conventions ## Conventions
```go ```go
type User struct { // TableName: `users`, gorm will pluralize struct name as table name type User struct { // TableName: `users`, gorm will pluralize struct name as table name
Id int64 // Id: Database Primary key Id int64 // Id: Database Primary key
Birthday time.Time // Time Birthday time.Time
Age int64 Age int64
Name string Name string
CreatedAt time.Time // Time of record is created, will be insert automatically CreatedAt time.Time // Time of record is created, will be insert automatically
UpdatedAt time.Time // Time of record is updated, will be updated automatically UpdatedAt time.Time // Time of record is updated, will be updated automatically
DeletedAt time.Time // Time of record is deleted, refer Soft Delete for more DeletedAt time.Time // Time of record is deleted, refer Soft Delete for more
Email []Email // Embedded structs Email []Email // Embedded structs
BillingAddress Address // Embedded struct BillingAddress Address // Embedded struct
@ -117,9 +115,9 @@ db.Save(&user)
// Get the first record // Get the first record
db.First(&user) db.First(&user)
//// SELECT * FROM users LIMIT 1; //// SELECT * FROM users LIMIT 1;
// Search table `users` are guessed from the out struct's name. // Search table `users` are guessed from the out struct type.
// You are possible to specify the table name with Model() if no out struct for some methods like Pluck() // You are possible to specify the table name with Model() if no out struct for some methods like Pluck()
// Or set table name with Table(), if so, it will ignore the out struct's type even have it. more details later. // Or set table name with Table(), if so, it will ignore the out struct even have it. more details following.
// Get All records // Get All records
db.Find(&users) db.Find(&users)
@ -200,7 +198,7 @@ db.Not(User{Name: "jinzhu"}).First(&user)
//// SELECT * FROM users WHERE name <> "jinzhu"; //// SELECT * FROM users WHERE name <> "jinzhu";
``` ```
### Inline Search ### Query With Inline Condition
```go ```go
// Find with primary key // Find with primary key
@ -226,7 +224,7 @@ db.Find(&users, map[string]interface{}{"age": 20})
### Query With Or ### Query With Or
``` ```go
db.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&users) db.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&users)
//// SELECT * FROM users WHERE role = 'admin' OR role = 'super_admin'; //// SELECT * FROM users WHERE role = 'admin' OR role = 'super_admin';
@ -240,10 +238,10 @@ db.Where("name = 'jinzhu'").Or(map[string]interface{}{"name": "jinzhu 2"}).Find(
### Query Chains ### Query Chains
Gorm has a chainable API, so you could write query in chain Gorm has a chainable API, so you could query like this
```go ```go
db.Where("name <> ?", "jinzhu").Where("age >= ? and role <> ?", 20, "admin").Find(&users) db.Where("name <> ?","jinzhu").Where("age >= ? and role <> ?",20,"admin").Find(&users)
//// SELECT * FROM users WHERE name <> 'jinzhu' AND age >= 20 AND role <> 'admin'; //// SELECT * FROM users WHERE name <> 'jinzhu' AND age >= 20 AND role <> 'admin';
db.Where("role = ?", "admin").Or("role = ?", "super_admin").Not("name = ?", "jinzhu").Find(&users) db.Where("role = ?", "admin").Or("role = ?", "super_admin").Not("name = ?", "jinzhu").Find(&users)
@ -262,7 +260,7 @@ db.Save(&user)
### Update one attribute with `Update` ### Update one attribute with `Update`
``` ```go
// Update an existing struct's name if name is different // Update an existing struct's name if name is different
db.Model(&user).Update("name", "hello") db.Model(&user).Update("name", "hello")
//// UPDATE users SET name='hello' WHERE id=111; //// UPDATE users SET name='hello' WHERE id=111;
@ -272,23 +270,23 @@ db.First(&user, 111).Update("name", "hello")
//// SELECT * FROM users LIMIT 1; //// SELECT * FROM users LIMIT 1;
//// UPDATE users SET name='hello' WHERE id=111; //// UPDATE users SET name='hello' WHERE id=111;
// Update a record // Specify table name with where search
db.Table("users").Where(10).Update("name", "hello") db.Table("users").Where(10).Update("name", "hello")
//// UPDATE users SET name='hello' WHERE id = 10; //// UPDATE users SET name='hello' WHERE id = 10;
``` ```
### Update multiple attributes with `Updates` ### Update multiple attributes with `Updates`
``` ```go
// Update an existing record if have any different attributes // Update an existing record if have any changed values
db.Model(&user).Updates(User{Name: "hello", Age: 18}) db.Model(&user).Updates(User{Name: "hello", Age: 18})
//// UPDATE users SET name='hello', age=18 WHERE id = 111; //// UPDATE users SET name='hello', age=18 WHERE id = 111;
// Update with Map // Updates with Map
db.Table("users").Where(10).Updates(map[string]interface{}{"name": "hello", "age": 18}) db.Table("users").Where(10).Updates(map[string]interface{}{"name": "hello", "age": 18})
//// UPDATE users SET name='hello', age=18 WHERE id = 10; //// UPDATE users SET name='hello', age=18 WHERE id = 10;
// Update with Struct // Updates with Struct
db.Model(User{}).Updates(User{Name: "hello", Age: 18}) db.Model(User{}).Updates(User{Name: "hello", Age: 18})
//// UPDATE users SET name='hello', age=18; //// UPDATE users SET name='hello', age=18;
``` ```
@ -311,7 +309,8 @@ db.Where("email LIKE ?", "%jinzhu%").Delete(Email{})
### Soft Delete ### Soft Delete
If a struct have DeletedAt field, it will get soft delete ability automatically! If a struct has DeletedAt field, it will get soft delete ability automatically!
For those don't have the filed, will be deleted from database permanently For those don't have the filed, will be deleted from database permanently
```go ```go
@ -338,6 +337,7 @@ db.Unscoped().Delete(&order)
## FirstOrInit ## FirstOrInit
Try to load the first record, if fails, initialize struct with search conditions. Try to load the first record, if fails, initialize struct with search conditions.
(only support map or struct conditions, SQL like conditions are not supported) (only support map or struct conditions, SQL like conditions are not supported)
```go ```go
@ -412,6 +412,7 @@ db.Where(User{Name: "jinzhu"}).Attrs(User{Age: 30}).FirstOrCreate(&user)
### FirstOrCreate With Assign ### FirstOrCreate With Assign
Assign's arguments would be used to initialize the struct if not record found, Assign's arguments would be used to initialize the struct if not record found,
If any record found, will assign those values to the record, and save it back to database. If any record found, will assign those values to the record, and save it back to database.
```go ```go
@ -588,7 +589,7 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us
//// SELECT * FROM users WHERE mail_type = 'TEXT'; (users1) //// SELECT * FROM users WHERE mail_type = 'TEXT'; (users1)
//// SELECT * FROM deleted_users WHERE mail_type = 'TEXT'; (users2) //// SELECT * FROM deleted_users WHERE mail_type = 'TEXT'; (users2)
db.Where("email = ?", "x@example.org"').Attrs(User{FromIp: "111.111.111.111"}).FirstOrCreate(&user) db.Where("email = ?", "x@example.org").Attrs(User{FromIp: "111.111.111.111"}).FirstOrCreate(&user)
//// SELECT * FROM users WHERE email = 'x@example.org'; //// SELECT * FROM users WHERE email = 'x@example.org';
//// INSERT INTO "users" (email,from_ip) VALUES ("x@example.org", "111.111.111.111") (if no record found) //// INSERT INTO "users" (email,from_ip) VALUES ("x@example.org", "111.111.111.111") (if no record found)