Giter Site home page Giter Site logo

xormt's Introduction

xormt

基于xorm和gin多租户实现

实例仓库

https://github.com/hrf304/ukid.git

使用步骤

1、在main函数中调用Init进行初始化,并传入相关TenantDBProvider和TenantIdResolver的具体实现

func main() {
	ginEngine := gin.Default()
	xormt.Init(xormtext.GetTenants, xormtext.GetTenantId)
	router.Register(ginEngine)
	ginEngine.Run(":8080")
}

2、需要同步实体类中调用AddModel

func init(){
	xormt.AddModel(new (User))
}

3、映射router,此时需要使用xormt.HandlerGin返回gin.HandlerFunc

v1.GET("/users/:id", xormt.HandlerGin(ctrl.Get))

4、实现controller处理函数,参数为*xormt.MultiTenantContext

func (c *UserController) Get(ctx *xormt.MultiTenantContext) {
	i++

	user := &entity.User{}
	user.Id = util.UUID()
	user.Name = fmt.Sprintf("huangrf%d", i)
	user.LoginId = fmt.Sprintf("huangrf%d", i)
	user.Major = fmt.Sprintf("major%d", i)

	_, err := ctx.DB.InsertOne(user)
	if err != nil{
		ctx.JSON(500, &entity.Resp{500, err.Error(), nil})
	}else{
		ctx.JSON(http.StatusOK, &entity.Resp{http.StatusOK, "", user.Id})
	}
}

xormt's People

Contributors

hrf304 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.