MCPcopy
hub / github.com/pocketbase/pocketbase / saveViewCollection

Function saveViewCollection

core/collection_query.go:261–297  ·  view source on GitHub ↗

------------------------------------------------------------------- saveViewCollection persists the provided View collection changes: - deletes the old related SQL view (if any) - creates a new SQL view with the latest newCollection.Options.Query - generates new feilds list based on newCollection.O

(app App, newCollection, oldCollection *Collection)

Source from the content-addressed store, hash-verified

259//
260// This method returns an error if newCollection is not a "view".
261func saveViewCollection(app App, newCollection, oldCollection *Collection) error {
262 if !newCollection.IsView() {
263 return errors.New("not a view collection")
264 }
265
266 return app.RunInTransaction(func(txApp App) error {
267 query := newCollection.ViewQuery
268
269 // generate collection fields from the query
270 viewFields, err := txApp.CreateViewFields(query)
271 if err != nil {
272 return err
273 }
274
275 // delete old renamed view
276 if oldCollection != nil {
277 if err := txApp.DeleteView(oldCollection.Name); err != nil {
278 return err
279 }
280 }
281
282 // wrap view query if necessary
283 query, err = normalizeViewQueryId(txApp, query)
284 if err != nil {
285 return fmt.Errorf("failed to normalize view query id: %w", err)
286 }
287
288 // (re)create the view
289 if err := txApp.SaveView(newCollection.Name, query); err != nil {
290 return err
291 }
292
293 newCollection.Fields = viewFields
294
295 return txApp.Save(newCollection)
296 })
297}
298
299// normalizeViewQueryId wraps (if necessary) the provided view query
300// with a subselect to ensure that the id column is a text since

Callers 1

Calls 7

normalizeViewQueryIdFunction · 0.85
IsViewMethod · 0.80
RunInTransactionMethod · 0.65
CreateViewFieldsMethod · 0.65
DeleteViewMethod · 0.65
SaveViewMethod · 0.65
SaveMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…