A reference is a field that points at another entity. It stores the target’s DXN, not a copy, and resolves the target
only when you ask. org.dxos.echo#F-4.1References and DXNs
explains the model; this page is the API.
Ref.Ref(Type) is the schema of a field that points at that type. Use it inside Schema.optional or Schema.Array like
any other field schema. The tutorial’s Person and Event declare both:
Returns a Type.Type entity — a live, in-memory TypeSchema instance,
NOT a Schema.Schema. Use Type.InstanceType<typeof Foo> for the instance
type and Type.getSchema(Foo) to obtain the underlying Effect Schema.
The entity's id defaults to EntityId.deterministic(typename, version) so
constructing a type never reaches crypto.getRandomValues() — required for
Cloudflare workerd, which forbids RNG calls in global (module-evaluation)
scope. Pass { id } to override (e.g. with EntityId.random() from a
request handler).
Giving the type a nominal class name keeps its inferred type portable for
declaration emit: structural expansion (which can pull in unnameable union
members from referenced schemas) is replaced by a reference to the class.
The second call takes either a schema, or struct fields (wrapped in Schema.Struct) with an optional function
that pipes annotations onto the struct.
Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Declared fields may be inherited and are copied to own properties in the
output. The __proto__ field is accepted only when it is an own property.
Parsing does not guarantee that output keys retain their input order.
Annotation<IconAnnotation>.set: (value: Annotation.IconAnnotation) => <SextendsSchema.Top>(schema: S) => S
Set the annotation on an Effect schema.
Only accepts Schema.Top — annotations must be applied to the
source schema BEFORE wrapping it with Type.makeObject / Type.makeRelation.
In a pipe, place every Annotation.X.set(...) before the Type.make... step.
@performance ― O(value size) encode, once at schema-definition time.
set({
18
icon: string
Sprite icon name (e.g., 'ph--user--regular', 'ph--cube--regular', 'px--anthropic--regular').
ph--* is Phosphor; px--* and dx--* are brand glyphs, which are sprite-only and carry no
weight variants. All three are admitted because a type whose subject IS a brand — an Anthropic
session, a GitHub repo — has no honest Phosphor equivalent.
Returns a Type.Type entity — a live, in-memory TypeSchema instance,
NOT a Schema.Schema. Use Type.InstanceType<typeof Foo> for the instance
type and Type.getSchema(Foo) to obtain the underlying Effect Schema.
The entity's id defaults to EntityId.deterministic(typename, version) so
constructing a type never reaches crypto.getRandomValues() — required for
Cloudflare workerd, which forbids RNG calls in global (module-evaluation)
scope. Pass { id } to override (e.g. with EntityId.random() from a
request handler).
Giving the type a nominal class name keeps its inferred type portable for
declaration emit: structural expansion (which can pull in unnameable union
members from referenced schemas) is replaced by a reference to the class.
The second call takes either a schema, or struct fields (wrapped in Schema.Struct) with an optional function
that pipes annotations onto the struct.
Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Declared fields may be inherited and are copied to own properties in the
output. The __proto__ field is accepted only when it is an own property.
Parsing does not guarantee that output keys retain their input order.
Annotation<IconAnnotation>.set: (value: Annotation.IconAnnotation) => <SextendsSchema.Top>(schema: S) => S
Set the annotation on an Effect schema.
Only accepts Schema.Top — annotations must be applied to the
source schema BEFORE wrapping it with Type.makeObject / Type.makeRelation.
In a pipe, place every Annotation.X.set(...) before the Type.make... step.
@performance ― O(value size) encode, once at schema-definition time.
set({
icon: string
Sprite icon name (e.g., 'ph--user--regular', 'ph--cube--regular', 'px--anthropic--regular').
ph--* is Phosphor; px--* and dx--* are brand glyphs, which are sprite-only and carry no
weight variants. All three are admitted because a type whose subject IS a brand — an Anthropic
session, a GitHub repo — has no honest Phosphor equivalent.
Ref.make takes an object you already hold. db.makeRef takes a URI and returns a reference bound to the database, so it
can load its target. Ref.fromURI also takes a URI, but its reference has nothing to load from until a database binds
it.
constmake: <BaseObj & Person & OfKind<EntityKind.Object>>(object: BaseObj & Person & OfKind<EntityKind.Object>) =>Ref<BaseObj & Person & OfKind<EntityKind.Object>>
Create a reference to an entity.
@performance ― O(1); allocates a ref that inlines the target, with no lookup.
make(
const ada: BaseObj & Person & OfKind<EntityKind.Object>
ada)] }),
8
);
9
10
// From a URI: bound to the database, so it can load.
11
const
const again: Ref<Person>
again =
const db: Database.Database
db.
Database.makeRef<Person>(uri: URI): Ref<Person>
Creates a reference to an existing object in the database.
NOTE: The reference may be dangling if the object is not present in the database.
NOTE: Difference from Ref.fromURIRef.fromURI(dxn) returns an unhydrated reference. The .load and .target APIs will not work.
db.makeRef(dxn) is preferable in cases with access to the database.
@performance ― O(1); allocates a resolver-bound ref without looking the target up.
peek returns the target if it is already in memory and never starts a load. load waits for the target, reading it
from disk or the network, and throws if it cannot be found; tryLoad returns undefined instead. Ref.loadAll loads an
array of references at once.
Avoid ref.target. It is deprecated because reading it starts a load as a side effect and can throw.
@returns ― The target when it is already materialized: the pinned target, or a side-effect-free
working-set lookup. Never throws and never triggers loading — the synchronous counterpart of
tryLoad . A just-added object can resolve here before it has settled into its own
document; callers that need a settled document must load instead.
@performance ― O(1) working-set lookup; never loads and never throws.
@returns ― Promise that will resolves with the target object.
Will load the object from disk if it is not present in the working set.
Short-circuits immediately when the target is already loaded.
@throws ― If the object is not available locally.
@idiom ― org.dxos.echo.refLoad
applies: Resolving a ref inside an async function or Effect handler — guarantees the object is available before proceeding
instead-of: ref.target — not guaranteed to be defined in async contexts; use await ref.load() (or yield* Database.load(ref) in Effect) to ensure the target is present
uses: load related: org.dxos.echo-react.useObjectReactive
@performance ― Async; resolves immediately for an inlined or loaded target, otherwise loads from disk or the network.
load();
7
// undefined instead of throwing when the target is missing
Whether two refs, or two ref arrays element by element, point at the same target: the same URI and the same
inlined target. Every read of a ref property mints a new wrapper, so identity never matches.
@performance ― O(n) in the array length; no allocation.
equals(
const ada: BaseObj & Person & OfKind<EntityKind.Object>
Determines whether the specified callback function returns true for any element of an array.
@param ― predicate A function that accepts up to three arguments. The some method calls
the predicate function for each element in the array until the predicate returns a value
which is coercible to the Boolean value true, or until the end of the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function.
If thisArg is omitted, undefined is used as the this value.