Record structure
A record consists of:- 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
Creating records
You create new records through the collection:The record
id is automatically generated when you save a new record. It’s a 15-character lowercase alphanumeric string by default.Reading field values
PocketBase provides multiple methods for accessing record data:Basic getters
Raw getters
JSON unmarshaling
Setting field values
You can set field values using various methods:Field modifiers
Some fields support modifier operations:Working with files
File fields require special handling:Uploading files
Accessing uploaded files
File URLs
Files are stored at:Auth record helpers
Auth collections have additional convenience methods:Relations and expand
Relation fields reference records from other collections. You can expand (resolve) these relations:Expanding relations
Setting expand data manually
Record validation
Records are validated automatically during save operations:Record hooks
Intercept record operations with hooks:OnRecordValidate- Before validationOnRecordCreate/OnRecordUpdate/OnRecordDelete- Before operationOnRecordCreateExecute/OnRecordUpdateExecute/OnRecordDeleteExecute- During database writeOnRecordAfterCreateSuccess/OnRecordAfterUpdateSuccess/OnRecordAfterDeleteSuccess- After successOnRecordAfterCreateError/OnRecordAfterUpdateError/OnRecordAfterDeleteError- After error
Querying records
Updating records
Partial updates
To update only changed fields:Deleting records
Deleting a record also deletes associated files and handles cascade deletion for related records if configured.
Record serialization
Public export
Custom visibility
JSON marshaling
Record state management
Best practices
Validate user input
Validate user input
Always validate data before setting it on records. Use collection field constraints and custom validation logic to ensure data integrity.
Use type-specific getters
Use type-specific getters
Prefer
GetString(), GetInt(), etc., over Get() to avoid type assertion errors and benefit from automatic type conversion.Handle relations carefully
Handle relations carefully
Always check if expanded relations exist before accessing them. Missing or deleted related records can cause errors.
Optimize file uploads
Optimize file uploads
For multiple files, consider uploading them in batches. Large file uploads should be handled with proper error handling and retry logic.
Use hooks for side effects
Use hooks for side effects
Implement business logic in record hooks rather than scattering it throughout your codebase. This ensures consistency and maintainability.
Be cautious with SaveNoValidate
Be cautious with SaveNoValidate
Only use
SaveNoValidate() when you’re certain the data is valid, such as system-generated updates or data migrations.