---
title: "Entity Flow with Attribute Sync"
description: "Keep one Flow per entity and let Attribute Store sync copy selected Attributes into a SQL table you can query."
canonical_url: "https://docs.superdurable.io/design-patterns/data-storage/entity-flow-with-attribute-sync/"
source: "docs/content/design-patterns/data-storage/entity-flow-attribute-sync.mdx"
locale: "en"
language: "python"
---

> Index of every page for AI agents: https://docs.superdurable.io/llms.txt

# Entity Flow with Attribute Sync

Code on this page is in Python. Other languages: [Go](https://docs.superdurable.io/design-patterns/data-storage/entity-flow-with-attribute-sync.go.md) · [Java](https://docs.superdurable.io/design-patterns/data-storage/entity-flow-with-attribute-sync.java.md) · [TypeScript](https://docs.superdurable.io/design-patterns/data-storage/entity-flow-with-attribute-sync.typescript.md) · [Rust](https://docs.superdurable.io/design-patterns/data-storage/entity-flow-with-attribute-sync.rust.md)

Keep one Flow per entity and let Attribute Store sync copy selected Attributes into a SQL table you own. Use it when an entity needs durable state that you also query with SQL, such as users that change through RPCs and are searched together.

<a id="when-to-use-this-pattern"></a>

## How it works

Use the entity ID as the Flow ID. It becomes the table's primary key. The example keeps one `UserProfileFlow` per user, keyed by `user_id` in `public.user_profiles`. The server configuration names the Attribute Store `entityStore`.

1. **Mark the synced Attributes.** Each Attribute to copy carries the sync flag.
2. **Select the store at start.** The start request names the store in `FlowConfig`.
3. **Change state through the Flow.** RPCs update or delete the Attributes. Dex then writes the latest values to the table in the background.

## Core implementation

### Define synced Attributes

Add each synced Attribute to the persistence schema. Dex saves each value in the Flow before it syncs the value.

**Python**

```python
class UserProfileFlow(Flow[None]):
    display_name = Attribute("display_name", str, sync_to_attribute_store=True)
    email = Attribute("email", str, sync_to_attribute_store=True)
    marketing_opt_in = Attribute("marketing_opt_in", bool, sync_to_attribute_store=True)
    credits = Attribute("credits", int, sync_to_attribute_store=True)
    weight = Attribute("weight", float, sync_to_attribute_store=True)
    last_logged_in_time = Attribute(
        "last_logged_in_time", datetime, sync_to_attribute_store=True
    )
    metadata = Attribute(
        "metadata", UserProfileMetadata, sync_to_attribute_store=True
    )

    def get_persistence_schema(self) -> PersistenceSchema:
        return PersistenceSchema.of(
            self.display_name,
            self.email,
            self.marketing_opt_in,
            self.credits,
            self.weight,
            self.last_logged_in_time,
            self.metadata,
        )
```

Example: [examples/python/dex_examples/patterns/entity-store/user_profile_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/entity-store/user_profile_flow.py)

### Update through a Flow RPC

The update RPC validates the profile, then writes every synced Attribute.

**Python**

```python
@rpc
def update_profile(self, context: Context, input: UserProfile) -> None:
    input.validate()
    self.display_name.set(context, input.display_name)
    self.email.set(context, input.email)
    self.marketing_opt_in.set(context, input.marketing_opt_in)
    self.credits.set(context, input.credits)
    self.weight.set(context, input.weight)
    self.last_logged_in_time.set(context, input.last_logged_in_time)
    self.metadata.set(context, input.metadata)
```

Example: [examples/python/dex_examples/patterns/entity-store/user_profile_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/entity-store/user_profile_flow.py)

### Select the storage target at Flow start

Select the server's Attribute Store in `FlowConfig` when you start the Flow. The initial Attribute values of that start request sync too.

**Python**

```python
config_override=FlowConfig(attribute_store_names=[STORE_NAME]),
```

Example: [examples/python/dex_examples/patterns/entity-store/controller.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/entity-store/controller.py)

## Rules and pitfalls

- **Decide from the Flow.** The Flow holds the authoritative state, and every write goes through it. Make Flow decisions from its Attributes, not from the table.
- **Treat the table as a latest-state copy.** Dex writes it asynchronously, and a row holds only the latest values. Use it for reads and queries.
- **Expect NULL after a delete.** Deleting an Attribute keeps the row and writes `NULL` to its column.

Column types, delivery timing, and retries are in the [Attribute Store sync reference](https://docs.superdurable.io/reference/server/attribute-store-sync.md).

## API endpoints

- **POST /patterns/entity-store/profile** — create a profile Flow
- **POST /patterns/entity-store/profile/update** — update its synced fields
- **GET /patterns/entity-store/profile?userId=...** — read the profile from the Flow
- **POST /patterns/entity-store/profile/clear?userId=...** — delete the synced values

## Run with PostgreSQL

The [shared example setup](https://github.com/superdurable/dex/tree/main/examples/entity-store) creates `public.user_profiles` and starts **dexcli dev** with the `entityStore` configuration. It has curl commands for every language.

## Next

- [Sequentially Chunked AttributeMap](https://docs.superdurable.io/design-patterns/data-storage/sequentially-chunked-attribute-map.md)
- [Attribute](https://docs.superdurable.io/concepts/primitives/attribute.md)
- [Sync Attribute data to an external store](https://docs.superdurable.io/concepts/primitives/attribute.md#attribute-store-projection): the setup steps for Attribute Store sync.
