Collections are usually managed via the Admin UI, but there are some situations where you may want to create or edit a collection programmatically (usually as part of a DB migration). PocketBase exposes several helpers to simplify the Collection model operations.

    collection, err := app.Dao().FindCollectionByNameOrId("example")
    baseCollections, err := app.Dao().FindCollectionsByType(models.CollectionTypeBase) authCollections, err := app.Dao().FindCollectionsByType(models.CollectionTypeAuth) viewCollections, err := app.Dao().FindCollectionsByType(models.CollectionTypeView)
    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, 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, }, }, ), Indexes: types.JsonArray[string]{ "CREATE UNIQUE INDEX idx_user ON example (user)", }, } // the id is autogenerated, but you can set a specific one if you want to: // collection.SetId("...") if err := app.Dao().SaveCollection(collection); err != nil { return err }
    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, 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 }
    import ( "github.com/pocketbase/pocketbase/models/schema" ) ... collection, err := app.Dao().FindCollectionByNameOrId("example") if err != nil { return err } // change the collection name collection.Name = "example_update" // add new field collection.Schema.AddField(&schema.SchemaField{ Name: "description", Type: schema.FieldTypeText, }) if err := app.Dao().SaveCollection(collection); err != nil { return err }
    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) // change the collection name form.Name = "example_update" // add new field 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 }
    collection, err := app.Dao().FindCollectionByNameOrId("example") if err != nil { return err } if err := app.Dao().DeleteCollection(collection); err != nil { return err }