---
title: "The Model Package: Client, Server, and Now Data"
linkTitle: The Model Package
date: 2026-03-04
description: "Go Micro now has a typed data model layer — define structs, get CRUD and queries, swap backends. Every service gets Client, Server, and Model."
aliases:
  - /blog/6
---
Go Micro has always given you `service.Client()` to call other services and `service.Server()` to handle requests. But most services also need to save and query data. Until now, that meant either using the low-level `store` package (key-value only) or wiring up your own database layer.

Today we're shipping the `model` package — a typed data model layer that completes the service trifecta: **Client, Server, Model**.

## The Problem

The existing `store` package is great for simple key-value storage, but real services need more. You need to filter by fields, paginate results, count records, and use different databases in dev vs production. Most teams end up writing their own data layer or pulling in an ORM that has nothing to do with Go Micro.

We wanted something that feels native to the framework. Define a Go struct, tag a key, and get type-safe CRUD and queries — with the same pluggable backend pattern Go Micro uses everywhere.

## Define a Struct, Get a Database

```go
type User struct {
    ID    string `json:"id" model:"key"`
    Name  string `json:"name"`
    Email string `json:"email" model:"index"`
    Age   int    `json:"age"`
}
```

The `model:"key"` tag marks your primary key. The `model:"index"` tag creates an index for faster queries. Column names come from `json` tags (or lowercased field names if no tag).

Create a model and use it:

```go
users := model.New[User](service.Model())

// Create
users.Create(ctx, &User{ID: "1", Name: "Alice", Email: "alice@example.com", Age: 30})

// Read
user, err := users.Read(ctx, "1")

// Update
user.Name = "Alice Smith"
users.Update(ctx, user)

// Delete
users.Delete(ctx, "1")
```

No migrations. No connection setup. No configuration files. The schema is derived from your struct at startup.

## Queries That Feel Like Go

List and count with composable query options:

```go
// Simple equality filter
active, _ := users.List(ctx, model.Where("email", "alice@example.com"))

// Operators, ordering, pagination
page, _ := users.List(ctx,
    model.WhereOp("age", ">=", 18),
    model.OrderDesc("name"),
    model.Limit(10),
    model.Offset(20),
)

// Count records
total, _ := users.Count(ctx, model.Where("age", 30))
```

Filters support `=`, `!=`, `<`, `>`, `<=`, `>=`, and `LIKE`. Everything composes — add as many query options as you need.

## Three Backends, One Interface

The model layer follows Go Micro's pluggable pattern. Same code, different backends:

**Memory** — the default. Zero config, great for development and testing:

```go
service := micro.New("users")
users := model.New[User](service.Model()) // in-memory by default
```

**SQLite** — single-file database for local development or single-node production:

```go
db, _ := sqlite.New(model.WithDSN("file:app.db"))
service := micro.New("users", micro.Model(db))
```

**Postgres** — production-grade with connection pooling:

```go
db, _ := postgres.New(model.WithDSN("postgres://localhost/myapp"))
service := micro.New("users", micro.Model(db))
```

Start with memory in dev, switch to SQLite or Postgres for production. Your application code doesn't change.

## The Complete Service Interface

The Service interface now has three core accessors:

```go
type Service interface {
    Client() client.Client    // Call other services
    Server() server.Server    // Handle incoming requests
    Model()  model.Database   // Save and query data
    // ...
}
```

This means a typical service has everything it needs in one place:

```go
func main() {
    service := micro.New("users", micro.Address(":9001"))

    // Data layer
    users := model.New[User](service.Model())

    // Handler with data access
    service.Handle(&UserService{users: users})

    // Run
    service.Run()
}
```

Call services with `service.Client()`. Handle requests with `service.Server()`. Save data with `service.Model()`. That's the complete picture.

## Multiple Models, One Database

You can create multiple typed models from the same database connection:

```go
db := service.Model()

users    := model.New[User](db)
posts    := model.New[Post](db)
comments := model.New[Comment](db)
```

Each model gets its own table (derived from the struct name). They share the database connection.

## What's Next

The model package is production-ready with memory, SQLite, and Postgres backends. Coming soon:

- **Relationships** — define foreign keys between models
- **Migrations** — track and apply schema changes
- **Protobuf codegen** — `protoc-gen-micro` generates model code from proto definitions

See the [model documentation](https://go-micro.dev/docs/model.html) for the full API reference, or browse the [model package source](https://github.com/micro/go-micro/tree/master/model) to see the implementation.
