Ent实现软删除(Soft Delete)

TrumanWong
2/25/2023
TrumanWong

前言

Ent是一个简单且功能强大的Go语言ORM框架,易于构建和维护应用程序与大数据模型。本文主要介绍在Ent中如何实现软删除。

拦截器Interceptors

拦截器Interceptors是各种类型的Ent查询的执行中间件,应用于读取路径并作为接口实现,允许它们在不同阶段拦截和修改查询,从而提供对查询行为的更细粒度的控制。

定义

要定义一个Interceptor,用户可以声明一个实现该Intercept方法的结构或使用预定义的 ent.InterceptFunc适配器。

ent.InterceptFunc(func(next ent.Querier) ent.Querier {
    return ent.QuerierFunc(func(ctx context.Context, query ent.Query) (ent.Value, error) {
        // Do something before the query execution.
        value, err := next.Query(ctx, query)
        // Do something after the query execution.
        return value, err
    })
})

在上面的示例中,ent.Query代表生成的查询生成器(例如,ent.<T>Query)并且访问其方法需要类型断言。例如:

ent.InterceptFunc(func(next ent.Querier) ent.Querier {
    return ent.QuerierFunc(func(ctx context.Context, query ent.Query) (ent.Value, error) {
        if q, ok := query.(*ent.UserQuery); ok {
            q.Where(user.Name("a8m"))
        }
        return next.Query(ctx, query)
    })
})

生成

在命令行界面执行如下代码:

$ go run -mod=mod entgo.io/ent/cmd/ent generate --feature intercept,schema/snapshot ./schema
注意:如果在schema中使用了interceptors,有可能在schema包和生成的ent包之间循环导入。为了避免这种情况,ent生成一个ent/runtime包,负责在运行时注册模式挂钩。如下所示:
import _ "<project>/ent/runtime"

软删除

Mixin

package schema

import (
	"context"
	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/mixin"
	"fmt"
	gen "<project>/ent"
	"<project>/ent/hook"
	"<project>/ent/intercept"
	"time"
)

// SoftDeleteMixin implements the soft delete pattern for schemas.
type SoftDeleteMixin struct {
	mixin.Schema
}

// Fields of the SoftDeleteMixin.
func (SoftDeleteMixin) Fields() []ent.Field {
	return []ent.Field{
		field.Time("delete_time").
			Optional(),
	}
}

type softDeleteKey struct{}

// SkipSoftDelete returns a new context that skips the soft-delete interceptor/mutators.
func SkipSoftDelete(parent context.Context) context.Context {
	return context.WithValue(parent, softDeleteKey{}, true)
}

// Interceptors of the SoftDeleteMixin.
func (d SoftDeleteMixin) Interceptors() []ent.Interceptor {
	return []ent.Interceptor{
		intercept.TraverseFunc(func(ctx context.Context, q intercept.Query) error {
			// Skip soft-delete, means include soft-deleted entities.
			if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
				return nil
			}
			d.P(q)
			return nil
		}),
	}
}

// Hooks of the SoftDeleteMixin.
func (d SoftDeleteMixin) Hooks() []ent.Hook {
	return []ent.Hook{
		hook.On(
			func(next ent.Mutator) ent.Mutator {
				return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
					// Skip soft-delete, means delete the entity permanently.
					if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
						return next.Mutate(ctx, m)
					}
					mx, ok := m.(interface {
						SetOp(ent.Op)
						Client() *gen.Client
						SetDeleteTime(time.Time)
						WhereP(...func(*sql.Selector))
					})
					if !ok {
						return nil, fmt.Errorf("unexpected mutation type %T", m)
					}
					d.P(mx)
					mx.SetOp(ent.OpUpdate)
					mx.SetDeleteTime(time.Now())
					return mx.Client().Mutate(ctx, m)
				})
			},
			ent.OpDeleteOne|ent.OpDelete,
		),
	}
}

// P adds a storage-level predicate to the queries and mutations.
func (d SoftDeleteMixin) P(w interface{ WhereP(...func(*sql.Selector)) }) {
	w.WhereP(
		sql.FieldIsNull(d.Fields()[0].Descriptor().Name),
	)
}

Mixin usage

// Pet holds the schema definition for the Pet entity.
type Pet struct {
    ent.Schema
}

// Mixin of the Pet.
func (Pet) Mixin() []ent.Mixin {
    return []ent.Mixin{
        SoftDeleteMixin{},
    }
}

Runtime usage

// Filter out soft-deleted entities.
pets, err := client.Pet.Query().All(ctx)
if err != nil {
    return err
}

// Include soft-deleted entities.
pets, err := client.Pet.Query().All(schema.SkipSoftDelete(ctx))
if err != nil {
    return err
}

更多关于interceptors的介绍请看官方文档