Osy#betaRun it on Osyrin: osyrin.com

Reference / Entity

[BindsBy] — name a row by its key

[Unique, BindsBy] <Type> <Member>; · [BindsBy(<Member>, <Member>…)] entity <Name> { … }

`[BindsBy]` on a `[Unique]` member, or `[BindsBy(A, B)]` on the entity naming a declared `[Unique(A, B)]`, lets a caller name an existing row by that key as well as by its id, wherever a function parameter of that type is bound from REST, MCP or `osy run`: `{"item": "ITEM-12"}`, `{"release": {"product": "<id>", "version": "1.2"}}`.

stable1 example compiled by CIentitymodelfunctionsapi

Summary#

People and agents name records by their key — ITEM-12, a release's product and version — not by a UUID. [BindsBy] says which key that is. A function that takes the entity then accepts the key wherever it accepts the id, with no change to the function:

[Required, Unique, BindsBy, MaxLength(24)] string Key;     // on PlanItem
string SetPlanStatus(PlanItem item, PlanStatus status, string why) { … }
// POST /api/rest/v1/planning/plan/status   {"item": "ITEM-12", "status": "Done", "why": "shipped"}

Signature#

[Unique, BindsBy] string <Member>;          // a one-member key: a bare value names the row
[Unique(<A>, <B>)]
[BindsBy(<A>, <B>)]                         // a key of several members, named in an object
entity <Name> { … }

[BindsBy(<Member>)] on the entity with ONE member is the same as [BindsBy] on that member.

Description#

What a parameter of that type then accepts#

the caller sendsit binds
"8050d9b7-…" or {"id": "8050d9b7-…"}the row with that id — as before
"ITEM-12" (a value that is not a UUID)the row whose one-member key holds it
{"key": "ITEM-12"}the same, written out — the member's name, in camelCase or PascalCase
{"product": "<id>", "version": "1.2"}the row a composite key names; a reference member takes the referenced row's id
an object with any other field, without an ida NEW row from those fields

⚠ An object of exactly a key's members is a NAME, not a new row. For a Release keyed by product and version, {"product": "<id>", "version": "1.3"} binds the existing 1.3 (or is a 404) — it no longer constructs one. A function that creates such rows from the caller's values should take them as plain parameters (Release AddRelease(Product product, string version)), which is the clearer signature anyway.

Every door binds the same way: a REST endpoint's JSON body, an MCP tool's arguments, osy run --arg item=ITEM-12, and an agent's tool call. The function's own code sees the row and nothing else.

A value that reads as a UUID is always the id. If a key value could look like one, name it in the object form.

A parameter typed as the base of a keyed type#

A kit's Organization declares no key, but your Product : Organization does. A function that takes the Organization — a kit's function, or one of yours written against the base — then binds a bare value by the product's key:

entity Product : Organization { [Required, Unique, BindsBy, MaxLength(12)] string Key; … }
string NoteOn(Organization org, string note) { … }
// POST …/note   {"org": "ACME", "note": "…"}   — binds the Product whose Key is "ACME"
  • It applies to a bare value only, and only when the parameter's own type has no one-member key: its own key always wins. An id binds as before; an object of fields still constructs a new row of the parameter's type.
  • The key is the one declared by the one type derived from it (at any depth; a subtype of Product carries Product's key and is not a second one). If two derived types each declare one — Product.Key and Team.Code — a bare value is ambiguous and refused with a 400 naming both: Parameter 'org' takes a Organization, which has no key of its own, and a bare value could be a Product's key or a Team's code, so it names neither: give the row's id …. The binder never tries each in turn.
  • The row is looked up as a query of the derived type, as the caller, through its read rule — the same as Product.Where(p => p.Key == "ACME") in their own code. A product they may not read is the same 404 as a key no product holds.

When it is refused#

Each refusal is a 400 (an MCP tool error, an osy run failure) that names the parameter and what it takes:

  • a bare value for an entity with no one-member key — {"release": "1.2"}: Parameter 'release' takes a Release: an existing row's id … or its product and version ({"product": "<id>", "version": "…"}) …
  • an object of part of a key and nothing else — {"release": {"version": "1.2"}}
  • a key member given null, or a value of the wrong type for the member

A key that names no row is a 404: No PlanItem with that key (parameter 'item'): it does not exist, or this caller cannot read it. It names the key, never the value you sent.

Who can name which row#

The key is looked up as the CALLER, through their read rule, exactly as an id is. A row they may not read, a row whose key column is masked from them (deny read Key), and a key no row holds are one and the same 404, worded the same word for word — so a key cannot be used to learn which rows exist. A masked whole number reads as 0, so where a key member is masked on SOME rows for a caller, the value 0 names nothing for them by key: name that row by its id. A release of the same version in another product is never the one bound: the caller names the product. Once bound, the row is an ordinary argument and the function's own checks run on it.

Why a plain [Unique] does not bind on its own#

A key callers name rows by is a promise your API makes, so it is declared. A bare value would otherwise start meaning something the day a [Unique] is added, two of them would make it ambiguous, and a unique that is not an identifier — a token hash, an email — would become a way to look rows up that nobody chose.

What the compiler holds a key to#

  • It is a declared unique for every row: [Unique] on the member, or a [Unique(A, B)] over exactly the named members, without Where =.
  • Its members are string, int or long; a key of several members may also hold a reference to another entity. A one-member key may not be a reference or a Guid — its value would be indistinguishable from an id.
  • At most one key per entity has a single member, since a bare value is read through it. Several composite keys are fine.
  • Not on a class (no rows) nor on an entity whose rows live at a data source (they are found there by their [Key]).

What a schema says#

An MCP tool's input schema and the REST API's OpenAPI document describe such a parameter as "The PlanItem's id or its key", with the forms above, and its schema is an anyOf of exactly those shapes — a string, an object of a key's members, the entity's fields — so a client that validates its arguments sends them. An endpoint whose input row binds by a key publishes its named arguments ({"item": …, "status": …}) as its request body.

Examples#

entity Product {
  [Required, MaxLength(100)] string Name;
  security { allow read when IsAuthenticated; }
}

entity PlanItem {
  [Required] Product Product;
  [Required, Unique, BindsBy, MaxLength(24)] string Key;
  [MaxLength(40)] string? Status;
  security { allow read, update when IsAuthenticated; }
}

[Unique(Product, Version, "That product has a release of that version.")]
[BindsBy(Product, Version)]
entity Release {
  [Required] Product Product;
  [Required, MaxLength(40)] string Version;
  [MaxLength(40)] string? Stage;
  security { allow read, update when IsAuthenticated; }
}

/// Set an item's status.
/// <param name="item">The item — its id or its key.</param>
/// <param name="status">Its new status.</param>
public string SetStatus(PlanItem item, string status) {
  item.Status = status;
  UnitOfWork.Commit();
  return item.Key + " " + status;
}

/// Move a release to a stage.
/// <param name="release">The release — its id, or its product and version.</param>
/// <param name="stage">The stage.</param>
public string Promote(Release release, string stage) {
  release.Stage = stage;
  UnitOfWork.Commit();
  return release.Version + " " + stage;
}
osy run SetStatus --arg item=ITEM-12 --arg status=Done
osy run Promote --args-json '{"release": {"product": "8050d9b7-…", "version": "1.2"}, "stage": "Beta"}'

See also#

  • constraints — [Unique], the index a key stands on
  • [[api-rest#binding]] — what an endpoint binds its parameters from
  • [[local-running-a-function#entity-arguments]] — passing an entity to osy run
  • function — a function's parameters

Related

constraints

The per-member rules the database enforces — Required, Unique, MaxLength/MinLength, Min/Max, Pattern, and the…

function

A function is a top-level unit of work, written like a C# method — a return type, a name, typed parameters, a body. It…

publishing a REST API (app.Apis)

`app.Apis` publishes your app to a third party over HTTP. You never write a controller or an endpoint handler — you…

Running a function

Runs one of your app's own functions from the command line — to put the app in a known state, backfill a column, or…