A relation is an object with two typed endpoints, a source and a target. Use one when the link has data of its own, such
as a role or a date, or when you need to query it from either end. A plain Ref field is enough when one object just
points at another. Relation mirrors Obj for everything that is not about endpoints.
Declare the type with Type.makeRelation, which fixes the endpoint types
org.dxos.echo#F-2.2. Then pass the endpoints under the reserved Relation.Source and
Relation.Target keys.
1
import {
import Obj
Obj,
import Relation
Relation } from'@dxos/echo';
2
import {
classOrganization
Organization,
classPerson
Person } from'./schema.ts';
3
import {
classWorksAt
WorksAt } from'./works-at.ts';
4
5
const
const ada: BaseObj & Person & OfKind<EntityKind.Object>
ada =
const db: Database.Database
db.
Database.add<BaseObj & Person & OfKind<EntityKind.Object>>(obj: BaseObj & Person & OfKind<EntityKind.Object>, opts?: Database.AddOptions): BaseObj & Person & OfKind<EntityKind.Object>
Adds an object or relation to the database.
Only Object and Relation entities are accepted. To persist a Type definition use
addType
— passing a Type entity is rejected at compile time (and at runtime).
Pass { to: feed } to append to a feed instead (synchronous; confirm with
flush
).
@performance ― Synchronous; O(n) in object size to write it into a new linked document (or the root), persisted in
the background.
targetOf selects the relations that point at the current selection, and sourceOf the ones that start from it. Then
source() or target() steps to the objects at the other end. Query covers traversal.
@performance ― Async; executes the full query (index plus working set) on every call, not cached; O(n) in the data
the executor examines, not in the result count.
@performance ― Async; executes the full query (index plus working set) on every call, not cached; O(n) in the data
the executor examines, not in the result count.
Perform mutations on an echo relation within a controlled context.
All mutations within the callback are batched and trigger a single notification
when the callback completes. Direct mutations outside of Relation.update will throw
an error for echo relations.
@param ― relation - The echo relation to mutate. Use Obj.update for objects.
@param ― callback - The callback that performs mutations on the relation.
@example
const worksFor = Relation.make(EmployedBy, {
[Relation.Source]: person,
[Relation.Target]: company,
role: 'Engineer',
});
// Mutate within Relation.update
Relation.update(worksFor, (obj) => {
obj.role = 'Senior Engineer';
});
Note: Only accepts relations. Use Obj.update for objects.
@performance ― Synchronous; costs the mutations made in the callback plus one batched notification.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Sorts an array in place.
This method mutates the array and returns a reference to the same array.
@param ―
compareFn Function used to determine the order of the elements. It is expected to return
a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
value otherwise. If omitted, the elements are sorted in ascending, UTF-16 code unit order.