type
HookContext
HookContext supplies the request, values, and local API to a collection or global hook. Change entries in Data before saving, or values in Document to change the response after saving. A Hook returns only an error.
Properties
| Property | Type | Description |
|---|---|---|
Context | context.Context | Context carries cancellation, deadlines, and the active transaction. Pass it to Local calls that must succeed or roll back with this operation. |
Operation | operation.Kind | Operation names the document operation, such as Create, Update, or Read, not the hook phase. Shared hooks should check it before running write-only work. |
WritePhase | operation.WritePhase | WritePhase identifies draft saves versus publication writes independently of the row's current public status. |
CollectionID | schema.StableID | CollectionID is the collection's stable resource ID, not its slug. It is empty for a global operation. |
GlobalID | schema.StableID | GlobalID is the global's stable resource ID, not its slug. It is empty for a collection operation. |
Actor | *store.Document | Actor is the authenticated document, or nil for an anonymous operation. |
ActorCollection | schema.CollectionSlug | ActorCollection identifies the exact auth collection that owns Actor. |
System | bool | System reports that trusted server code started this operation with System set, so no access rule ran. Nested Local calls do not inherit it: pass System again when the nested work is also trusted. |
Data | store.Values | Data contains the values being saved. On an update it holds only the top-level fields the caller sent; Candidate adds the unchanged ones from Original. Change map entries to change stored values; assigning a new map to Data does not replace the engine's values. Changes after saving are not persisted. |
Document | *store.Document | Document is the response document when available. AfterRead receives each document in a list separately. Changes to Document.Values affect the response, not stored data, and remain subject to field read access rules. |
Original | *store.Document | Original is a detached copy of the saved document before an update, delete, or duplication. Changing it does not change storage. |
Local | *LocalAPI | Local performs nested reads and writes through the normal operation engine. Pass Context to reuse the transaction, and pass Actor, ActorCollection, and locale options explicitly when the nested operation represents the same user. Standalone reads reject nested writes; AfterCommit starts new transactions. |
Error | error | Error is set only for after-error hooks and preserves the original failure. Returning nil from AfterError does not make the failed operation succeed. |
Locale | schema.LocaleCode | Locale is the selected content language. Write hooks use the write locale; AfterRead uses the response locale, whose values may include fallback text. |
AllLocales | bool | AllLocales reports whether localized values contain maps keyed by locale. It describes this hook's values, which may differ from the requested response. |
Full signature
type HookContext struct {
// Context carries cancellation, deadlines, and the active transaction.
// Pass it to Local calls that must succeed or roll back with this operation.
Context context.Context
// Operation names the document operation, such as Create, Update, or Read,
// not the hook phase. Shared hooks should check it before running write-only work.
Operation operation.Kind
// WritePhase identifies draft saves versus publication writes independently
// of the row's current public status.
WritePhase operation.WritePhase
// CollectionID is the collection's stable resource ID, not its slug.
// It is empty for a global operation.
CollectionID schema.StableID
// GlobalID is the global's stable resource ID, not its slug.
// It is empty for a collection operation.
GlobalID schema.StableID
// Actor is the authenticated document, or nil for an anonymous operation.
Actor *store.Document
// ActorCollection identifies the exact auth collection that owns Actor.
ActorCollection schema.CollectionSlug
// System reports that trusted server code started this operation with
// System set, so no access rule ran. Nested Local calls do not inherit it:
// pass System again when the nested work is also trusted.
System bool
// Data contains the values being saved. On an update it holds only the
// top-level fields the caller sent; Candidate adds the unchanged ones from
// Original. Change map entries to change stored values; assigning a new map
// to Data does not replace the engine's values. Changes after saving are not
// persisted.
Data store.Values
// Document is the response document when available. AfterRead receives each
// document in a list separately. Changes to Document.Values affect the response,
// not stored data, and remain subject to field read access rules.
Document *store.Document
// Original is a detached copy of the saved document before an update, delete,
// or duplication. Changing it does not change storage.
Original *store.Document
// Local performs nested reads and writes through the normal operation engine.
// Pass Context to reuse the transaction, and pass Actor, ActorCollection, and
// locale options explicitly when the nested operation represents the same user.
// Standalone reads reject nested writes; AfterCommit starts new transactions.
Local *LocalAPI
// Error is set only for after-error hooks and preserves the original failure.
// Returning nil from AfterError does not make the failed operation succeed.
Error error
// Locale is the selected content language. Write hooks use the write locale;
// AfterRead uses the response locale, whose values may include fallback text.
Locale schema.LocaleCode
// AllLocales reports whether localized values contain maps keyed by locale.
// It describes this hook's values, which may differ from the requested response.
AllLocales bool
}Methods
HookContext.Candidate()Candidate returns the values the document will have once this write is saved: Original's values with each top-level entry of Data in their place. On a create it is a copy of Data. Use it to read a field whether or not the caller sent it, for example to check a rule that spans two fields on a partial update. The map is a detached copy; change Data, not the result, to change what is saved.