Understanding PocketBase records and how to work with data
Records are individual data entries within a collection. Each record contains field values defined by its parent collection’s schema and provides methods for data access, manipulation, and validation.
Collection reference - The parent collection defining the schema
Field data - Values for each field defined in the collection
System fields - Auto-managed fields like id, created, updated
Expand data - Resolved relation field data
Custom data - Additional fields not defined in the collection schema
type Record struct { collection *Collection data map[string]any expand map[string]any originalData map[string]any // For tracking changes BaseModel // Includes Id, Created, Updated}
collection, _ := app.FindCollectionByNameOrId("posts")record := core.NewRecord(collection)// Set field valuesrecord.Set("title", "Hello World")record.Set("content", "This is my first post")record.Set("published", true)// Save to databaseif err := app.Save(record); err != nil { return err}
The record id is automatically generated when you save a new record. It’s a 15-character lowercase alphanumeric string by default.
// Basic setterrecord.Set("title", "Updated Title")// Bulk load from maprecord.Load(map[string]any{ "title": "New Title", "content": "New content", "published": true,})// Set raw value (bypasses field setters)record.SetRaw("title", "Direct Value")
file := &filesystem.File{ Name: "document.pdf", Reader: fileReader,}record.Set("attachments", file)// or for multiple filesrecord.Set("attachments", []*filesystem.File{file1, file2})// Save uploads the files automaticallyapp.Save(record)
Records are validated automatically during save operations:
// Validate without savingerr := app.Validate(record)if err != nil { // Handle validation errors if validationErr, ok := err.(validation.Errors); ok { for field, fieldErr := range validationErr { fmt.Printf("Field %s: %v\n", field, fieldErr) } }}// Save with validation (default)err := app.Save(record)// Save without validation (use with caution)err := app.SaveNoValidate(record)
// Hide specific fieldsrecord.Hide("email", "password")// Unhide fields (even if marked hidden in schema)record.Unhide("email")// Include custom/unknown fieldsrecord.WithCustomData(true)// Ignore email visibility for auth recordsrecord.IgnoreEmailVisibility(true)publicData := record.PublicExport()
// Check if record is new (not yet saved)if record.IsNew() { // Handle new record}// Get original data (as loaded from database)original := record.Original()// Get fresh copy (current state, no expand/custom data)fresh := record.Fresh()// Get full clone (including expand and custom data)clone := record.Clone()// Get collectioncollection := record.Collection()// Get table nametableName := record.TableName()// Get files pathfilesPath := record.BaseFilesPath()// Returns: {collectionId}/{recordId}