Log In
Get
Docs
Showcase
Community

Backend Handlers using Database

DeploySolo uses PocketBase’s built-in SQLite database and ORM. You can interact with collections (tables) directly in your handler functions using the e.App.FindRecordById(), e.App.Save(), and query methods.

Accessing the Authenticated User

The authenticated user is available as e.Auth in handlers protected by middleware.LoginRedirect():

func handler(e *core.RequestEvent) error {
    user := e.Auth // current authenticated user record
    userID := user.Id
    userEmail := user.Email()
    return nil
}

Working with JSON Fields

Store structured data in JSON fields on the users collection or custom collections:

// Read JSON field
var tasks []Task
err := e.Auth.UnmarshalJSONField("tasks", &tasks)
if err != nil {
    return err
}

// Modify and save
tasks = append(tasks, newTask)
tasksJSON, _ := json.Marshal(tasks)
e.Auth.Set("tasks", tasksJSON)
err = e.App.Save(e.Auth)

This pattern is used in the todo.go example to store user tasks without creating a separate collection.

Querying Collections

To work with custom collections, use PocketBase’s query methods:

// Find by ID
record, err := e.App.FindRecordById("posts", postID)

// Query with filters
records, err := e.App.FindRecordsByFilter(
    "posts",
    "user = {:userId} && status = 'published'",
    "-created", // sort descending
    10,         // limit
    0,          // offset
    dbx.Params{"userId": e.Auth.Id},
)

// Create new record
collection, _ := e.App.FindCollectionByNameOrId("posts")
record := core.NewRecord(collection)
record.Set("title", "New Post")
record.Set("user", e.Auth.Id)
e.App.Save(record)

// Update existing
record.Set("title", "Updated Title")
e.App.Save(record)

// Delete
e.App.Delete(record)

Defining Collections

Use PocketBase’s admin UI at http://localhost:8090/_/ during development to create collections and fields.