Documentation ¶
Overview ¶
Package enthistory provides code generation to add history tables for an ent schema
Index ¶
- Variables
- func HistoryHooks[T Mutation]() []ent.Hook
- func On(hk ent.Hook, op ent.Op) ent.Hook
- type Annotations
- type AuthzSettings
- type Config
- type ExtensionOption
- func WithAllowedRelation(relation string) ExtensionOption
- func WithAuditing() ExtensionOption
- func WithAuthzPolicy() ExtensionOption
- func WithFirstRun(firstRun bool) ExtensionOption
- func WithGQLQuery() ExtensionOption
- func WithHistoryTimeIndex() ExtensionOption
- func WithImmutableFields() ExtensionOption
- func WithNillableFields() ExtensionOption
- func WithSchemaName(schemaName string) ExtensionOption
- func WithSchemaPath(schemaPath string) ExtensionOption
- func WithSkipper(skipper string) ExtensionOption
- func WithUpdatedBy(key string, valueType ValueType) ExtensionOption
- func WithUpdatedByFromSchema(valueType ValueType, nillable bool) ExtensionOption
- type FieldProperties
- type HistoryExtension
- type Mutation
- type Mutator
- type OpType
- type UpdatedBy
- type ValueType
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedIDType is returned when id type other than string or int is used ErrUnsupportedIDType = errors.New("unsupported id type, only int and strings are allowed") // ErrUnsupportedType is returned when the object type is not supported ErrUnsupportedType = errors.New("unsupported type") // ErrNoIDType is returned when the id type cannot be determined from the schema ErrNoIDType = errors.New("could not get id type for schema") // ErrInvalidSchemaPath is returned when the schema path cannot be determined ErrInvalidSchemaPath = errors.New("invalid schema path, unable to find package name in path") // ErrFailedToGenerateTemplate is returned when the template cannot be generated ErrFailedToGenerateTemplate = errors.New("failed to generate template") // ErrFailedToWriteTemplate is returned when the template cannot be written ErrFailedToWriteTemplate = errors.New("failed to write template") )
Functions ¶
func HistoryHooks ¶
HistoryHooks returns a list of hooks that can be used to create history entries
Types ¶
type Annotations ¶
type Annotations struct { Exclude bool `json:"exclude,omitempty"` // Will exclude history tracking for this schema IsHistory bool `json:"isHistory,omitempty"` // DO NOT APPLY TO ANYTHING EXCEPT HISTORY SCHEMAS }
Annotations of the history extension
type AuthzSettings ¶ added in v0.1.0
type AuthzSettings struct { // Enabled is a boolean that tells the extension to generate the authz policy Enabled bool // FirstRun is a boolean that tells the extension to only generate the policies after the first run FirstRun bool // AllowedRelation is the name of the relation that should be used to restrict // all audit log queries to users with that role, if not set the interceptor will not be added AllowedRelation string }
type Config ¶
type Config struct { IncludeUpdatedBy bool UpdatedBy *UpdatedBy Auditing bool SchemaPath string SchemaName string Query bool Skipper string FieldProperties *FieldProperties HistoryTimeIndex bool Auth AuthzSettings }
Config is the configuration for the history extension
type ExtensionOption ¶
type ExtensionOption = func(*HistoryExtension)
func WithAllowedRelation ¶ added in v0.1.0
func WithAllowedRelation(relation string) ExtensionOption
WithAllowedRelation sets the relation that should be used to restrict all audit log queries to users with that role
func WithAuditing ¶
func WithAuditing() ExtensionOption
WithAuditing allows you to turn on the code generation for the `.Audit()` method
func WithAuthzPolicy ¶ added in v0.1.0
func WithAuthzPolicy() ExtensionOption
func WithFirstRun ¶ added in v0.1.0
func WithFirstRun(firstRun bool) ExtensionOption
WithFirstRun tells the extension to generate the history schema on the first run which leaves out the entfga policy
func WithGQLQuery ¶ added in v0.0.7
func WithGQLQuery() ExtensionOption
WithGQLQuery adds the entgql Query annotation to the history schema in order to allow for querying
func WithHistoryTimeIndex ¶
func WithHistoryTimeIndex() ExtensionOption
WithHistoryTimeIndex allows you to add an index to the "history_time" fields
func WithImmutableFields ¶
func WithImmutableFields() ExtensionOption
WithImmutableFields allows you to set all tracked fields in history to Immutable
func WithNillableFields ¶
func WithNillableFields() ExtensionOption
WithNillableFields allows you to set all tracked fields in history to Nillable except enthistory managed fields (history_time, ref, operation, updated_by, & deleted_by)
func WithSchemaName ¶ added in v0.0.6
func WithSchemaName(schemaName string) ExtensionOption
WithSchemaName allows you to set an alternative schema name This can be used to set a schema name for multi-schema migrations and SchemaConfig feature https://entgo.io/docs/multischema-migrations/
func WithSchemaPath ¶
func WithSchemaPath(schemaPath string) ExtensionOption
WithSchemaPath allows you to set an alternative schemaPath Defaults to "./schema"
func WithSkipper ¶ added in v0.0.7
func WithSkipper(skipper string) ExtensionOption
WithSkipper allows you to set a skipper function to skip history tracking
func WithUpdatedBy ¶
func WithUpdatedBy(key string, valueType ValueType) ExtensionOption
WithUpdatedBy sets the key and type for pulling updated_by from the context, usually done via a middleware to track which users are making which changes
func WithUpdatedByFromSchema ¶ added in v0.1.1
func WithUpdatedByFromSchema(valueType ValueType, nillable bool) ExtensionOption
WithUpdatedByFromSchema uses the original update_by value in the schema and includes in the audit results
type FieldProperties ¶
FieldProperties is a struct that holds the properties for the fields in the history schema
type HistoryExtension ¶
type HistoryExtension struct { entc.DefaultExtension // contains filtered or unexported fields }
HistoryExtension implements entc.Extension.
func New ¶ added in v0.1.0
func New(opts ...ExtensionOption) *HistoryExtension
New creates a new history extension
func (*HistoryExtension) Annotations ¶
func (h *HistoryExtension) Annotations() []entc.Annotation
Annotations of the HistoryExtension
func (*HistoryExtension) GenerateSchemas ¶ added in v0.1.0
func (h *HistoryExtension) GenerateSchemas() error
GenerateSchemas generates the history schema for all schemas in the schema path this should be called before the entc.Generate call so the schemas exist at the time of code generation
func (*HistoryExtension) SetFirstRun ¶ added in v0.1.0
func (h *HistoryExtension) SetFirstRun(firstRun bool)
SetFirstRun sets the first run value for the history extension outside of the options
func (*HistoryExtension) Templates ¶
func (h *HistoryExtension) Templates() []*gen.Template
Templates returns the generated templates which include the client, history query, history from mutation and an optional auditing template
type Mutation ¶
type Mutation interface { Op() ent.Op CreateHistoryFromCreate(ctx context.Context) error CreateHistoryFromUpdate(ctx context.Context) error CreateHistoryFromDelete(ctx context.Context) error }
Mutation is an interface that must be implemented by all mutations that are
type OpType ¶
type OpType string
OpType is the ent operation type in string form
func (OpType) MarshalGQL ¶
MarshalGQL implement the Marshaler interface for gqlgen
func (*OpType) Scan ¶
Scan implements the `database/sql.Scanner` interface for the `OpType` type and is used to convert a value from the database into an `OpType` value.
func (*OpType) UnmarshalGQL ¶
UnmarshalGQL implement the Unmarshaler interface for gqlgen