Skip to content

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.

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>.

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.

SlaMinutes sla = store.get(context.client(), TicketSettings.SLA); // the client's, or the default
int 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.

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.

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.

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.

  • Integers: Go reads a JSON number with a zero fraction (12.0) as an integer, as Jackson does, for int, int32 and int64; 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 null is the default in Go’s Read and Get; Java’s ClientSettings.get returns null for it.
  • The text Go sends to PostgreSQL has sorted keys; the conformance vector compares jsonb, which does not keep key order or number spelling.

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.