gorm/README.md

161 lines
4.5 KiB
Markdown
Raw Normal View History

2013-10-25 12:24:29 +04:00
# GORM
2013-10-26 11:18:44 +04:00
Yet Another ORM library for Go, aims for developer friendly
2013-10-25 12:24:29 +04:00
2013-10-27 18:18:06 +04:00
## Basic Usage
2013-10-27 17:37:31 +04:00
```go
db, _ = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
type User struct {
Id int64
Age int64
Birthday time.Time
Name string
CreatedAt time.Time
UpdatedAt time.Time
}
// Create
user = User{Name: "jinzhu", Age: 18, Birthday: time.Now())
db.Save(&user)
// Update
user.Name = "jinzhu 2"
db.Save(&user)
// Delete
db.Delete(&user)
// Get First matched record
db.Where("name = ?", "jinzhu").First(&user)
// Get All matched records
db.Where("name = ?", "jinzhu").Find(&users)
// Advanced Where Usage
2013-10-27 18:36:43 +04:00
// select * from users name <> 'jinzhu';
2013-10-27 17:37:31 +04:00
db.Where("name <> ?", "jinzhu").Find(&users)
2013-10-27 18:36:43 +04:00
// select * from users name = 'jinzhu' and age >= 22;
db.Where("name = ? and age >= ?", "jinzhu", "22").Find(&users)
// select * from users name in ('jinzhu', 'jinzhu 2');
2013-10-27 17:37:31 +04:00
db.Where("name in (?)", []string["jinzhu", "jinzhu 2"]).Find(&users)
db.Where("birthday < ?", time.Now()).Find(&users)
2013-10-27 18:18:06 +04:00
// Inline search condition
2013-10-27 18:36:43 +04:00
// select * from users where id = 23 limit 1;
db.First(&user, 23)
// select * from users where name = "jinzhu" limit 1;
db.First(&user, "name = ?", "jinzhu")
// select * from users where name = "jinzhu";
db.Find(&users, "name = ?", "jinzhu")
// select * from users where name <> "jinzhu" and age > 20;
db.Find(&users, "name <> ? and age > ?", "jinzhu", 20)
2013-10-27 18:18:06 +04:00
2013-10-27 17:37:31 +04:00
// Select
2013-10-27 18:36:43 +04:00
// select name from users;
2013-10-27 17:37:31 +04:00
db.Select("name").Find(&users)
// Order
2013-10-27 18:36:43 +04:00
// select * from users order by age desc, name;
2013-10-27 17:37:31 +04:00
db.Order("age desc, name").Find(&users)
2013-10-27 18:18:06 +04:00
db.Order("age desc").Order("name").Find(&users)
2013-10-27 17:37:31 +04:00
// Limit
2013-10-27 18:36:43 +04:00
// select * from users limit 3;
2013-10-27 17:37:31 +04:00
db.Limit(3).Find(&users)
2013-10-27 18:18:06 +04:00
db.Limit(10).Find(&ten_users).Limit(20).Find(&twenty_users).Limit(-1).Find(&all_users)
2013-10-27 17:37:31 +04:00
// Offset
2013-10-27 18:36:43 +04:00
// select * from users offset 3;
2013-10-27 17:37:31 +04:00
db.Offset(3).Find(&users)
2013-10-27 18:18:06 +04:00
db.Offset(10).Find(&users).Offset(20).Find(&users).Offset(-1).Find(&users)
2013-10-27 17:37:31 +04:00
// Or
2013-10-27 18:36:43 +04:00
// select * from users where role = 'admin' or role = 'super_admin';
2013-10-27 18:18:06 +04:00
db.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&users)
2013-10-27 17:37:31 +04:00
// Count
2013-10-27 18:36:43 +04:00
// select count(*) from users where name = 'jinzhu' or name = 'jinzhu 2'';
2013-10-27 18:18:06 +04:00
db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count)
db.Model(&User{}).Where("name = ?", "jinzhu").Count(&count)
2013-10-27 17:37:31 +04:00
// CreatedAt (auto insert current time on create)
2013-10-27 18:18:06 +04:00
If your struct has field CreatedAt,
it will be filled with the current time when insert into database
2013-10-27 17:37:31 +04:00
// UpdatedAt (auto update the time on save)
2013-10-27 18:18:06 +04:00
If your struct has field UpdatedAt,
it will be filled with the current time when update it
2013-10-27 17:37:31 +04:00
// Callbacks
2013-10-27 18:18:06 +04:00
Below callbacks are defined now:
`BeforeCreate`, `BeforeUpdate`, `BeforeSave`, `AfterCreate`, `AfterUpdate`, `AfterSave`
`BeforeDelete`, `AfterDelete`
2013-10-27 17:37:31 +04:00
Callbacks is a function defined to a model, if the function return error, will prevent the database operations.
2013-10-27 18:18:06 +04:00
func (u *User) BeforeUpdate() (err error) {
if u.readonly() {
err = errors.New("Read Only User")
}
return
}
// Pluck (get users's age as map)
2013-10-27 17:37:31 +04:00
var ages []int64
2013-10-27 18:18:06 +04:00
db.Find(&users).Pluck("age", &ages)
var names []string
db.Model(&User{}).Pluck("name", &names)
2013-10-27 17:37:31 +04:00
// Query Chains
db.Where("name <> ?", "jinzhu").Where("age >= ? and role <> ?", 20, "admin").Find(&users)
// Create Table with struct
db.CreateTable(&User{})
// Run Raw SQL
db.Exec("drop table users;")
```
2013-10-27 18:18:06 +04:00
## Cool Examples
```go
// Already excited about the basic usage? Let's see some magic!
db.First(&first_article).Count(&total_count).Limit(10).Find(&first_page_articles).Offset(10).Find(&second_page_articles)
// first_article return the latest article
// total_count return the total numbers of articles
// first_page_articles return the latest 10 articles
// second_page_articles return the latest 10 to 20 articles
db.Where("created_at > ?", "2013/10/10").Find(&cancelled_orders, "state = ?", "cancelled").Find(&shipped_orders, "state = ?", "shipped")
// cancelled_orders return all cancelled orders since 2013/10/10
// shipped_orders return all shipped orders since 2013/10/10
db.Model(&Order{}).Where("amount > ?", 10000).Pluck("user_id", &paid_user_ids)
db.Where("user_id = ?", paid_user_ids).Find(&:paid_users)
db.Where("product_name = ?", "fancy_product").Find(&orders).Find(&shopping_cart)
// Open your mind, add more cool examples
```
2013-10-26 11:18:44 +04:00
## TODO
2013-10-27 18:18:06 +04:00
* Update, Updates like rails's update_attribute, update_attributes
2013-10-27 12:47:02 +04:00
* Soft Delete
2013-10-27 18:18:06 +04:00
* Query with map or struct
2013-10-26 11:18:44 +04:00
* FindOrInitialize / FindOrCreate
* SQL Log
* Auto Migration
* Index, Unique, Valiations
2013-10-25 12:24:29 +04:00
2013-10-26 11:18:44 +04:00
# Author
2013-10-25 12:24:29 +04:00
2013-10-26 11:18:44 +04:00
**Jinzhu**
2013-10-25 12:24:29 +04:00
2013-10-26 11:18:44 +04:00
* <http://github.com/jinzhu>
* <wosmvp@gmail.com>
* <http://twitter.com/zhangjinzhu>