Skip to content

Latest commit

 

History

History
88 lines (61 loc) · 2.36 KB

README.md

File metadata and controls

88 lines (61 loc) · 2.36 KB

go report card Run Tests codecov GoDoc

Audit

Audit is used to record the last user who created/updated the model. It uses a CreatedBy and UpdatedBy field to save the information.

Example

Register GORM callbacks

db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
err = audit.RegisterCallbacks(db)

Making a model which include the extra information.

Embed audit.Audit for audit information.

type User struct {
    gorm.Model
    audit.Audit
    Name string
}

Usage

// setup the information to the db context
db = db.Set(audit.ContextKeyCurrentUser, "Admin")

// create a record
var user = User{Name: "Alice"}
db.Create(&user)
// INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`created_by`,`updated_by`,`name`) VALUES ("...","...",NULL,"Admin","Admin","Alice")

Tenant

Tenant is used to record the model belong to which tenant. It uses TenantID to save the information when create the model.

Example

Register GORM callbacks

db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
err = tenant.RegisterCallbacks(db)

Making a model which include the extra information.

Embed audit.Audit for audit information.

type User struct {
    gorm.Model
    tenant.TenantModel
    Name string
    Role string
}

Usage

db := db.Set(ContextKeyTenantID, uint(1))

db.Create(&User{Name: "Alice", Role: "admin"})
// INSERT INTO `users` (`tenant_id`,`name`,`role`) VALUES (1,"Alice","admin") RETURNING `id`

db.Model(&User{}).Where("name", "Alice").Update("role", "user")
// UPDATE `users` SET `role`="user" WHERE `name` = "Alice" AND `tenant_id` = 1

var users []User
db.Find(&users)
// SELECT * FROM `users` WHERE `name` = "Alice" AND `tenant_id` = 1

License

Released under the MIT License.