Data stores: model, routing and API
Section titled “Data stores: model, routing and API”This page is for integrators and platform engineers who create or read stores through the API, or need to know exactly how the platform decides where an entry is written and which spaces can read it.
Overview
Section titled “Overview”A data store is the physical partition that a data entity’s entries are written into. Every data entry carries a single store reference, and that reference decides which partition the entry lives in and, therefore, which spaces can read it. Stores are not a general-purpose key–value cache for builder code; they are the storage-routing layer that sits underneath a data entity’s entries and is driven by per-entity store rules. Conceptually, stores are dedicated units categorized by type to define the scope of sharing and permissions for an entity’s entries.
OSOS supports multi-store routing: an entry is written to a single store (the highest-priority store available to the current space) but reads aggregate across all stores available to that space.
Availability: Long-standing. The store-rules model and the Stores API predate the 1.2 milestone, so treat this as a foundational, always-present capability rather than a recent feature.
Store types
Section titled “Store types”There are three kinds of store, defined by DataStoreTypeEnum (backend/packages/osos-shared/src/models/enums.ts):
| Type | Numeric value | Scope | Persisted as a DataStore document? |
|---|---|---|---|
Public | 0 | One global store shared by every space | No — virtual/default |
Private | 1 | One store per space | No — virtual/default |
Shared | 2 | A store spanning two or more explicitly selected spaces | Yes |
- Public store — created automatically with the entity; holds data accessible from all spaces. Only its weight can be adjusted.
- Private store — created automatically with the entity; holds data accessible only within the owning space. Only its weight can be adjusted.
- Shared store — defined by an administrator when needed; enables sharing data between specific spaces chosen at creation. The administrator selects the participating spaces and sets a weight.
Only Shared stores are real documents in the DataStore collection and are the only type you create, read, or update through the Stores API. Public and Private are synthesised at runtime from fixed identifiers and never stored as DataStore rows.
Fixed identifiers
Section titled “Fixed identifiers”From backend/packages/osos-loopback/src/store/types.ts:
| Constant | Value |
|---|---|
PUBLIC_STORE_ID | "PUBLIC" |
PRIVATE_STORE_ID | "PRIVATE" |
PUBLIC_STORE | ReferenceProperty { dataEntity: "DataStore", dataEntry: "PUBLIC" } |
PRIVATE_STORE(spaceId) | ReferenceProperty { dataEntity: "DataStore", dataEntry: "PRIVATE_<spaceId>" } |
The public store has one fixed dataEntry (PUBLIC). Each space gets its own private store whose dataEntry is PRIVATE_<spaceId>.
Store rules
Section titled “Store rules”A data entity owns a list of store rules (dataEntity.storeRules: StoreRule[]). A store rule (store-rule.model.ts) has three fields:
| Field | Type | Notes |
|---|---|---|
type | DataStoreTypeEnum (number) | Public / Private / Shared |
storeId | string | null | Set only when type === Shared; the DataStore id |
priority | number (required) | Higher priority wins when routing a write |
When a data entity is created, it is seeded with two default rules (StoreService.defaultStoreRule):
| Rule | Type | storeId | priority |
|---|---|---|---|
| Public default | Public | PUBLIC_STORE_ID ("PUBLIC") | 100 |
| Private default | Private | PRIVATE_STORE_ID ("PRIVATE") | 50 |
These defaults are attached in the create-data-entity and create-virtual-data-entity command handlers. Adding a Shared store to an entity appends a third rule (with the store’s id and the requested weight).
In the API surface the store rule’s priority is exposed as weight. They are the same value; the DTOs use weight, the persisted model uses priority.
How a store rule routes a write
Section titled “How a store rule routes a write”StoreService.getStoreForWrite(storeRules, spaceId) decides where a new entry is written:
- Drop any Shared rule whose store does not include the current
spaceIdin itsspaceslist.PublicandPrivaterules are always kept. - From the remaining rules, pick the one with the highest
priority. - Resolve it to a
ReferenceProperty:Shared→{ dataEntity: "DataStore", dataEntry: <storeId> }Private→PRIVATE_STORE(spaceId)- anything else →
PUBLIC_STORE
Because a freshly created Shared store is given a weight/priority, its value relative to the Public (100) and Private (50) defaults determines whether new entries in the participating spaces land in the shared store or fall back to the public/private defaults.
How store rules gate a read
Section titled “How store rules gate a read”StoreService.getStoresForRead(spaceId) returns the set of stores visible from a space: the Public store, that space’s Private store, and every Shared store whose spaces include the space. The entry-read/validation paths (for example in data-entry-writer.service.ts) check that an entry’s store.dataEntry is in this set; an entry whose store is not visible from the current space is treated as not found in that space. This is the mechanism by which stores scope entry visibility across spaces.
Data model: DataStore
Section titled “Data model: DataStore”data-store.model.ts — persisted only for Shared stores. Extends Identified<UUID> (so id is a UUID).
| Field | Type | Required | Notes |
|---|---|---|---|
name | LocalizedValue | Yes | Localised display name |
type | DataStoreTypeEnum (number) | Yes | In practice always Shared (2) for persisted stores |
dataEntity | string | Yes | Id of the owning data entity |
spaces | ReferenceProperty[] | Yes | The spaces the store spans |
description | NullableLocalizedValue | No | |
creationInfo | ActionInfo | No | Who/when created (with optional delegatee) |
updateInfo | ActionInfo | No | Who/when last updated |
Persistence is a plain LoopBack DefaultCrudRepository (DataStoreRepository) over the Mongo datasource.
API reference
Section titled “API reference”All endpoints live in StoresController and require authentication via the OSOS Auth2 LoopBack strategy. They are all scoped to a data entity by entityId in the path. Before doing anything, each handler loads the entity and runs Helper.validateDataEntityExistsActive (the entity must exist and be active).
| Method | Path | Purpose |
|---|---|---|
POST | /v1/entities/{entityId}/stores | Create a Shared store for the entity |
GET | /v1/entities/{entityId}/stores | List all stores for the entity (defaults + shared) |
GET | /v1/entities/{entityId}/stores/{storeId} | Get one store rule / store |
PUT | /v1/entities/{entityId}/stores/{storeId} | Update a store |
Response shape: StoresResponseDto
Section titled “Response shape: StoresResponseDto”Every endpoint returns this shape (or an array of it):
| Field | Type | Notes |
|---|---|---|
storeId | string | Store id, or "PUBLIC" / "PRIVATE" for the defaults |
storeName | LocalizedValue | Only populated for Shared stores |
type | DataStoreTypeEnum (number) | Required |
description | NullableLocalizedValue | Optional; Shared only |
spaces | ReferenceProperty[] | Optional; Shared only |
weight | number | The store rule priority |
For Public/Private entries the response carries only storeId, type, and weight — storeName, description, and spaces are left unset because those stores have no DataStore document.
Create a store — POST /v1/entities/{entityId}/stores
Section titled “Create a store — POST /v1/entities/{entityId}/stores”Request body (CreateDataStoreRequestDto, which extends DataStore and adds weight):
| Field | Type | Required | Notes |
|---|---|---|---|
name | LocalizedValue | Yes | |
type | DataStoreTypeEnum (number) | Yes | Must be 2 (Shared) — see validation |
spaces | ReferenceProperty[] | Yes | At least two, no duplicates |
description | NullableLocalizedValue | No | |
weight | number | Yes | Store rule priority; must be unique within the entity |
dataEntity | string | — | Ignored in the body; the controller overwrites it with entityId from the path |
Server-side flow:
- Reject if any existing store rule on the entity already has
priority === weight→StoreWithSameWeightExistsException. storeDto.dataEntityis forced to the pathentityId.StoreService.createStorevalidates (see below) and persists theDataStore, stampingcreationInfo/updateInfowith the caller (anddelegateewhen acting on behalf of another user).- A new
StoreRule { storeId, priority: weight, type }is appended to the entity’sstoreRules. - An
UpdateDataEntityCommandis published on the service bus and a token is registered viaInvokerService.registerToken. The rule attachment to the entity is therefore asynchronous / eventually consistent — the HTTP response returns before the entity update is confirmed.
Response: StoresResponseDto for the created store (storeName, description, spaces, type, weight, storeId).
Example request:
{ "name": { "en": "Regional shared store" }, "type": 2, "spaces": [ { "dataEntity": "Space", "dataEntry": "space-a" }, { "dataEntity": "Space", "dataEntry": "space-b" } ], "description": { "en": "Entries visible to both regional spaces" }, "weight": 75}Create-time validation (validateCreateStore)
Section titled “Create-time validation (validateCreateStore)”| Condition | Exception | Error code |
|---|---|---|
| Fewer than 2 spaces | NotEnoughStoreSpacesSelectedException | NOT_ENOUGH_STORE_SPACES_SELECTED |
| Duplicate space ids | StoreSelectedSpacesDuplicationNotAllowedException | SELECTED_SPACES_SHOULD_NOT_HAVE_DUPLICATION |
type !== Shared | OperationNotSupportedForThisType | OPERATION_NOT_SUPPORTED_FOR_THIS_STORE_TYPE |
| Weight collides with an existing rule | StoreWithSameWeightExistsException | STORE_WITH_SAME_WEIGHT_ALREADY_EXIST |
A source comment confirms the intent: “At the moment of implementation only supports type: shared.” Creating Public or Private stores through the API is not supported.
List stores — GET /v1/entities/{entityId}/stores
Section titled “List stores — GET /v1/entities/{entityId}/stores”Optional query parameter filterCriteria (a FilterCriteria<DataStore>) is merged with a forced where.dataEntity = entityId before querying, so filters only ever match this entity’s Shared stores.
The response concatenates:
- Default rules — every store rule whose
type !== Shared(i.e. the Public and Private defaults), each returned as{ storeId, type, weight }. - Shared stores — fetched from the
DataStorecollection for this entity, each returned with full detail and itsweightlooked up from the matching store rule’spriority.
For Shared stores the service also resolves each space reference: it loads the matching DataEntryIndex rows and attaches the space’s index meta as a _resolved field on each space reference (used to render the space’s display metadata without a second call).
Get one store — GET /v1/entities/{entityId}/stores/{storeId}
Section titled “Get one store — GET /v1/entities/{entityId}/stores/{storeId}”Looks up the store rule on the entity by storeId.
- If no rule matches →
DataSourceStoreNotExist. - If the rule’s
type !== Shared(a Public/Private default) → returns{ type, weight, storeId }only, without hitting theDataStorecollection. - If the rule is Shared → loads the
DataStore(viagetStoreById, which throwsDataSourceStoreNotExistonENTITY_NOT_FOUND) and returns full detail plus the rule’sweight.
Update a store — PUT /v1/entities/{entityId}/stores/{storeId}
Section titled “Update a store — PUT /v1/entities/{entityId}/stores/{storeId}”Request body (UpdateDataStoreRequestDto) — all fields optional, treated as a partial update:
| Field | Type | Notes |
|---|---|---|
name | LocalizedValue | Shared stores only |
description | NullableLocalizedValue | Shared stores only |
weight | number | New store-rule priority |
spaces | ReferenceProperty[] | Shared stores only; must have at least 2 entries |
Server-side flow:
- The store rule must exist on the entity → else
DataSourceStoreNotExist. - If
weightis provided and collides with another rule’s priority →StoreWithSameWeightExistsException. - If the rule is Shared,
StoreService.updateStoreupdates theDataStoredocument. It rejects aspacesarray with fewer than 2 entries (NotEnoughStoreSpacesSelectedException), then merges each supplied field over the existing values (name,description,spaces) and refreshesupdateInfo.type,dataEntity, andcreationInfoare preserved. For Public/Private rules theDataStorecollection is not touched. - The store rule’s
priorityis set to the newweight(or left unchanged), the entity’sstoreRulesarray is rewritten, and — as with create — anUpdateDataEntityCommandis published and a token registered (asynchronous / eventually consistent).
Note:
updateStoreuses the repository’supdateById, which rebuilds the model before writing.DataStore’s constructor does not force-seed array fields, so the omitted-field merge is done explicitly inupdateStore. If you extend the model, keep this in mind (see the repository’s migration guidance on partial updates).
Collated store indexes for list reads Since 1.10
Section titled “Collated store indexes for list reads Since 1.10”Every entity list read starts by matching the entry’s store (then its state and virtual flag), and it runs with the entity collation {locale: 'en', strength: 2}. MongoDB only uses an index whose collation matches the operation’s, and none of the implicit indexes had one, so on a large entity that leading store match was a collection scan.
From 1.10 each non-virtual entity also carries:
- collated twins of the implicit
storeandsharingLevelindexes, and - a store-and-order index named
IX_<entity>_store_metaOrder_state_ci(keysstore.dataEntry,value._metaOrder.value,_id, then thestate/virtualfilter), so a list page sorts and skips on index keys instead of in memory.
New entities get them when they are created; existing entities receive them through data-service migrations 1.10.0-a38, a41 and a44. The indexes are built asynchronously by the indexing service, so a freshly upgraded tenant serves list reads from the new indexes only once its index queue has built them. There is nothing to configure, and store rules and the Stores API are unchanged. Virtual entities are skipped, because a MongoDB view cannot carry an index.
Error reference
Section titled “Error reference”| Error code | Meaning |
|---|---|
NOT_ENOUGH_STORE_SPACES_SELECTED | A store needs at least two spaces |
SELECTED_SPACES_SHOULD_NOT_HAVE_DUPLICATION | The selected spaces contain duplicates |
OPERATION_NOT_SUPPORTED_FOR_THIS_STORE_TYPE | Attempted a create with a non-Shared type |
STORE_WITH_SAME_WEIGHT_ALREADY_EXIST | Another store rule already uses this weight |
DATA_SOURCE_STORE_... (DataSourceStoreNotExist) | The store / store rule does not exist for this entity |
Building notes and constraints
Section titled “Building notes and constraints”- Weight/priority is per-entity unique. You cannot have two store rules on the same entity with the same weight; both create and update enforce this.
- Weight is the routing knob. New entries in a space are written to the highest-priority store rule that applies to that space. With the default weights the Public rule (
100) outranks the Private rule (50), so new entries go to the public store unless the weights are changed. Set a Shared store’s weight above100to make it win for the spaces it spans; a weight between50and100only outranks the private default, which the public default already does, so it has no effect unless the Public weight has been lowered below it. - Shared stores must span ≥ 2 spaces with no duplicates, on both create and (when
spacesis supplied) update. - Only Shared stores are manageable via the API. Public and Private stores exist implicitly on every entity and cannot be created or edited.
- Rule attachment is eventually consistent. Create/update return before the
UpdateDataEntityCommandis processed, so an immediately following read of the entity’s store rules may not yet reflect the change. - Cross-space visibility is enforced on read. An entry is only visible in a space whose read set (Public + that space’s Private + Shared stores covering it) contains the entry’s store.
Platform 1.10.0 · Backend 1.11.0-alpha.11 · Frontend 1.11.0-alpha.6 · Mobile 1.10.2 · Chart 1.11.8 · Gateway 2.6.11 · MCP 0.2.0 · Grafana DS 1.2.0Docs build 2026-11-27