MCPcopy Index your code
hub / github.com/pocketbase/pocketbase / batchTransaction

Function batchTransaction

apis/batch.go:94–167  ·  view source on GitHub ↗

NB! When the request is submitted as multipart/form-data, the regular fields data is expected to be submitted as serialized json under the @jsonPayload field and file keys need to follow the pattern "requests.N.fileField" or requests[N].fileField.

(e *core.RequestEvent)

Source from the content-addressed store, hash-verified

92// json under the @jsonPayload field and file keys need to follow the
93// pattern "requests.N.fileField" or requests[N].fileField.
94func batchTransaction(e *core.RequestEvent) error {
95 maxRequests := e.App.Settings().Batch.MaxRequests
96 if !e.App.Settings().Batch.Enabled || maxRequests <= 0 {
97 return e.ForbiddenError("Batch requests are not allowed.", nil)
98 }
99
100 txTimeout := time.Duration(e.App.Settings().Batch.Timeout) * time.Second
101 if txTimeout <= 0 {
102 txTimeout = 3 * time.Second // for now always limit
103 }
104
105 maxBodySize := e.App.Settings().Batch.MaxBodySize
106 if maxBodySize <= 0 {
107 maxBodySize = 128 << 20
108 }
109
110 err := applyBodyLimit(e, maxBodySize)
111 if err != nil {
112 return err
113 }
114
115 form := &batchRequestsForm{max: maxRequests}
116
117 // load base requests data
118 err = e.BindBody(form)
119 if err != nil {
120 return e.BadRequestError("Failed to read the submitted batch data.", err)
121 }
122
123 // load uploaded files into each request item
124 // note: expects the files to be under "requests.N.fileField" or "requests[N].fileField" format
125 // (the other regular fields must be put under `@jsonPayload` as serialized json)
126 if strings.HasPrefix(e.Request.Header.Get("Content-Type"), "multipart/form-data") {
127 for i, ir := range form.Requests {
128 iStr := strconv.Itoa(i)
129
130 files, err := extractPrefixedFiles(e.Request, "requests."+iStr+".", "requests["+iStr+"].")
131 if err != nil {
132 return e.BadRequestError("Failed to read the submitted batch files data.", err)
133 }
134
135 for key, files := range files {
136 if ir.Body == nil {
137 ir.Body = map[string]any{}
138 }
139 ir.Body[key] = files
140 }
141 }
142 }
143
144 // validate batch request form
145 err = form.validate()
146 if err != nil {
147 return e.BadRequestError("Invalid batch request data.", err)
148 }
149
150 event := new(core.BatchRequestEvent)
151 event.RequestEvent = e

Callers

nothing calls this directly

Calls 13

validateMethod · 0.95
ProcessMethod · 0.95
applyBodyLimitFunction · 0.85
extractPrefixedFilesFunction · 0.85
firstApiErrorFunction · 0.85
ForbiddenErrorMethod · 0.80
BindBodyMethod · 0.80
BadRequestErrorMethod · 0.80
TriggerMethod · 0.80
JSONMethod · 0.80
SettingsMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…