callbacks.go
func DependentResourcesValidator(pairs map[coal.Model]string) *Callback {
	return C("DependentResourcesValidator", Only(Delete), func(ctx *Context) error {
		// check all relations
		for model, field := range pairs {
			// prepare query
			query := bson.M{coal.F(model, field): ctx.Model.ID()}

			// exclude soft deleted documents if supported
			if sdm := coal.L(model, "fire-soft-delete", false); sdm != "" {
				query[coal.F(model, sdm)] = nil
			}

			// count referencing documents
			ctx.Tracer.Push("mgo/Query.Count")
			ctx.Tracer.Tag("query", query)
			n, err := ctx.Store.DB().C(coal.C(model)).Find(query).Limit(1).Count()
			if err != nil {
				return err
			}
			ctx.Tracer.Pop()

			// return err of documents are found
			if n != 0 {
				return E("resource has dependent resources")
			}
		}

		// pass validation
		return nil
	})
}
func DependentResourcesValidator(pairs map[coal.Model]string) *Callback {
	return C("DependentResourcesValidator", Only(Delete), func(ctx *Context) error {
		// check all relations
		for model, field := range pairs {
			// prepare query
			query := bson.M{coal.F(model, field): ctx.Model.ID()}

			// exclude soft deleted documents if supported
			if sdm := coal.L(model, "fire-soft-delete", false); sdm != "" {
				query[coal.F(model, sdm)] = nil
			}

			// count referencing documents
			ctx.Tracer.Push("mgo/Query.Count")
			ctx.Tracer.Tag("query", query)
			n, err := ctx.Store.DB().C(coal.C(model)).Find(query).Limit(1).Count()
			if err != nil {
				return err
			}
			ctx.Tracer.Pop()

			// return err of documents are found
			if n != 0 {
				return E("resource has dependent resources")
			}
		}

		// pass validation
		return nil
	})
}
// DependentResourcesValidator counts related documents and returns an error if
// some get found. This callback is meant to protect resources from breaking
// relations when requested to be deleted.
//
// Dependent resources are defined by passing pairs of models and fields that
// reference the current model.
//
//	fire.DependentResourcesValidator(map[coal.Model]string{
//		&Post{}: "Author",
//		&Comment{}: "Author",
//	})
//
// The callback supports models that use the soft delete mechanism.