MCPcopy
hub / github.com/pocketbase/pocketbase / collectionCreate

Function collectionCreate

apis/collection.go:78–116  ·  view source on GitHub ↗
(e *core.RequestEvent)

Source from the content-addressed store, hash-verified

76}
77
78func collectionCreate(e *core.RequestEvent) error {
79 // populate the minimal required factory collection data (if any)
80 factoryExtract := struct {
81 Type string `form:"type" json:"type"`
82 Name string `form:"name" json:"name"`
83 }{}
84 if err := e.BindBody(&factoryExtract); err != nil {
85 return e.BadRequestError("Failed to load the collection type data due to invalid formatting.", err)
86 }
87
88 // create scaffold
89 collection := core.NewCollection(factoryExtract.Type, factoryExtract.Name)
90
91 // merge the scaffold with the submitted request data
92 if err := e.BindBody(collection); err != nil {
93 return e.BadRequestError("Failed to load the submitted data due to invalid formatting.", err)
94 }
95
96 event := new(core.CollectionRequestEvent)
97 event.RequestEvent = e
98 event.Collection = collection
99
100 return e.App.OnCollectionCreateRequest().Trigger(event, func(e *core.CollectionRequestEvent) error {
101 if err := e.App.Save(e.Collection); err != nil {
102 // validation failure
103 var validationErrors validation.Errors
104 if errors.As(err, &validationErrors) {
105 return e.BadRequestError("Failed to create collection.", validationErrors)
106 }
107
108 // other generic db error
109 return e.BadRequestError("Failed to create collection. Raw error: \n"+err.Error(), nil)
110 }
111
112 return execAfterSuccessTx(true, e.App, func() error {
113 return e.JSON(http.StatusOK, e.Collection)
114 })
115 })
116}
117
118func collectionUpdate(e *core.RequestEvent) error {
119 collection, err := e.App.FindCollectionByNameOrId(e.Request.PathValue("collection"))

Callers

nothing calls this directly

Calls 9

NewCollectionFunction · 0.92
execAfterSuccessTxFunction · 0.85
BindBodyMethod · 0.80
BadRequestErrorMethod · 0.80
TriggerMethod · 0.80
JSONMethod · 0.80
SaveMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…