Getting Started
CLI
Request handling
- Routing
- Action Controller
- Resources
- Context
- Request Binding
- Middleware
- Error Handling
- Sessions
- Cookies
Frontend
Database
- Getting started with Pop
- Soda CLI
- Database Configuration
- Buffalo Integration
- Models
- Generators
- Migrations
- Fizz
- Mutations
- Querying
- Raw Queries
- Callbacks
- Scoping
- Associations and Relationships
- One to one associations
- One to many associations
Guides
- API Applications
- File Uploads
- Background Job Workers
- Mailers
- Tasks
- Local Authentication
- Third Party Authentication
- Events
- Go Modules
- Localization
- Logging
- Template Engines
- Testing
- Videos
Deploy
Mutations
Database
Mutations
In this chapter, you’ll learn how to create, update and delete objects from you database using Pop.
Create
Without validation
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{}
// Create a fruit without running validations
err := c.Create(fruit)
With validation
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{}
// Run validations and create if all validations passed
vErrors, err := c.ValidateAndCreate(fruit)
Update
Without validation
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{}
// Update a fruit without running validations
err := c.Update(fruit)
With validation
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{}
// Run validations and update if all validations passed
vErrors, err := c.ValidateAndUpdate(fruit)
Save
Save checks for the ID in you model: if the ID is the zero value of the type (so for example if it’s an int
and its value is 0
), Save
calls Create
.
Otherwise, it calls Update
.
Without validation
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{ID: 0}
// Create a fruit without running validations
err := c.Save(fruit)
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{ID: 1}
// Update a fruit without running validations
err := c.Save(fruit)
With validation
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{ID: 0}
// Run validations and create if all validations passed
vErrors, err := c.ValidateAndSave(fruit)
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{ID: 1}
// Run validations and update if all validations passed
vErrors, err := c.ValidateAndSave(fruit)
Delete
c, err := pop.Connect("development")
// [...]
fruit := &models.Fruit{ID: 1}
// Destroy the fruit
err := c.Destroy(fruit)
Next Steps
- Querying - Fetch the data you inserted in the database.
- Associations and Relationships - Handle relations between models.