Sometimes you may want to create or edit a collection programmatically (eg. as part of a DB migration). You could find all available Collection related db methods in the daos godoc, but here are some commonly used ones:

    Fetch collections

    Fetch collection by name or id
    collection, err := app.Dao().FindCollectionByNameOrId("example")
    Fetch collections by type
    baseCollections, err := app.Dao().FindCollectionsByType(models.CollectionTypeBase) authCollections, err := app.Dao().FindCollectionsByType(models.CollectionTypeAuth)

    Create new collection

    Create new collection WITHOUT data validations
    import ( "github.com/pocketbase/pocketbase/models" "github.com/pocketbase/pocketbase/models/schema" "github.com/pocketbase/pocketbase/tools/types" ) ... collection := &models.Collection{ Name: "example", Type: models.CollectionTypeBase, ListRule: nil, ViewRule: types.Pointer("@request.auth.id != ''"), CreateRule: types.Pointer(""), UpdateRule: types.Pointer("@request.auth.id != ''"), DeleteRule: nil, Schema: schema.NewSchema( &schema.SchemaField{ Name: "title", Type: schema.FieldTypeText, Required: true, Unique: true, Options: &schema.TextOptions{ Max: types.Pointer(10), }, }, &schema.SchemaField{ Name: "user", Type: schema.FieldTypeRelation, Required: true, Options: &schema.RelationOptions{ MaxSelect: types.Pointer(1), CollectionId: "ae40239d2bc4477", CascadeDelete: true, }, }, ), } // the id is autogenerated, but you can set a specific one if you want to: // collection.SetId("...") // collection.MarkAsNew() if err := app.Dao().SaveCollection(collection); err != nil { return err }
    Create new collection WITH data validations
    import ( "github.com/pocketbase/pocketbase/forms" "github.com/pocketbase/pocketbase/models" "github.com/pocketbase/pocketbase/models/schema" "github.com/pocketbase/pocketbase/tools/types" ) ... collection := &models.Collection{} form := forms.NewCollectionUpsert(app, collection) form.Name = "example" form.Type = models.CollectionTypeBase form.ListRule = nil form.ViewRule = types.Pointer("@request.auth.id != ''") form.CreateRule = types.Pointer("") form.UpdateRule = types.Pointer("@request.auth.id != ''") form.DeleteRule = nil form.Schema.AddField(&schema.SchemaField{ Name: "title", Type: schema.FieldTypeText, Required: true, Unique: true, Options: &schema.TextOptions{ Max: types.Pointer(10), }, }) form.Schema.AddField(&schema.SchemaField{ Name: "user", Type: schema.FieldTypeRelation, Required: true, Options: &schema.RelationOptions{ MaxSelect: types.Pointer(1), CollectionId: "ae40239d2bc4477", CascadeDelete: true, }, }) // validate and submit (internally it calls app.Dao().SaveCollection(collection) in a transaction) if err := form.Submit(); err != nil { return err }

    Update existing collection

    Update collection WITHOUT data validations
    import ( "github.com/pocketbase/pocketbase/models/schema" ) ... collection, err := app.Dao().FindCollectionByNameOrId("example") if err != nil { return err } collection.Name = "example_update" collection.Schema.AddField(&schema.SchemaField{ Name: "description", Type: schema.FieldTypeText, }) if err := app.Dao().SaveCollection(collection); err != nil { return err }
    Update collection WITH data validations
    import ( "github.com/pocketbase/pocketbase/forms" "github.com/pocketbase/pocketbase/models/schema" ) ... collection, err := app.Dao().FindCollectionByNameOrId("example") if err != nil { return err } form := forms.NewCollectionUpsert(app, collection) form.Name = "example_update" form.Schema.AddField(&schema.SchemaField{ Name: "description", Type: schema.FieldTypeText, }) // validate and submit (internally it calls app.Dao().SaveCollection(collection) in a transaction) if err := form.Submit(); err != nil { return err }

    Delete collection

    collection, err := app.Dao().FindCollectionByNameOrId("example") if err != nil { return err } if err := app.Dao().DeleteCollection(collection); err != nil { return err }