1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 13:21:29 +00:00
Beego/orm/README.md

164 lines
3.0 KiB
Markdown
Raw Normal View History

2013-08-07 15:28:14 +00:00
# beego orm
2013-07-30 12:32:38 +00:00
2013-08-13 10:06:01 +00:00
[![Build Status](https://drone.io/github.com/astaxie/beego/status.png)](https://drone.io/github.com/astaxie/beego/latest)
A powerful orm framework for go.
It is heavily influenced by Django ORM, SQLAlchemy.
2013-07-30 12:32:38 +00:00
now, beta, unstable, may be changing some api make your app build failed.
2013-08-11 14:27:54 +00:00
**Support Database:**
2013-08-07 15:28:14 +00:00
* MySQL: [github.com/go-sql-driver/mysql](https://github.com/go-sql-driver/mysql)
2013-08-11 14:27:54 +00:00
* PostgreSQL: [github.com/lib/pq](https://github.com/lib/pq)
* Sqlite3: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
Passed all test, but need more feedback.
2013-08-07 15:28:14 +00:00
**Features:**
2013-08-13 10:06:01 +00:00
* full go type support
* easy for usage, simple CRUD operation
* auto join with relation table
* cross DataBase compatible query
* Raw SQL query / mapper without orm model
* full test keep stable and strong
more features please read the docs
2013-08-07 15:28:14 +00:00
**Install:**
go get github.com/astaxie/beego/orm
2013-08-13 10:06:01 +00:00
## Changelog
2013-08-19 14:37:53 +00:00
* 2013-08-19: support table auto create
2013-08-13 10:06:01 +00:00
* 2013-08-13: update test for database types
* 2013-08-13: go type support, such as int8, uint8, byte, rune
* 2013-08-13: date / datetime timezone support very well
2013-08-07 15:28:14 +00:00
## Quick Start
#### Simple Usage
```go
package main
import (
"fmt"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql" // import your used driver
)
// Model Struct
type User struct {
Id int `orm:"auto"`
Name string `orm:"size(100)"`
}
func init() {
// register model
orm.RegisterModel(new(User))
// set default database
orm.RegisterDataBase("default", "mysql", "root:root@/my_db?charset=utf8", 30)
}
func main() {
o := orm.NewOrm()
user := User{Name: "slene"}
// insert
id, err := o.Insert(&user)
// update
user.Name = "astaxie"
num, err := o.Update(&user)
// read one
u := User{Id: user.Id}
err = o.Read(&u)
// delete
num, err = o.Delete(&u)
}
```
#### Next with relation
```go
type Post struct {
Id int `orm:"auto"`
Title string `orm:"size(100)"`
User *User `orm:"rel(fk)"`
}
var posts []*Post
qs := o.QueryTable("post")
num, err := qs.Filter("User__Name", "slene").All(&posts)
```
#### Use Raw sql
2013-08-08 03:07:08 +00:00
If you don't like ORMuse Raw SQL to query / mapping without ORM setting
2013-08-07 15:28:14 +00:00
```go
var maps []Params
num, err := o.Raw("SELECT id FROM user WHERE name = ?", "slene").Values(&maps)
if num > 0 {
fmt.Println(maps[0]["id"])
}
```
#### Transaction
```go
o.Begin()
...
user := User{Name: "slene"}
id, err := o.Insert(&user)
if err != nil {
o.Commit()
} else {
o.Rollback()
}
```
#### Debug Log Queries
In development env, you can simple use
```go
func main() {
orm.Debug = true
...
```
enable log queries.
output include all queries, such as exec / prepare / transaction.
like this:
```go
[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [INSERT INTO `user` (`name`) VALUES (?)] - `slene`
...
```
note: not recommend use this in product env.
2013-08-07 15:28:14 +00:00
## Docs
more details and examples in docs and test
2013-08-22 10:35:35 +00:00
* [中文](http://beego.me/docs/Models_Overview?lang=zh)
* [English](http://beego.me/docs/Models_Overview?lang=en)
2013-08-07 15:28:14 +00:00
2013-07-30 12:32:38 +00:00
## TODO
- some unrealized api
- examples
- docs