MCPcopy
hub / github.com/pocketbase/pocketbase / recordRefHooks

Function recordRefHooks

core/auth_origin_model.go:146–239  ·  view source on GitHub ↗

------------------------------------------------------------------- recordRefHooks registers common hooks that are usually used with record proxies that have polymorphic record relations (aka. "collectionRef" and "recordRef" fields).

(app App, collectionName string, optCollectionTypes ...string)

Source from the content-addressed store, hash-verified

144// recordRefHooks registers common hooks that are usually used with record proxies
145// that have polymorphic record relations (aka. "collectionRef" and "recordRef" fields).
146func recordRefHooks[T RecordProxy](app App, collectionName string, optCollectionTypes ...string) {
147 app.OnRecordValidate(collectionName).Bind(&hook.Handler[*RecordEvent]{
148 Func: func(e *RecordEvent) error {
149 collectionId := e.Record.GetString("collectionRef")
150 err := validation.Validate(collectionId, validation.Required, validation.By(validateCollectionId(e.App, optCollectionTypes...)))
151 if err != nil {
152 return validation.Errors{"collectionRef": err}
153 }
154
155 recordId := e.Record.GetString("recordRef")
156 err = validation.Validate(recordId, validation.Required, validation.By(validateRecordId(e.App, collectionId)))
157 if err != nil {
158 return validation.Errors{"recordRef": err}
159 }
160
161 return e.Next()
162 },
163 Priority: 99,
164 })
165
166 // delete on collection ref delete
167 app.OnCollectionDeleteExecute().Bind(&hook.Handler[*CollectionEvent]{
168 Func: func(e *CollectionEvent) error {
169 if e.Collection.Name == collectionName || (len(optCollectionTypes) > 0 && !slices.Contains(optCollectionTypes, e.Collection.Type)) {
170 return e.Next()
171 }
172
173 originalApp := e.App
174 txErr := e.App.RunInTransaction(func(txApp App) error {
175 e.App = txApp
176
177 if err := e.Next(); err != nil {
178 return err
179 }
180
181 rels, err := txApp.FindAllRecords(collectionName, dbx.HashExp{"collectionRef": e.Collection.Id})
182 if err != nil {
183 return err
184 }
185
186 for _, mfa := range rels {
187 if err := txApp.Delete(mfa); err != nil {
188 return err
189 }
190 }
191
192 return nil
193 })
194 e.App = originalApp
195
196 return txErr
197 },
198 Priority: 99,
199 })
200
201 // delete on record ref delete
202 app.OnRecordDeleteExecute().Bind(&hook.Handler[*RecordEvent]{
203 Func: func(e *RecordEvent) error {

Callers

nothing calls this directly

Calls 13

validateCollectionIdFunction · 0.85
validateRecordIdFunction · 0.85
GetStringMethod · 0.80
CollectionMethod · 0.80
OnRecordValidateMethod · 0.65
ValidateMethod · 0.65
NextMethod · 0.65
RunInTransactionMethod · 0.65
FindAllRecordsMethod · 0.65
DeleteMethod · 0.65
OnRecordDeleteExecuteMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…