Skip to content
/ orm Public

add sharding table implement fork from beego orm v.1.12.3

Notifications You must be signed in to change notification settings

gityf/orm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

orm

add sharding table implement fork from beego orm v.1.12.3

how to sharding table?

    // 1. get orm instance
    ormInstance = orm.NewOrm()
    err =ormInstance.Using("default")
    
    // 2. set table name in time
    ormInstance.ShardingTable(
        func(tableName string) string {
            return tableName + "_yyyymm"
        },
    )
    
    // 3. curd data from db
    err = ormInstance.Insert(testTableModel)

insert

o := orm.NewOrm()
user := User{Name: "slene"}
// insert
id, err := o.Insert(&user)

sharding insert

o := orm.NewOrm()
user := User{Name: "slene"}
 
// set table name to `user_1`
o.ShardingTable(
    func(tableName string) string {
        return tableName + "_1"
    },
)
 
// insert
id, err := o.Insert(&user)

update

o := orm.NewOrm()
user := User{Name: "slene"}
 
// update
user.Name = "astaxie"
num, err := o.Update(&user)

sharding update

o := orm.NewOrm()
user := User{Name: "slene"}
 
// set table name to `user_1`
o.ShardingTable(
    func(tableName string) string {
        return tableName + "_1"
    },
)
 
// update
user.Name = "astaxie"
num, err := o.Update(&user)

query

o := orm.NewOrm()
user := User{id: 1}
 
 
// select
o.QueryTable(user).One(&user)

sharding query

o := orm.NewOrm()
user := User{id: 1}
 
// set table name to `user_1`
o.ShardingTable(
    func(tableName string) string {
        return tableName + "_1"
    },
)
 
// select
o.QueryTable(user).Offset(offset).Limit(limit).One(&user)
 
// select id, name from user_1 where id=1 limit 0,1

delete

o := orm.NewOrm()
user := User{id: 1}
 
 
// delete
o.Delete(user)
 
// delete from user_1 where id=1

sharding delete

o := orm.NewOrm()
user := User{id: 1}
 
// set table name to `user_1`
o.ShardingTable(
    func(tableName string) string {
        return tableName + "_1"
    },
)
 
// delete
o.Delete(user)
 
// delete from user_1 where id=1