added in License
This commit is contained in:
66
db/client.go
66
db/client.go
@@ -1,3 +1,7 @@
|
||||
// Copyright 2024 Patial Tech. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
@@ -54,48 +58,52 @@ func Client() *ent.Client {
|
||||
return cl
|
||||
}
|
||||
|
||||
type entity interface {
|
||||
GetID() (int64, error)
|
||||
}
|
||||
|
||||
// A AuditHook is an example for audit-log hook.
|
||||
func AuditHook(next ent.Mutator) ent.Mutator {
|
||||
type Entity interface {
|
||||
GetID() (int64, error)
|
||||
}
|
||||
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (v ent.Value, err error) {
|
||||
var id int64
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
saveAudit(ctx, id, m.Type(), m.Op().String(), time.Since(start), err)
|
||||
saveAudit(ctx, m.Type(), m.Op(), v, err, time.Since(start))
|
||||
}()
|
||||
|
||||
v, err = next.Mutate(ctx, m)
|
||||
|
||||
if en, ok := v.(Entity); ok {
|
||||
id, _ = en.GetID()
|
||||
}
|
||||
logger.Info("** %v", v)
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
func saveAudit(_ context.Context, entID int64, entT, op string, d time.Duration, err error) {
|
||||
logger.Info("audit: %d, %s, %s, %s, %v", entID, entT, op, d, err)
|
||||
// ml.SetCreatedAt(time.Now())
|
||||
// if usr := auth.CtxUser(ctx); usr != nil {
|
||||
// ml.SetByID(usr.ID)
|
||||
// ml.SetBy(usr.DisplayName)
|
||||
// }
|
||||
func saveAudit(ctx context.Context, t string, op ent.Op, v ent.Value, err error, d time.Duration) {
|
||||
if t == "Audit" {
|
||||
// skip Audit table operations
|
||||
return
|
||||
}
|
||||
|
||||
// switch op := m.Op(); {
|
||||
// case op.Is(ent.OpCreate):
|
||||
// ml.SetOperation("Create")
|
||||
var entOp string
|
||||
switch {
|
||||
case op.Is(ent.OpCreate):
|
||||
entOp = "Create"
|
||||
case op.Is(ent.OpDelete):
|
||||
entOp = "Delete"
|
||||
case op.Is(ent.OpDeleteOne):
|
||||
entOp = "DeleteOne"
|
||||
case op.Is(ent.OpUpdate):
|
||||
entOp = "Update"
|
||||
case op.Is(ent.OpUpdateOne):
|
||||
entOp = "UpdateOne"
|
||||
}
|
||||
|
||||
// case op.Is(ent.OpUpdate):
|
||||
// ml.SetOperation("Update")
|
||||
// case op.Is(ent.OpUpdateOne):
|
||||
// ml.SetOperation("UpdateOne")
|
||||
big.
|
||||
reqID := ctx.Value("RequestID")
|
||||
ip := ctx.Value("RequestIP")
|
||||
ua := ctx.Value("RequestUA")
|
||||
|
||||
// case op.Is(ent.OpDelete):
|
||||
// ml.SetOperation("Delete")
|
||||
// case op.Is(ent.OpDeleteOne):
|
||||
// ml.SetOperation("DeleteOne")
|
||||
// }
|
||||
if en, ok := v.(entity); ok {
|
||||
id, _ := en.GetID()
|
||||
logger.Info("%s %s %s-%s(%d) ip=%s ua=%s t=%v error=%e", reqID, status, op.String(), t, id, ip, ua, d, err)
|
||||
} else {
|
||||
logger.Info("%s %s %s-%s ip=%s ua=%s t=%v error=%e", reqID, status, op.String(), t, ip, ua, d, err)
|
||||
}
|
||||
}
|
||||
|
@@ -1,3 +1,7 @@
|
||||
// Copyright 2024 Patial Tech. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
|
Reference in New Issue
Block a user