Model
This page is giving details about the configuration of a Model.
- Basic example
- Typescript
- JSON Schema
{
name: 'model',
correlation_id: 'model_id',
schema: {
model: {
type: 'object',
properties: {
email: {
type: 'string',
description: 'Email address',
example: 'john@doe.org',
format: 'email',
}
}
}
}
}
{
name: string;
correlation_field: string;
schema: ModelSchema;
// ---
db?: string;
is_enabled?: boolean;
description?: string;
retry_duration?: number;
indexes?: ModelIndex[];
encrypted_fields?: string[];
links?: {
[key: string]: string;
};
// Model options:
with_default_events?: boolean;
with_global_version?: boolean;
with_blockchain_hash?: boolean;
current_hash_field?: string;
previous_hash_field?: string;
nonce_field?: string;
blockchain_hash_difficulty?: number;
blockchain_hash_genesis?: string;
}
{
"type": "object",
"required": ["name", "correlation_field", "schema"],
"properties": {
"is_enabled": {
"type": "boolean",
"description": "Boolean indicating if the model is active or not",
"example": true
},
"db": {
"type": "string",
"description": "Database connection to use for this model",
"example": "Hello World"
},
"name": {
"type": "string",
"description": "Model name",
"example": "Hello World"
},
"description": {
"type": "string",
"description": "Model description",
"example": "Hello World"
},
"correlation_field": {
"type": "string",
"description": "Model correlation field",
"example": "Hello World"
},
"encrypted_fields": {
"type": "array",
"items": {
"type": "string",
"description": "Field path in the model of the data to be encrypted"
}
},
"retry_duration": {
"type": "number",
"description": "Max events handler retry duration",
"example": 0.2
},
"indexes": {
"type": "array",
"items": {
"type": "object",
"properties": {
"collection": {
"type": "string",
"description": "Collection name for the index"
},
"keys": {
"type": "object",
"description": "List of keys for the index"
},
"opts": {
"type": "object",
"description": "List of options to apply during the index creation"
}
}
}
},
"schema": {
"type": "object",
"description": "Model schema",
"properties": {
"model": {
"type": "object"
},
"events": {
"type": "object"
}
}
},
"links": {
"type": "object",
"patternProperties": {
"^[0-9a-z_]+$": {
"type": "string"
}
},
"examples": [
{
"correlation_id": "model_name"
},
{
"entity_id": "entity_type"
},
{
"child_id": "children",
"family_id": "families"
}
]
},
"with_global_version": {
"type": "boolean",
"description": "Use a global version index instead of a entity local one"
},
"with_blockchain_hash": {
"type": "boolean",
"description": "Use a blockchain logic hash generation logic"
},
"current_hash_field": {
"type": "string",
"description": "Field to use for current event blockchain hash (blockchain mode activation required)",
"example": "hash"
},
"previous_hash_field": {
"type": "string",
"description": "Field to use for previous event blockchain hash (blockchain mode activation required)",
"example": "prev"
},
"nonce_field": {
"type": "string",
"description": "Field to use for blockchain nonce value (blockchain mode activation required)",
"example": "nonce"
},
"blockchain_hash_difficulty": {
"type": "integer",
"description": "Number of `0` to enforce in the generated hash",
"example": 1
},
"blockchain_hash_genesis": {
"type": "string",
"description": "First hash to use for the initialization of the blockchain",
"example": "0000000000000000000000000000000000000000000000000000000000000000"
}
}
}
Required
is_enabled: boolean;
Boolean indicating the current state of this model: active (true) or inactive (false).
name: string
Name of the model. Prefer a plural form like: users, badges, invoices.
correlation_field: string
Field that will be used to identify an entity in this model. Entity state, events and snapshots will all be correlated with this Field.
For example, if correlation_field = user_id, a User will have the property user_id with a unique ID as well as every event associated with a unique version number.
schema: ModelSchema
Here is the full schema defining your model with the model described as a JSON Schema and every single event associated to it.
Options
description?: string
Description of the model. Helpful to add more context in the Open API documentation or in Metabase after a Data Model synchronization.
retry_duration?: number
milliseconds
Duration in milliseconds to accept an event reduction concurrently to another event happening on the same history: entity-based or globally.
indexes?: ModelIndex[]
Format:
{
collection: string;
fields: IndexSpecification;
opts: CreateIndexesOptions;
}
where collection is the name of the collection to apply the index on. For a model with a name users, the state collection is users, the events collection is users_events and the snapshots collection is users_snapshots.
Based on your business logic, you must define indexes on either one or several of these 3 collections.
By default, some indexes are automatically created to ensure correlation Id unicity or events unicity and order.
encrypted_fields?: string[]
List of fields that must be considered as sensitive and encrypted in database. Every field in this field will be automatically encrypted on any write operation. A specific decrypt action must be executed on the data to access the clear data back. A log is registered to keep track of the different accesses.
Deep notation is supported like my.sensitive.data for object:
{
my: {
sensitive: {
data: {
hash: 'xxx',
encrypted: 'xxx'
}
}
}
}
Advanced
with_default_events?: boolean
If false, default events attached to a model are removed. Those events are basically CREATED, UPDATED, PATCHED, ARCHIVED, DELETED, RESTORED and ROLLBACKED.
This is useful if you plan to apply a strict application of the Domain Driven approach excluding any non specific event.
with_global_version?: boolean
If true, the event history versioning is applied on the full collection instead of the entity.
with_blockchain_hash?: boolean
If true, blockchain hashes are generated and attached to every event stored in the Datastore. Fields used are defined below.
current_hash_field?: string
default:
hash
Field used to store the current event hash.
previous_hash_field?: string
default:
prev
Field used to store the previous event hash.
nonce_field?: string
default:
nonce
Field used to store the nonce value.
blockchain_hash_difficulty?: number
default:
0
Difficulty used to generate the hash. 1 means that the found hash must start with a 0. 3 means that the hash must start with 000.
Higher value means harder to find a hash.
blockchain_hash_genesis?: string
default:
0000000000000000000000000000000000000000000000000000000000000000
Genesis hash used to start the blockchain.