Core Concepts - ljcom/operadb GitHub Wiki
๐ง Core Concepts โ OperaDB
OperaDB is structured around modular and replayable logic. This page explains the essential concepts that make up the foundation of how OperaDB operates internally.
๐งโโ๏ธ Actor
- An actor is the originator of an event โ either a user, a system, a bot, or an external service.
- All events are created in the context of an actor.
- Actors are registered in the system with a unique ID and assigned roles (e.g., admin, system, guest).
{
"actorId": "user:abc123",
"roles": ["admin"]
}
๐งพ Event
- An event is an immutable log of an action.
- It records what happened, when, by whom, and on which entity.
- Events can be stored permanently, and used to replay or audit state.
{
"type": "asset.create",
"actor": "user:abc123",
"data": {
"assetId": "asset:x001",
"name": "Gold",
"qty": 100
},
"timestamp": "2025-06-04T07:30:00Z"
}
๐งฎ Reducer
- A reducer is a function that receives events and produces the current state.
- Reducers are schema-specific and versioned.
- They define logic like incrementing quantities, merging fields, or validating constraints.
// Example (simplified)
function reducer(state, event) {
if (event.type === "asset.create") {
return { ...event.data };
}
if (event.type === "asset.update") {
return { ...state, ...event.data };
}
return state;
}
๐ Schema
- A schema defines the structure of an entity โ similar to a database schema but modular and versioned.
- Schemas include:
- Field names and types
- Required fields
- Validation rules
- Associated reducer
{
"schemaId": "mod:asset",
"fields": [
{ "name": "assetId", "type": "string", "required": true },
{ "name": "name", "type": "string", "required": true },
{ "name": "qty", "type": "number", "required": false }
],
"reducerCode": "// reducer logic"
}
๐ฆ State
- The state is the result of applying all related events to an entity.
- State is not stored permanently by default โ it is derived by replaying events through a reducer.
- Optionally, state can be cached or snapshotted for performance.
{
"assetId": "asset:x001",
"name": "Gold",
"qty": 120
}
๐ Chain & Slot
- Each entity (e.g. asset, coin, actor) is stored under an account namespace and a slot.
- Slots allow multiple versions or variants of the same entity to exist simultaneously.
- Chains are used to group events for replay and traceability.
๐งญ Guiding Flow
Actor โ Event โ Reducer โ State
Schema guides both the event format and the reducer behavior
๐ Example Lifecycle
- Actor creates an
asset.create
event. - Event is saved and validated.
- Reducer processes the event.
- State is generated or updated.
- State can be queried or used in the frontend.
Continue to Modularity & Schemas to learn how to define and organize schema modules in OperaDB.