Entity Flow with Attribute Sync
View as MarkdownKeep 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.
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.
- Mark the synced Attributes. Each Attribute to copy carries the sync flag.
- Select the store at start. The start request names the store in
FlowConfig. - 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.
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
var (
DisplayName = dex.DefineAttribute[string]("display_name", dex.SyncToAttributeStore())
Email = dex.DefineAttribute[string]("email", dex.SyncToAttributeStore())
MarketingOptIn = dex.DefineAttribute[bool]("marketing_opt_in", dex.SyncToAttributeStore())
Credits = dex.DefineAttribute[int64]("credits", dex.SyncToAttributeStore())
Weight = dex.DefineAttribute[float64]("weight", dex.SyncToAttributeStore())
LastLoggedIn = dex.DefineAttribute[time.Time]("last_logged_in_time", dex.SyncToAttributeStore())
Metadata = dex.DefineAttribute[UserProfileMetadata]("metadata", dex.SyncToAttributeStore())
)
func (*UserProfileFlow) GetPersistenceSchema() dex.PersistenceSchema {
return dex.PersistenceSchema{
Attributes: []dex.AttributeDef{
DisplayName, Email, MarketingOptIn, Credits, Weight, LastLoggedIn, Metadata,
},
}
}
public final Attribute<String> displayName =
Attribute.define("display_name", String.class).syncToAttributeStore();
public final Attribute<String> email =
Attribute.define("email", String.class).syncToAttributeStore();
public final Attribute<Boolean> marketingOptIn =
Attribute.define("marketing_opt_in", Boolean.class).syncToAttributeStore();
public final Attribute<Long> credits =
Attribute.define("credits", Long.class).syncToAttributeStore();
public final Attribute<Double> weight =
Attribute.define("weight", Double.class).syncToAttributeStore();
public final Attribute<Instant> lastLoggedInTime =
Attribute.define("last_logged_in_time", Instant.class).syncToAttributeStore();
public final Attribute<UserProfileMetadata> metadata =
Attribute.define("metadata", UserProfileMetadata.class).syncToAttributeStore();
@Override
public PersistenceSchema getPersistenceSchema() {
return PersistenceSchema.of(
displayName,
email,
marketingOptIn,
credits,
weight,
lastLoggedInTime,
metadata);
}
Example: examples/java/src/main/java/io/superdurable/dex/patterns/entitystore/UserProfileFlow.java
public readonly displayName = new Attribute("display_name", stringCodec)
.syncToAttributeStore();
public readonly email = new Attribute("email", stringCodec).syncToAttributeStore();
public readonly marketingOptIn = new Attribute("marketing_opt_in", booleanCodec)
.syncToAttributeStore();
public readonly credits = new Attribute("credits", int64Codec).syncToAttributeStore();
public readonly weight = new Attribute("weight", doubleCodec).syncToAttributeStore();
public readonly lastLoggedInTime = new Attribute("last_logged_in_time", dateCodec)
.syncToAttributeStore();
public readonly metadata = new Attribute("metadata", metadataCodec)
.syncToAttributeStore();
public getPersistenceSchema(): PersistenceSchema {
return {
attributes: [
this.displayName,
this.email,
this.marketingOptIn,
this.credits,
this.weight,
this.lastLoggedInTime,
this.metadata,
],
};
}
Example: examples/typescript/src/patterns/entity-store/user-profile-flow.ts
fn persistence(&self) -> PersistenceSchema {
PersistenceSchema::new()
.attribute(&DISPLAY_NAME)
.attribute(&EMAIL)
.attribute(&MARKETING_OPT_IN)
.attribute(&CREDITS)
.attribute(&WEIGHT)
.attribute(&LAST_LOGGED_IN_TIME)
.attribute(&METADATA)
}
static DISPLAY_NAME: LazyLock<Attribute<String>> =
LazyLock::new(|| Attribute::new("display_name").sync_to_attribute_store());
static METADATA: LazyLock<Attribute<UserProfileMetadata>> =
LazyLock::new(|| Attribute::new("metadata").sync_to_attribute_store());
Update through a Flow RPC
The update RPC validates the profile, then writes every synced Attribute.
@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
func (flow *UserProfileFlow) GetRPCs() []dex.RPCDef {
return []dex.RPCDef{
dex.DefineRPC(flow.UpdateProfile, nil),
dex.DefineRPC(flow.GetProfile, nil),
dex.DefineRPC(flow.ClearProfile, nil),
}
}
func (*UserProfileFlow) UpdateProfile(
ctx dex.Context,
profile UserProfile,
) (*dex.RPCResult[dex.None], error) {
if err := validateProfile(profile); err != nil {
return nil, err
}
if err := DisplayName.Set(ctx, profile.DisplayName); err != nil {
return nil, err
}
if err := Email.Set(ctx, profile.Email); err != nil {
return nil, err
}
if err := MarketingOptIn.Set(ctx, profile.MarketingOptIn); err != nil {
return nil, err
}
if err := Credits.Set(ctx, profile.Credits); err != nil {
return nil, err
}
if err := Weight.Set(ctx, profile.Weight); err != nil {
return nil, err
}
if err := LastLoggedIn.Set(ctx, profile.LastLoggedIn); err != nil {
return nil, err
}
if err := Metadata.Set(ctx, profile.Metadata); err != nil {
return nil, err
}
return &dex.RPCResult[dex.None]{}, nil
}
@RPC
public void updateProfile(final Context context, final UserProfile profile) {
profile.validate();
displayName.set(context, profile.displayName);
email.set(context, profile.email);
marketingOptIn.set(context, profile.marketingOptIn);
credits.set(context, profile.credits);
weight.set(context, profile.weight);
lastLoggedInTime.set(context, profile.lastLoggedInTime);
metadata.set(context, profile.metadata);
}
Example: examples/java/src/main/java/io/superdurable/dex/patterns/entitystore/UserProfileFlow.java
@rpc({ inputCodec: userProfileCodec })
public updateProfile(context: Context, profile: UserProfile): void {
this.displayName.set(context, profile.displayName);
this.email.set(context, profile.email);
this.marketingOptIn.set(context, profile.marketingOptIn);
this.credits.set(context, BigInt(profile.credits));
this.weight.set(context, profile.weight);
this.lastLoggedInTime.set(context, profile.lastLoggedInTime);
this.metadata.set(context, profile.metadata);
}
Example: examples/typescript/src/patterns/entity-store/user-profile-flow.ts
fn update(&self, context: &mut Context, replacement: UserProfile) -> HandlerResult<()> {
validate_profile(&replacement)?;
DISPLAY_NAME.set(context, replacement.display_name)?;
EMAIL.set(context, replacement.email)?;
MARKETING_OPT_IN.set(context, replacement.marketing_opt_in)?;
CREDITS.set(context, replacement.credits)?;
WEIGHT.set(context, replacement.weight)?;
LAST_LOGGED_IN_TIME.set(context, replacement.last_logged_in_time)?;
METADATA.set(context, replacement.metadata)
}
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.
config_override=FlowConfig(attribute_store_names=[STORE_NAME]),
Example: examples/python/dex_examples/patterns/entity-store/controller.py
options.ConfigOverride = &sdk.FlowConfig{
AttributeStoreNames: []string{StoreName},
}
.configOverride(FlowConfig.newBuilder()
.attributeStoreNames(UserProfileFlow.STORE_NAME)
.build())
Example: examples/java/src/main/java/io/superdurable/dex/patterns/entitystore/EntityStoreController.java
configOverride: { attributeStoreNames: [ENTITY_STORE_NAME] },
Example: examples/typescript/src/patterns/entity-store/controller.ts
FlowConfig::new().attribute_store_names(vec![STORE_NAME.to_owned()])
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
NULLto its column.
Column types, delivery timing, and retries are in the Attribute Store sync reference.
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 creates public.user_profiles and starts dexcli dev with the entityStore configuration. It has curl commands for every language.
Next
- Sequentially Chunked AttributeMap
- Attribute
- Sync Attribute data to an external store: the setup steps for Attribute Store sync.