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

Method goCreateTemplate

plugins/migratecmd/templates.go:368–419  ·  view source on GitHub ↗
(collection *core.Collection)

Source from the content-addressed store, hash-verified

366}
367
368func (p *plugin) goCreateTemplate(collection *core.Collection) (string, error) {
369 // unset timestamp fields
370 collectionData, err := toMap(collection)
371 if err != nil {
372 return "", err
373 }
374 delete(collectionData, "created")
375 delete(collectionData, "updated")
376 deleteNestedMapKey(collectionData, "oauth2", "providers")
377
378 jsonData, err := marhshalWithoutEscape(collectionData, "\t\t", "\t")
379 if err != nil {
380 return "", fmt.Errorf("failed to serialize collections list: %w", err)
381 }
382
383 const template = `package %s
384
385import (
386 "encoding/json"
387
388 "github.com/pocketbase/pocketbase/core"
389 m "github.com/pocketbase/pocketbase/migrations"
390)
391
392func init() {
393 m.Register(func(app core.App) error {
394 jsonData := ` + "`%s`" + `
395
396 collection := &core.Collection{}
397 if err := json.Unmarshal([]byte(jsonData), &collection); err != nil {
398 return err
399 }
400
401 return app.Save(collection)
402 }, func(app core.App) error {
403 collection, err := app.FindCollectionByNameOrId(%q)
404 if err != nil {
405 return err
406 }
407
408 return app.Delete(collection)
409 })
410}
411`
412
413 return fmt.Sprintf(
414 template,
415 filepath.Base(p.config.Dir),
416 escapeBacktick(string(jsonData)),
417 collection.Id,
418 ), nil
419}
420
421func (p *plugin) goDeleteTemplate(collection *core.Collection) (string, error) {
422 // unset timestamp fields

Callers 1

goDiffTemplateMethod · 0.95

Calls 4

toMapFunction · 0.85
deleteNestedMapKeyFunction · 0.85
marhshalWithoutEscapeFunction · 0.85
escapeBacktickFunction · 0.85

Tested by

no test coverage detected