Last Updated: 3/9/2026
Event hooks
The standard way to modify PocketBase is through event hooks in your Go code.
All hooks have 3 main methods:
Bind(handler)adds a new handler to the specified event hook. A handler has 3 fields:Id(optional) - the name of the handler (could be used as argument forUnbind)Priority(optional) - the execution order of the handler (if empty fallbacks to the order of registration in the code).Func(required) - the handler function.
BindFunc(func)is similar toBindbut registers a new handler from just the provided function.
The registered handler is added with a default 0 priority and the id is autogenerated (the returned string value).Trigger(event, oneOffHandlerFuncs...)triggers the event hook.
This method rarely has to be called manually by users.
To remove an already registered hook handler, you can use the handler id and pass it to Unbind(id) or remove all handlers with UnbindAll() (!including system handlers).
All hook handler functions share the same func(e T) error signature and expect the user to call e.Next() if they want to proceed with the execution chain.
If you need to access the app instance from inside a hook handler, prefer using the e.App field instead of reusing a parent scope app variable because the hook could be part of a DB transaction and can cause deadlock.
Also avoid using global mutex locks inside a hook handler because it could be invoked recursively (e.g. cascade delete) and can cause deadlock.
You can explore all available hooks below:
App hooks
OnBootstrap hook is triggered when initializing the main application resources (db, app settings, etc).
Note that attempting to access the database before the e.Next() call will result in an error.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnBootstrap(). BindFunc(func(*.) error{if:=. Next();!= nil{return}// e.App return nil}) if:=. Start();!= nil{. Fatal()}}
OnServe hook is triggered when the app web server is started (after starting the TCP listener but before initializing the blocking serve task), allowing you to adjust its options and attach new routes or middlewares.
package import("log""net/http""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/apis""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnServe(). BindFunc(func(*.) error{// register new "GET /hello" route.. GET("/hello", func(*.) error{return. String(200,"Hello world!")}). Bind(. RequireAuth()) return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnSettingsReload hook is triggered every time when the App.Settings() is being replaced with a new state.
Calling e.App.Settings() after e.Next() returns the new state.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnSettingsReload(). BindFunc(func(*.) error{if:=. Next();!= nil{return}// e.App.Settings() return nil}) if:=. Start();!= nil{. Fatal()}}
OnBackupCreate is triggered on each App.CreateBackup call.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnBackupCreate(). BindFunc(func(*.) error{// e.App// e.Name - the name of the backup to create// e.Exclude - list of pb_data dir entries to exclude from the backup return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnBackupRestore is triggered before app backup restore (aka. on App.RestoreBackup call).
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnBackupRestore(). BindFunc(func(*.) error{// e.App// e.Name - the name of the backup to restore// e.Exclude - list of dir entries to exclude from the backup return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnTerminate hook is triggered when the app is in the process of being terminated (ex. on SIGTERM signal).
Note that the app could be terminated abruptly without awaiting the hook completion.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnTerminate(). BindFunc(func(*.) error{// e.App// e.IsRestart return. Next()}) if:=. Start();!= nil{. Fatal()}}
Mailer hooks
OnMailerSend hook is triggered every time when a new email is being sent using the App.NewMailClient() instance.
It allows intercepting the email message or to use a custom mailer client.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnMailerSend(). BindFunc(func(*.) error{// e.App// e.Mailer// e.Message// ex. change the mail subject.. = "new subject" return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnMailerRecordAuthAlertSend hook is triggered when sending a new device login auth alert email, allowing you to intercept and customize the email message that is being sent.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnMailerRecordAuthAlertSend(). BindFunc(func(*.) error{// e.App// e.Mailer// e.Message// e.Record// e.Meta["info"].(string)// ex. change the mail subject.. = "new subject" return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnMailerRecordPasswordResetSend hook is triggered when sending a password reset email to an auth record, allowing you to intercept and customize the email message that is being sent.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnMailerRecordPasswordResetSend(). BindFunc(func(*.) error{// e.App// e.Mailer// e.Message// e.Record// e.Meta["token"].(string)// ex. change the mail subject.. = "new subject" return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnMailerRecordVerificationSend hook is triggered when sending a verification email to an auth record, allowing you to intercept and customize the email message that is being sent.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnMailerRecordVerificationSend(). BindFunc(func(*.) error{// e.App// e.Mailer// e.Message// e.Record// e.Meta["token"].(string)// ex. change the mail subject.. = "new subject" return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnMailerRecordEmailChangeSend hook is triggered when sending a confirmation new address email to an auth record, allowing you to intercept and customize the email message that is being sent.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnMailerRecordEmailChangeSend(). BindFunc(func(*.) error{// e.App// e.Mailer// e.Message// e.Record// e.Meta["token"].(string)// e.Meta["newEmail"].(string)// ex. change the mail subject.. = "new subject" return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnMailerRecordOTPSend hook is triggered when sending an OTP email to an auth record, allowing you to intercept and customize the email message that is being sent.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnMailerRecordOTPSend(). BindFunc(func(*.) error{// e.App// e.Mailer// e.Message// e.Record// e.Meta["otpId"].(string)// e.Meta["password"].(string)// ex. change the mail subject.. = "new subject" return. Next()}) if:=. Start();!= nil{. Fatal()}}
Realtime hooks
OnRealtimeConnectRequest hook is triggered when establishing the SSE client connection.
Any execution after e.Next() of a hook handler happens after the client disconnects.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnRealtimeConnectRequest(). BindFunc(func(*.) error{// e.App// e.Client// e.IdleTimeout// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRealtimeSubscribeRequest hook is triggered when updating the client subscriptions, allowing you to further validate and modify the submitted change.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnRealtimeSubscribeRequest(). BindFunc(func(*.) error{// e.App// e.Client// e.Subscriptions// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRealtimeMessageSend hook is triggered when sending an SSE message to a client.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnRealtimeMessageSend(). BindFunc(func(*.) error{// e.App// e.Client// e.Message// and all original connect RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
Record model hooks
These are lower level Record model hooks and could be triggered from anywhere (custom console command, scheduled cron job, when calling e.Save(record), etc.) and therefore they have no access to the request context!
If you want to intercept the builtin Web APIs and to access their request body, query parameters, headers or the request auth state, then please use the designated Record *Request hooks .
OnRecordEnrich is triggered every time when a record is enriched - as part of the builtin Record responses, during realtime message serialization, or when apis.EnrichRecord is invoked.
It could be used for example to redact/hide or add computed temporary Record model props only for the specific request info.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnRecordEnrich("posts"). BindFunc(func(*.) error{// hide one or more fields.. Hide("role")// add new custom field for registered users if..!= nil &&... Collection(). == "users"{.. WithCustomData(true)// for security requires explicitly allowing it.. Set("computedScore",.. GetInt("score")*... GetInt("base"))} return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordValidate is a Record proxy model hook of OnModelValidate.
OnRecordValidate is called every time when a Record is being validated, e.g. triggered by App.Validate() or App.Save().
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordValidate(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordValidate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
Record model create hooks
OnRecordCreate is a Record proxy model hook of OnModelCreate.
OnRecordCreate is triggered every time when a new Record is being created, e.g. triggered by App.Save().
Operations BEFORE the e.Next() execute before the Record validation and the INSERT DB statement.
Operations AFTER the e.Next() execute after the Record validation and the INSERT DB statement.
Note that successful execution doesn’t guarantee that the Record is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnRecordAfterCreateSuccess or OnRecordAfterCreateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordCreate(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordCreate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordCreateExecute is a Record proxy model hook of OnModelCreateExecute.
OnRecordCreateExecute is triggered after successful Record validation and right before the model INSERT DB statement execution.
Usually it is triggered as part of the App.Save() in the following firing order:
OnRecordCreate
-> OnRecordValidate (skipped with App.SaveNoValidate())
-> OnRecordCreateExecute
Note that successful execution doesn’t guarantee that the Record is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnRecordAfterCreateSuccess or OnRecordAfterCreateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordCreateExecute(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordCreateExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAfterCreateSuccess is a Record proxy model hook of OnModelAfterCreateSuccess.
OnRecordAfterCreateSuccess is triggered after each successful Record DB create persistence.
Note that when a Record is persisted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordAfterCreateSuccess(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordAfterCreateSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAfterCreateError is a Record proxy model hook of OnModelAfterCreateError.
OnRecordAfterCreateError is triggered after each failed Record DB create persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Save()failure - delayed on transaction rollback
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordAfterCreateError(). BindFunc(func(*.) error{// e.App// e.Record// e.Error return. Next()})// fires only for "users" and "articles" records. OnRecordAfterCreateError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Record model update hooks
OnRecordUpdate is a Record proxy model hook of OnModelUpdate.
OnRecordUpdate is triggered every time when a new Record is being updated, e.g. triggered by App.Save().
Operations BEFORE the e.Next() execute before the Record validation and the UPDATE DB statement.
Operations AFTER the e.Next() execute after the Record validation and the UPDATE DB statement.
Note that successful execution doesn’t guarantee that the Record is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnRecordAfterUpdateSuccess or OnRecordAfterUpdateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordUpdate(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordUpdate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordUpdateExecute is a Record proxy model hook of OnModelUpdateExecute.
OnRecordUpdateExecute is triggered after successful Record validation and right before the model UPDATE DB statement execution.
Usually it is triggered as part of the App.Save() in the following firing order:
OnRecordUpdate
-> OnRecordValidate (skipped with App.SaveNoValidate())
-> OnRecordUpdateExecute
Note that successful execution doesn’t guarantee that the Record is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnRecordAfterUpdateSuccess or OnRecordAfterUpdateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordUpdateExecute(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordUpdateExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAfterUpdateSuccess is a Record proxy model hook of OnModelAfterUpdateSuccess.
OnRecordAfterUpdateSuccess is triggered after each successful Record DB update persistence.
Note that when a Record is persisted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordAfterUpdateSuccess(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordAfterUpdateSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAfterUpdateError is a Record proxy model hook of OnModelAfterUpdateError.
OnRecordAfterUpdateError is triggered after each failed Record DB update persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Save()failure - delayed on transaction rollback
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordAfterUpdateError(). BindFunc(func(*.) error{// e.App// e.Record// e.Error return. Next()})// fires only for "users" and "articles" records. OnRecordAfterUpdateError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Record model delete hooks
OnRecordDelete is a Record proxy model hook of OnModelDelete.
OnRecordDelete is triggered every time when a new Record is being deleted, e.g. triggered by App.Delete().
Operations BEFORE the e.Next() execute before the Record validation and the UPDATE DB statement.
Operations AFTER the e.Next() execute after the Record validation and the UPDATE DB statement.
Note that successful execution doesn’t guarantee that the Record is deleted from the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted deleted events, you can bind to OnRecordAfterDeleteSuccess or OnRecordAfterDeleteError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordDelete(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordDelete("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordDeleteExecute is a Record proxy model hook of OnModelDeleteExecute.
OnRecordDeleteExecute is triggered after the internal delete checks and right before the Record the model DELETE DB statement execution.
Usually it is triggered as part of the App.Delete() in the following firing order:
OnRecordDelete
-> internal delete checks
-> OnRecordDeleteExecute
Note that successful execution doesn’t guarantee that the Record is deleted from the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnRecordAfterDeleteSuccess or OnRecordAfterDeleteError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordDeleteExecute(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordDeleteExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAfterDeleteSuccess is a Record proxy model hook of OnModelAfterDeleteSuccess.
OnRecordAfterDeleteSuccess is triggered after each successful Record DB delete persistence.
Note that when a Record is deleted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordAfterDeleteSuccess(). BindFunc(func(*.) error{// e.App// e.Record return. Next()})// fires only for "users" and "articles" records. OnRecordAfterDeleteSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAfterDeleteError is a Record proxy model hook of OnModelAfterDeleteError.
OnRecordAfterDeleteError is triggered after each failed Record DB delete persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Delete()failure - delayed on transaction rollback
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every record. OnRecordAfterDeleteError(). BindFunc(func(*.) error{// e.App// e.Record// e.Error return. Next()})// fires only for "users" and "articles" records. OnRecordAfterDeleteError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Record// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Collection model hooks
These are lower level Collection model hooks and could be triggered from anywhere (custom console command, scheduled cron job, when calling e.Save(collection), etc.) and therefore they have no access to the request context!
If you want to intercept the builtin Web APIs and to access their request body, query parameters, headers or the request auth state, then please use the designated Collection *Request hooks .
OnCollectionValidate is a Collection proxy model hook of OnModelValidate.
OnCollectionValidate is called every time when a Collection is being validated, e.g. triggered by App.Validate() or App.Save().
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionValidate(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionValidate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
Collection mode create hooks
OnCollectionCreate is a Collection proxy model hook of OnModelCreate.
OnCollectionCreate is triggered every time when a new Collection is being created, e.g. triggered by App.Save().
Operations BEFORE the e.Next() execute before the Collection validation and the INSERT DB statement.
Operations AFTER the e.Next() execute after the Collection validation and the INSERT DB statement.
Note that successful execution doesn’t guarantee that the Collection is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnCollectionAfterCreateSuccess or OnCollectionAfterCreateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionCreate(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionCreate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionCreateExecute is a Collection proxy model hook of OnModelCreateExecute.
OnCollectionCreateExecute is triggered after successful Collection validation and right before the model INSERT DB statement execution.
Usually it is triggered as part of the App.Save() in the following firing order:
OnCollectionCreate
-> OnCollectionValidate (skipped with App.SaveNoValidate())
-> OnCollectionCreateExecute
Note that successful execution doesn’t guarantee that the Collection is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnCollectionAfterCreateSuccess or OnCollectionAfterCreateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionCreateExecute(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionCreateExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionAfterCreateSuccess is a Collection proxy model hook of OnModelAfterCreateSuccess.
OnCollectionAfterCreateSuccess is triggered after each successful Collection DB create persistence.
Note that when a Collection is persisted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionAfterCreateSuccess(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionAfterCreateSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionAfterCreateError is a Collection proxy model hook of OnModelAfterCreateError.
OnCollectionAfterCreateError is triggered after each failed Collection DB create persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Save()failure - delayed on transaction rollback
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionAfterCreateError(). BindFunc(func(*.) error{// e.App// e.Collection// e.Error return. Next()})// fires only for "users" and "articles" collections. OnCollectionAfterCreateError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Collection mode update hooks
OnCollectionUpdate is a Collection proxy model hook of OnModelUpdate.
OnCollectionUpdate is triggered every time when a new Collection is being updated, e.g. triggered by App.Save().
Operations BEFORE the e.Next() execute before the Collection validation and the UPDATE DB statement.
Operations AFTER the e.Next() execute after the Collection validation and the UPDATE DB statement.
Note that successful execution doesn’t guarantee that the Collection is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnCollectionAfterUpdateSuccess or OnCollectionAfterUpdateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionUpdate(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionUpdate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionUpdateExecute is a Collection proxy model hook of OnModelUpdateExecute.
OnCollectionUpdateExecute is triggered after successful Collection validation and right before the model UPDATE DB statement execution.
Usually it is triggered as part of the App.Save() in the following firing order:
OnCollectionUpdate
-> OnCollectionValidate (skipped with App.SaveNoValidate())
-> OnCollectionUpdateExecute
Note that successful execution doesn’t guarantee that the Collection is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnCollectionAfterUpdateSuccess or OnCollectionAfterUpdateError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionUpdateExecute(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionUpdateExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionAfterUpdateSuccess is a Collection proxy model hook of OnModelAfterUpdateSuccess.
OnCollectionAfterUpdateSuccess is triggered after each successful Collection DB update persistence.
Note that when a Collection is persisted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionAfterUpdateSuccess(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionAfterUpdateSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionAfterUpdateError is a Collection proxy model hook of OnModelAfterUpdateError.
OnCollectionAfterUpdateError is triggered after each failed Collection DB update persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Save()failure - delayed on transaction rollback
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionAfterUpdateError(). BindFunc(func(*.) error{// e.App// e.Collection// e.Error return. Next()})// fires only for "users" and "articles" collections. OnCollectionAfterUpdateError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Collection mode delete hooks
OnCollectionDelete is a Collection proxy model hook of OnModelDelete.
OnCollectionDelete is triggered every time when a new Collection is being deleted, e.g. triggered by App.Delete().
Operations BEFORE the e.Next() execute before the Collection validation and the UPDATE DB statement.
Operations AFTER the e.Next() execute after the Collection validation and the UPDATE DB statement.
Note that successful execution doesn’t guarantee that the Collection is deleted from the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted deleted events, you can bind to OnCollectionAfterDeleteSuccess or OnCollectionAfterDeleteError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionDelete(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionDelete("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionDeleteExecute is a Collection proxy model hook of OnModelDeleteExecute.
OnCollectionDeleteExecute is triggered after the internal delete checks and right before the Collection the model DELETE DB statement execution.
Usually it is triggered as part of the App.Delete() in the following firing order:
OnCollectionDelete
-> internal delete checks
-> OnCollectionDeleteExecute
Note that successful execution doesn’t guarantee that the Collection is deleted from the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnCollectionAfterDeleteSuccess or OnCollectionAfterDeleteError hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionDeleteExecute(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionDeleteExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionAfterDeleteSuccess is a Collection proxy model hook of OnModelAfterDeleteSuccess.
OnCollectionAfterDeleteSuccess is triggered after each successful Collection DB delete persistence.
Note that when a Collection is deleted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionAfterDeleteSuccess(). BindFunc(func(*.) error{// e.App// e.Collection return. Next()})// fires only for "users" and "articles" collections. OnCollectionAfterDeleteSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionAfterDeleteError is a Collection proxy model hook of OnModelAfterDeleteError.
OnCollectionAfterDeleteError is triggered after each failed Collection DB delete persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Delete()failure - delayed on transaction rollback
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnCollectionAfterDeleteError(). BindFunc(func(*.) error{// e.App// e.Collection// e.Error return. Next()})// fires only for "users" and "articles" collections. OnCollectionAfterDeleteError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Request hooks
The request hooks are triggered only when the corresponding API request endpoint is accessed.
Record CRUD request hooks
OnRecordsListRequest hook is triggered on each API Records list request. Could be used to validate or modify the response before returning it to the client.
Note that if you want to hide existing or add new computed Record fields prefer using the OnRecordEnrich hook because it is less error-prone and it is triggered by all builtin Record responses (including when sending realtime Record events).
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnRecordsListRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Records// e.Result// and all RequestEvent fields... return. Next()})// fires only for "users" and "articles" collections. OnRecordsListRequest("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection// e.Records// e.Result// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordViewRequest hook is triggered on each API Record view request. Could be used to validate or modify the response before returning it to the client.
Note that if you want to hide existing or add new computed Record fields prefer using the OnRecordEnrich hook because it is less error-prone and it is triggered by all builtin Record responses (including when sending realtime Record events).
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnRecordViewRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "articles" collections. OnRecordViewRequest("users", "articles"). BindFunc(func(*.) error{. Println(.). Println(.) return nil}) if:=. Start();!= nil{. Fatal()}}
OnRecordCreateRequest hook is triggered on each API Record create request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnRecordCreateRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "articles" collections. OnRecordCreateRequest("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordUpdateRequest hook is triggered on each API Record update request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnRecordUpdateRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "articles" collections. OnRecordUpdateRequest("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordDeleteRequest hook is triggered on each API Record delete request.
Could be used to additionally validate the request data or implement completely different delete behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every collection. OnRecordDeleteRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "articles" collections. OnRecordDeleteRequest("users", "articles"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
Record auth request hooks
OnRecordAuthRequest hook is triggered on each successful API record authentication request (sign-in, token refresh, etc.). Could be used to additionally validate or modify the authenticated record data and token.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordAuthRequest(). BindFunc(func(*.) error{// e.App// e.Record// e.Token// e.Meta// e.AuthMethod// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordAuthRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Record// e.Token// e.Meta// e.AuthMethod// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAuthRefreshRequest hook is triggered on each Record auth refresh API request (right before generating a new auth token).
Could be used to additionally validate the request data or implement completely different auth refresh behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordAuthRefreshRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordAuthRefreshRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAuthWithPasswordRequest hook is triggered on each Record auth with password API request.
e.Record could be nil if no matching identity is found, allowing you to manually locate a different Record model (by reassigning e.Record).
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordAuthWithPasswordRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record (could be nil)// e.Identity// e.IdentityField// e.Password// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordAuthWithPasswordRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record (could be nil)// e.Identity// e.IdentityField// e.Password// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAuthWithOAuth2Request hook is triggered on each Record OAuth2 sign-in/sign-up API request (after token exchange and before external provider linking).
If e.Record is not set, then the OAuth2 request will try to create a new auth record.
To assign or link a different existing record model you can change the e.Record field.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordAuthWithOAuth2Request(). BindFunc(func(*.) error{// e.App// e.Collection// e.ProviderName// e.ProviderClient// e.Record (could be nil)// e.OAuth2User// e.CreateData// e.IsNewRecord// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordAuthWithOAuth2Request("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.ProviderName// e.ProviderClient// e.Record (could be nil)// e.OAuth2User// e.CreateData// e.IsNewRecord// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordRequestPasswordResetRequest hook is triggered on each Record request password reset API request.
Could be used to additionally validate the request data or implement completely different password reset behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordRequestPasswordResetRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordRequestPasswordResetRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordConfirmPasswordResetRequest hook is triggered on each Record confirm password reset API request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordConfirmPasswordResetRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordConfirmPasswordResetRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordRequestVerificationRequest hook is triggered on each Record request verification API request.
Could be used to additionally validate the loaded request data or implement completely different verification behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordRequestVerificationRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordRequestVerificationRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordConfirmVerificationRequest hook is triggered on each Record confirm verification API request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordConfirmVerificationRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordConfirmVerificationRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordRequestEmailChangeRequest hook is triggered on each Record request email change API request.
Could be used to additionally validate the request data or implement completely different request email change behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordRequestEmailChangeRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// e.NewEmail// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordRequestEmailChangeRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// e.NewEmail// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordConfirmEmailChangeRequest hook is triggered on each Record confirm email change API request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordConfirmEmailChangeRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// e.NewEmail// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordConfirmEmailChangeRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// e.NewEmail// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordRequestOTPRequest hook is triggered on each Record request OTP API request.
e.Record could be nil if no user with the requested email is found, allowing you to manually create a new Record or locate a different Record model (by reassigning e.Record).
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordRequestOTPRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record (could be nil)// e.Password// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordRequestOTPRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record (could be nil)// e.Password// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnRecordAuthWithOTPRequest hook is triggered on each Record auth with OTP API request.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth collection. OnRecordAuthWithOTPRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// e.OTP// and all RequestEvent fields... return. Next()})// fires only for "users" and "managers" auth collections. OnRecordAuthWithOTPRequest("users", "managers"). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// e.OTP// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
Batch request hooks
OnBatchRequest hook is triggered on each API batch request.
Could be used to additionally validate or modify the submitted batch requests.
This hook will also fire the corresponding OnRecordCreateRequest, OnRecordUpdateRequest, OnRecordDeleteRequest hooks, where e.App is the batch transactional app.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnBatchRequest(). BindFunc(func(*.) error{// e.App// e.Batch// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
File request hooks
OnFileDownloadRequest hook is triggered before each API File download request. Could be used to validate or modify the file response before returning it to the client.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnFileDownloadRequest(). BindFunc(func(*.) error{// e.App// e.Collection// e.Record// e.FileField// e.ServedPath// e.ServedName// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnFileTokenRequest hook is triggered on each auth file token API request.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every auth model. OnFileTokenRequest(). BindFunc(func(*.) error{// e.App// e.Record// e.Token// and all RequestEvent fields... return. Next();})// fires only for "users". OnFileTokenRequest("users"). BindFunc(func(*.) error{// e.App// e.Record// e.Token// and all RequestEvent fields... return. Next();}) if:=. Start();!= nil{. Fatal()}}
Collection request hooks
OnCollectionsListRequest hook is triggered on each API Collections list request. Could be used to validate or modify the response before returning it to the client.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnCollectionsListRequest(). BindFunc(func(*.) error{// e.App// e.Collections// e.Result// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionViewRequest hook is triggered on each API Collection view request. Could be used to validate or modify the response before returning it to the client.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnCollectionViewRequest(). BindFunc(func(*.) error{// e.App// e.Collection// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionCreateRequest hook is triggered on each API Collection create request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnCollectionCreateRequest(). BindFunc(func(*.) error{// e.App// e.Collection// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionUpdateRequest hook is triggered on each API Collection update request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnCollectionUpdateRequest(). BindFunc(func(*.) error{// e.App// e.Collection// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionDeleteRequest hook is triggered on each API Collection delete request.
Could be used to additionally validate the request data or implement completely different delete behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnCollectionDeleteRequest(). BindFunc(func(*.) error{// e.App// e.Collection// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnCollectionsImportRequest hook is triggered on each API collections import request.
Could be used to additionally validate the imported collections or to implement completely different import behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnCollectionsImportRequest(). BindFunc(func(*.) error{// e.App// e.CollectionsData// e.DeleteMissing return. Next()}) if:=. Start();!= nil{. Fatal()}}
Settings request hooks
OnSettingsListRequest hook is triggered on each API Settings list request.
Could be used to validate or modify the response before returning it to the client.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnSettingsListRequest(). BindFunc(func(*.) error{// e.App// e.Settings// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnSettingsUpdateRequest hook is triggered on each API Settings update request.
Could be used to additionally validate the request data or implement completely different persistence behavior.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New(). OnSettingsUpdateRequest(). BindFunc(func(*.) error{// e.App// e.OldSettings// e.NewSettings// and all RequestEvent fields... return. Next()}) if:=. Start();!= nil{. Fatal()}}
Base model hooks
The Model hooks are fired for all PocketBase structs that implements the Model DB interface - Record, Collection, Log, etc.
For convenience, if you want to listen to only the Record or Collection DB model events without doing manual type assertion, you can use the OnRecord* and OnCollection* proxy hooks above.
OnModelValidate is called every time when a Model is being validated, e.g. triggered by App.Validate() or App.Save().
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelValidate(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelValidate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
Base model create hooks
OnModelCreate is triggered every time when a new Model is being created, e.g. triggered by App.Save().
Operations BEFORE the e.Next() execute before the Model validation and the INSERT DB statement.
Operations AFTER the e.Next() execute after the Model validation and the INSERT DB statement.
Note that successful execution doesn’t guarantee that the Model is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnModelAfterCreateSuccess or OnModelAfterCreateError hooks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelCreate(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelCreate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelCreateExecute is triggered after successful Model validation and right before the model INSERT DB statement execution.
Usually it is triggered as part of the App.Save() in the following firing order:
OnModelCreate
-> OnModelValidate (skipped with App.SaveNoValidate())
-> OnModelCreateExecute
Note that successful execution doesn’t guarantee that the Model is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnModelAfterCreateSuccess or OnModelAfterCreateError hooks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelCreateExecute(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelCreateExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelAfterCreateSuccess is triggered after each successful Model DB create persistence.
Note that when a Model is persisted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelAfterCreateSuccess(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelAfterCreateSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelAfterCreateError is triggered after each failed Model DB create persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Save()failure - delayed on transaction rollback
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelAfterCreateError(). BindFunc(func(*.) error{// e.App// e.Model// e.Error return. Next()})// fires only for "users" and "articles" models. OnModelAfterCreateError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Base model update hooks
OnModelUpdate is triggered every time when a new Model is being updated, e.g. triggered by App.Save().
Operations BEFORE the e.Next() execute before the Model validation and the UPDATE DB statement.
Operations AFTER the e.Next() execute after the Model validation and the UPDATE DB statement.
Note that successful execution doesn’t guarantee that the Model is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnModelAfterUpdateSuccess or OnModelAfterUpdateError hooks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelUpdate(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelUpdate("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelUpdateExecute is triggered after successful Model validation and right before the model UPDATE DB statement execution.
Usually it is triggered as part of the App.Save() in the following firing order:
OnModelUpdate
-> OnModelValidate (skipped with App.SaveNoValidate())
-> OnModelUpdateExecute
Note that successful execution doesn’t guarantee that the Model is persisted in the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnModelAfterUpdateSuccess or OnModelAfterUpdateError hooks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelUpdateExecute(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelUpdateExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelAfterUpdateSuccess is triggered after each successful Model DB update persistence.
Note that when a Model is persisted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelAfterUpdateSuccess(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelAfterUpdateSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelAfterUpdateError is triggered after each failed Model DB update persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Save()failure - delayed on transaction rollback
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelAfterUpdateError(). BindFunc(func(*.) error{// e.App// e.Model// e.Error return. Next()})// fires only for "users" and "articles" models. OnModelAfterUpdateError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}
Base model delete hooks
OnModelDelete is triggered every time when a new Model is being deleted, e.g. triggered by App.Delete().
Operations BEFORE the e.Next() execute before the Model validation and the UPDATE DB statement.
Operations AFTER the e.Next() execute after the Model validation and the UPDATE DB statement.
Note that successful execution doesn’t guarantee that the Model is deleted from the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted deleted events, you can bind to OnModelAfterDeleteSuccess or OnModelAfterDeleteError hooks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelDelete(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelDelete("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelDeleteExecute is triggered after the internal delete checks and right before the Model the model DELETE DB statement execution.
Usually it is triggered as part of the App.Delete() in the following firing order:
OnModelDelete
-> internal delete checks
-> OnModelDeleteExecute
Note that successful execution doesn’t guarantee that the Model is deleted from the database since its wrapping transaction may not have been committed yet. If you want to listen to only the actual persisted events, you can bind to OnModelAfterDeleteSuccess or OnModelAfterDeleteError hooks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelDeleteExecute(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelDeleteExecute("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelAfterDeleteSuccess is triggered after each successful Model DB delete persistence.
Note that when a Model is deleted as part of a transaction, this hook is delayed and executed only AFTER the transaction has been committed. This hook is NOT triggered in case the transaction fails/rollbacks.
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelAfterDeleteSuccess(). BindFunc(func(*.) error{// e.App// e.Model return. Next()})// fires only for "users" and "articles" models. OnModelAfterDeleteSuccess("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model return. Next()}) if:=. Start();!= nil{. Fatal()}}
OnModelAfterDeleteError is triggered after each failed Model DB delete persistence.
Note that the execution of this hook is either immediate or delayed depending on the error:
- immediate on
App.Delete()failure - delayed on transaction rollback
For convenience, if you want to listen to only the Record or Collection models events without doing manual type assertion, you can use the equivalent OnRecord* and OnCollection* proxy hooks.
package import("log""github.com/pocketbase/pocketbase""github.com/pocketbase/pocketbase/core") func main(){:=. New()// fires for every model. OnModelAfterDeleteError(). BindFunc(func(*.) error{// e.App// e.Model// e.Error return. Next()})// fires only for "users" and "articles" models. OnModelAfterDeleteError("users", "articles"). BindFunc(func(*.) error{// e.App// e.Model// e.Error return. Next()}) if:=. Start();!= nil{. Fatal()}}