Per-client settings
A value that differs per client, that someone changes while the application runs and that the
application reads when it works for that client: a ticket service’s SLA minutes, an invoice prefix, a
retention period. The framework keeps these in one table, fw_client_settings, and lets you define
each setting in code; reading and writing are the write path you
already use.
This is not configuration. Configuration holds the settings of a service or a connection and varies per deployment (Configuration); a per-client setting varies per client within one deployment and lives in the database. The normative rules are in the client settings specification.
The table, the migration and the JSON are shared by Java and Go: module id client-settings, table
fw_client_settings (client_id, version, settings JSONB), one row per client. A Go service can
take over a Java service’s database.
Define a setting
Section titled “Define a setting”A setting is a constant: a key, the type of its value (anything the language’s JSON library reads and
writes), the default a client gets until it sets one, and an optional rule. The key matches
[a-z][a-z0-9._-]{0,99} and is yours to namespace (tickets.sla). The rule returns a message that is
safe to show a person, or nothing when the value is fine; the default must satisfy it. Two definitions
with the same key would share a value, so keep each key in one place.
From the tickets example (TicketSettings); the value is any type Jackson reads and writes:
public record SlaMinutes(int low, int normal, int high, int urgent) { ... }
public static final ClientSetting<SlaMinutes> SLA = ClientSetting.of("tickets.sla", SlaMinutes.class, DEFAULT_SLA).rule(m -> problem(m).map(Problem::message));The rule returns Optional<String>.
var SLA = clientsettings.MustSetting("tickets.sla", Sla{2880, 1440, 480, 60})NewSetting(key, default) (or MustSetting for a package-level value) takes the key and a default;
the type is anything encoding/json/v2 reads and writes (a struct, an enum type with its own
UnmarshalJSON, a string, a number, a slice). WithRule(func(T) string) adds a rule: "" is
acceptable, anything else is a message. It runs on every With, can be called alone
(Setting.Check, in a use case’s Validate), and a default that breaks its own rule is refused.
Wire it
Section titled “Wire it”List the module after the database module. It adds the migration and provides the repository and store.
Add the framework-client-settings artifact (it is in the BOM), then:
.module(new DatabaseModule()).module(new OutboxModule()).module(new UseCaseModule()).module(new ClientSettingsModule())ClientSettingsModule provides ClientSettingsRepository and ClientSettingsStore.
clientsettings.Declare(app) // after the database module, before StartDeclare adds the migration set (module id client-settings) to the default database instance. Make
the store with clientsettings.NewStore(db).
SlaMinutes sla = store.get(context.client(), TicketSettings.SLA); // the client's, or the defaultint minutes = settings.forClient(database, client).get(TicketSettings.SLA).minutesFor(priority);A stored value that no longer decodes as the setting’s type is an IllegalStateException naming the
key and the client.
store := clientsettings.NewStore(db)sla, err := clientsettings.Read(ctx, store, client, SLA) // the default when there is no row or no valueA stored value that does not decode as the type is an error wrapping clientsettings.ErrDecode that
names the key and the client. A stored JSON null reads as the default.
A read goes to the database on every call: there is no cache, so cache a hot read yourself. A client with no row, or no value for the key, gets the default. A decoding failure is never replaced by the default silently. Extra fields in a stored JSON object are ignored, so a value type can gain fields.
Write: an ordinary use case
Section titled “Write: an ordinary use case”The framework supplies the aggregate and its repository, not a use case or an endpoint: who may change which setting, what event it raises and what the form looks like are yours. The repository returns the client’s row, or an empty aggregate at version 0 that the plan inserts.
From TicketUseCases:
.validate((context, c) -> TicketSettings.problem(minutes(c)).map(p -> UseCaseError.validation(p.code(), p.message()))).authorize(requires(Permissions.SLA_MANAGE)).execute((context, c) -> { ClientSettings saved = settings.forClient(database, context.client()).with(TicketSettings.SLA, minutes(c)); return Plan.save(saved, settings, new SlaPolicyChanged(events.metadata(context, TicketEvents.POLICY_CHANGED, saved.id()), ...));})with(setting, value) runs the rule and throws IllegalArgumentException if it fails; check first
(setting.check(value), or your own validate) so that a person gets a VALIDATION error.
without(setting) removes the client’s value and the default applies again.
cur, err := repo.ForClient(ctx, db, cx.Client()) // the stored row, or an empty version-0 aggregatechanged, err := clientsettings.With(cur.Settings(), SLA, newSLA) // runs the rule; or clientsettings.Without(...)plan := usecase.NewPlan(event, repo.Step(cx, cur, changed)) // insert at version 1, else version-checked updateWith returns an error client setting <key>: <message> when the rule refuses. Without removes the
client’s value. The functions are generic (Read, Get, With, Without, IsSet) because Go
methods cannot have type parameters; Current carries the version that Java keeps on the aggregate.
Repository is a usecase.Repository[Settings, string] (id = the client’s code; a find of another
client’s id is a miss). A Settings of another client than the Context’s is refused by the runner as
CROSS_CLIENT_WRITE.
The settings, the event and the audit row commit in one transaction; a failed run writes nothing.
One row per client. All of a client’s settings are one aggregate with one version. Two people
changing two settings of the same client at the same moment conflict and the loser gets
STALE_AGGREGATE (give the use case a retry policy if that matters); in exchange a use case can change
several settings atomically. Two first writes for a client at once: the second is UNIQUE_VIOLATION
and the caller tries again.
Language differences
Section titled “Language differences”- Integers: Go reads a JSON number with a zero fraction (
12.0) as an integer, as Jackson does, forint,int32andint64; a real fraction is an error (Jackson truncates it) and a JSON string"12"for a number is an error (Jackson coerces it). Unknown members of a stored record are ignored and missing ones are zero, as in Java. - A stored JSON
nullis the default in Go’sReadandGet; Java’sClientSettings.getreturnsnullfor it. - The text Go sends to PostgreSQL has sorted keys; the conformance vector compares
jsonb, which does not keep key order or number spelling.
What it does not do
Section titled “What it does not do”No cache, no framework endpoint, admin page or per-setting permission (the application writes those), no change history beyond the audit rows of your use cases, no list of every setting (declare the list where a page needs one), no encrypted values (use per-client secrets), and not the platform’s per-client configuration (see Platform config). The specification marks “one row per client, no cache, an undecodable value is an error” as provisional rulings.