Skip to main content

Writing data

The Lark SDK gives you several ways to write data. All write operations are optimistic: your local state updates immediately, and the write is sent to the server in the background. If the server rejects it (due to security rules, for example), the local state rolls back.

set(value)

Overwrites the data at a reference. Any existing data at that location, including all children, is replaced.
set() replaces everything at that path. If you only want to update specific fields without touching others, use update() instead.

update(values)

Performs a shallow merge at the reference location. Only the specified keys are written. Other existing keys are left untouched.

Multi-path updates

You can update multiple locations atomically by calling update() on a parent reference with full paths as keys:
This is powerful for keeping denormalized data in sync. Either all paths update or none do.
Multi-path updates are one of the most useful patterns in Lark. Use them any time you need to write to several locations as a single atomic operation.

remove()

Deletes data at a reference. Equivalent to set(null).

push(value)

Generates a unique key and writes the value under it. Returns a reference to the new child location.
You can also call push() without a value to just generate the key:

setWithPriority(value, priority)

Sets data along with a priority value. Priorities affect the default sort order when querying.

setPriority(priority)

Updates only the priority of an existing node without changing its value.

Server values

The SDK provides special server-side values through ServerValue.

ServerValue.TIMESTAMP

Resolves to the server’s current timestamp (milliseconds since epoch) when the write is processed.
ServerValue.TIMESTAMP is evaluated on the server, so it reflects the server’s clock, not the client’s. This is important for consistency across clients in different time zones or with inaccurate local clocks.

Write guarantees

All writes return a promise. The promise resolves when the server has acknowledged the write. If the write is rejected (e.g., by security rules), the promise rejects with a LarkError.
Even though writes are optimistic (local state updates immediately), you should still handle errors to catch permission issues and data validation failures.