Skip to content

Mutations

Objects returned from query are automatically tracked by the Client and direct manipulation of them will result in writes being dispatched over the network to listening peers in the space.

import { Client } from '@dxos/client';
const client = new Client();
await client.initialize();
// Ensure an identity exists.
if (!client.halo.identity.get()) {
await client.halo.createIdentity();
}
// Get a list of all spaces.
const spaces = client.spaces.get();
// Grab a space.
const space = spaces[0];
// Grab an object.
const result = await space.db.query({ type: 'task' }).run();
const object = result.objects[0];
// Mutate the object directly.
object.completed = true;

The mutation will queue up inside the Client and begin propagating to listening peers on the next tick.

Creating objects

To insert an object into an ECHO space, simply construct it and call the add method to begin tracking it.

Untyped

Without strong types, the generic Expando class can be used:

import { Client } from '@dxos/client';
import { Expando, create } from '@dxos/client/echo';
const client = new Client();
await client.initialize();
// Ensure an identity exists.
if (!client.halo.identity.get()) {
await client.halo.createIdentity();
}
// Get a list of all spaces.
const spaces = client.spaces.get();
// Grab a space.
const space = spaces[0];
// Create an object.
const object = create(Expando, { type: 'task', name: 'buy milk' });
// Add the object to the space.
space.db.add(object);

Typed

If strong types are desired, an instance of a specific TypedObject descendant should be used:

import { Client } from '@dxos/client';
import { create } from '@dxos/client/echo';
import { Task } from './schema';
const client = new Client({ types: [Task] });
await client.initialize();
// Ensure an identity exists.
if (!client.halo.identity.get()) {
await client.halo.createIdentity();
}
// Get a list of all spaces.
const spaces = client.spaces.get();
// Grab a space.
const space = spaces[0];
// Create an object.
const object = create(Task, { name: 'buy milk' });
// Add the object to the space.
space.db.add(object);

Removing objects

To remove an object (typed or untyped) call the remove API on a space.

space.db.remove(object);