Each space has one database. It holds the space’s objects, runs queries over them and saves writes to disk. Effect code
reaches the same database as Database.Service, which the Effect guide covers.
add is synchronous. It returns the object, now stored in the database, and saves it in the background. remove marks the
object deleted; queries leave it out from then on.
1
import {
import Obj
Obj } from'@dxos/echo';
2
import {
classPerson
Person } from'./schema.ts';
3
4
const
const ada: BaseObj & Person & OfKind<EntityKind.Object>
ada =
const db: EchoDatabase
db.
Database.add<BaseObj & Person & OfKind<EntityKind.Object>>(obj: BaseObj & Person & OfKind<EntityKind.Object>, opts?: 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.
db.query takes a filter or a query and returns a QueryResult. Call run() for the results once, or subscribe() to
follow them. org.dxos.echo#F-5.1Query covers building queries.
@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.
flush resolves once pending writes are on disk. Options also wait for the indexes and for query updates, which is what a
test needs before it reads back what it wrote.
Wait for all pending changes to be saved to disk.
Optionaly waits for changes to be propagated to indexes and event handlers.
@performance ― Async; always waits for pending document creation, then only for the disk, index and update work
that opts selects, so it is as slow as that selected backlog.
Wait for all pending changes to be saved to disk.
Optionaly waits for changes to be propagated to indexes and event handlers.
@performance ― Async; always waits for pending document creation, then only for the disk, index and update work
that opts selects, so it is as slow as that selected backlog.
db.add rejects type entities. Persist a type with addType, which replicates it to the space’s other members. If the
space already holds a type with the same typename and version, addType returns that one and writes nothing.
Construct a new object-kind type entity from raw metadata — for cases where
an Effect Schema isn't available (e.g. JSON-Schema arriving over the network
or from a UI editor). Parallel to
makeObject
but takes pre-built
jsonSchema instead of piping through an Effect schema.
The returned entity is in-memory; persist it with db.addType(entity).
@performance ― O(schema size); validates the JSON schema into a new in-memory type entity, with no database I/O.
Persists a Type definition (clones/forks the entity) so it replicates to other peers.
Runs a conflict query first: if a type with the same typename + version already exists in
this space, the existing persisted entity is returned and no duplicate is created. This is
the only supported way to add Type entities —
add
rejects them.
@performance ― Async; O(t) in the space persisted types, which it queries in full for a duplicate before writing.
A branch is a separate line of changes to an object and the objects it references. createBranch starts one,
switchBranch shows it on this device, and mergeBranch merges it into main. db.branch returns a second live copy
bound to a branch, without switching; call dispose on it when done. Changes and
branches explains the model.
Fork the object and its referenced subtree into a new branch (does not switch to it).
@param ― opts.fromHeads Fork from a historical frontier instead of the tip (a bare heads array
applies to the root only; a map forks each member from its own frontier).
@performance ― Async; O(subtree size), each member document forked as a full history copy.
client.graph is the Hypergraph: every space this device holds. It queries across spaces, finds a space’s database by
id, and holds the registry of types and objects registered in code.
@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 ― O(1) setup of two service streams, each update recombined in O(peers).
subscribeToSyncState((
state: SyncState
state) =>
var console: Console
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()).