08 Creating A Datamodel

Creating A Datamodel

This page is the practical side of the four datamodel classes: which class to choose for a new model, where its repository goes, what you get from it, and what a breaking change means in each class. Why the classes exist and how strict each one is are explained in the governing principles, in Datamodel classes.

Before You Create A Model

Search Build → Datamodels in klok.dev first. If a model already fits, use it. If it almost fits, check whether the change you need is compatible (see Schema Evolution And Versioning). A second model for the same business object makes every later integration choose between them.

Choose The Class

Ask these questions in order and stop at the first yes:

  1. Does the model mirror the format of one external system, such as a vendor's API, a file layout or a database table? Choose class X, system.
  2. Will other tenants or parties outside your organization read it? Choose class A, public.
  3. Will several of your tenant's domains read it? Choose class B, common.
  4. Otherwise, it belongs to one business domain. Choose class C, domain.

All four classes may be sent over the broker. The class does not decide whether a model may travel on a topic; it decides how widely the model is shared and how carefully it must be changed. Choose the narrowest class that fits: a narrower class is easier to change.

A class X model on the broker ties every reader to that external system. When the data is to reach other systems or domains, transform it to a class C or B model in the ingress, and back to the receiving system's format in the egress (see Transform At The Edge).

Moving a model to another class later changes its repository path and its Java package, so its readers have to move as they would for a breaking change.

Where To Create The Repository

Class Repository Example
A, public <tenant>/datamodel/public/<model>-v<n> klok/datamodel/public/klok-mail-v1
B, common <tenant>/datamodel/common/<model>-v<n> klok/datamodel/common/klok-log-event-v5
C, domain <tenant>/datamodel/domain/<domain>/<model>-v<n> klok/datamodel/domain/hr/personnel-scheduling-v1
X, system <tenant>/datamodel/system/<system>/<model>-v<n>, or plain Java classes in the integration's own source klok/datamodel/system/fortnox/fortnox-supplier-invoice-v1

Create the repository with New model on Build → Datamodels, or on a domain's page, which fills in class C and the domain. You give the class, the domain or system, the name in PascalCase, the major version and one line of description; before anything is created, the form shows the repository, the root element and the namespace that the platform's naming rules give the model. klok.dev then creates the repository from klok/environment/project-template-datamodel, with its documentation project beside it, and opens the model on Edit. Write the XSD there as a draft and create a merge request from it; merging it releases version 1. Until the model's consumers can use it, its page lists what is still missing under Before it runs.

You can also create the repository by hand in GitLab from klok/environment/project-template-datamodel. The repository name is the model name in kebab case with its major version (supplier-invoice-v1), and the XSD in schema-definition-xsd/ has the same name. klok.dev's library lists the repository within five minutes, or at once with Sync now: it reads GitLab and never stores a copy you have to keep in step.

Changing an existing model in klok.dev's editor opens a merge request in the model's repository; nothing is written to main until the merge request is merged. The walkthrough, how the library reads GitLab and who may save an edit are on klok.dev's Datamodels page.

A class X model does not need an XSD. If only one integration uses it, write it as simple Java classes in that integration and explain them in the source code. Give it a repository with an XSD when more than one integration needs it, or when a service in another language has to read it.

What You Get

A model with its own repository and an XSD (classes A, B and C, and X when it has one) gets this on every pipeline on main:

What Where
Java classes, dev.klok.<tenant>.datamodel:<model>-v<n> the project's package registry (Maven Central for a public model), and the version in the dependency catalog: the platform catalog for a klok model, the tenant's own catalog <tenant>/framework/<tenant>-gradle-dependency-catalog for every other tenant
Python classes, <tenant>-datamodel-<model>-v<n> the project's PyPI registry
TypeScript classes, @<tenant>/datamodel-<model>-v<n> the project's npm registry (a public model's package is staged on npmjs.org)
Documentation pages docs.klok.dev, in the documentation project <tenant>/documentation/datamodel/<same path>

The three languages are generated to be compatible on the wire, so a message written by a Java route can be read by a Python or TypeScript service on the same topic, and the other way round.

On docs.klok.dev the model appears under its Git path, for example klok / datamodel / system / konvoj or klok / datamodel / public. Its documentation is generated from the XSD: a page with the model's structure, then sections written by Kloker, from each complex type up to an overview. Write xs:documentation on the types and fields; that is what the generated text is built from. Your own pages in the repository's klok-docs/ are published next to them.

In a Java project, use the model through the dependency catalog: run ./gradlew updateKlokLibsToml and reference the catalog alias. A klok model is klokLibs.<alias> (the platform catalog); your tenant's own model is tenantLibs.<alias> (the tenant catalog, refreshed into gradle/tenant-libs.versions.toml by the same task). Never type a version by hand.

If the model's objects will be stored in the Operative Data Layer, design for it from the start: a stable id, and the fields you list, filter and sort on as simple fields at the first level. Using Datamodels In The ODL walks through the design, the build and the use from routes.

A class X model written as plain Java in an integration gets none of this. It can be published on the broker from Java, and it is documented as part of the integration that holds it.

Breaking Changes Per Class

The rules are the same for every class:

  • The major version is part of the name: InvoiceV1, InvoiceV2.
  • A compatible change, such as a new optional field, stays in the current major version.
  • A breaking change, such as removing, renaming or retyping a field, making an optional field required or adding or removing an enum value, is a new major version: a new repository, a new class and a new topic. The old and the new version run in parallel while readers move.
  • The pipeline decides which is which. Every merge request is compared with every earlier release of the model, in both directions, and klok_datamodel_compatibility fails a change that would break a service built with one of them. See Schema Evolution And Versioning.

What differs is who agrees to the change and how long the old version keeps running:

Class Who agrees How long the old version runs, at least
A, public the model's owner, after telling every reader, including parties outside the tenant 12 months after the notice to every reader, with the replacement and a date
B, common the domains that read the model, together 3 months after the deprecation commit, and until the domains that read it have moved
C, domain the domain that owns it no fixed minimum: until its readers have moved
X, system the external system: when it changes its format, the model follows no fixed minimum: until the system stops using the old format

The minimums are a floor: the old version is retired only when the minimum has passed and every known reader has moved. The reasoning is in the governing principles (datamodel classes). Retiring the old version follows the datamodel lifecycle.

Checklist

  • [ ] No existing model fits, or the change to one is breaking.
  • [ ] The class is chosen and is the narrowest that fits.
  • [ ] The repository path matches the class and ends in the major version.
  • [ ] The XSD documents its types and fields (not required for a class X model in an integration's source).
  • [ ] Shared data that starts in a system format is transformed to class B or C at the edge.
  • [ ] If the objects are stored in the ODL, they have an id and simple first-level fields to filter on.
  • [ ] Readers of a breaking change are known, and the old version runs in parallel until they have moved.