Skip to main content

Connecting

The LarkDatabase class is your entry point. Pass your project, database, and auth details to the constructor and start using it immediately. The connection happens automatically.

Creating an instance

The constructor takes a path string in the format projectId/databaseId, and an options object:
The connection is established lazily. You don’t need to call anything else before reading, writing, or subscribing. Operations queue automatically and execute once the connection is authenticated.

Options

All connection configuration is passed as the second argument to the constructor:
You must provide either anonymous: true or a token. If you pass both, the token takes priority.
Self-hosting Lark? Set domain to your own host instead of the default larkdb.net. See Open source.

Waiting for the connection

If you need to confirm the connection is established before proceeding, you can optionally call await db.connect():
Most code doesn’t need this. Lazy queuing handles it for you. Use await db.connect() when you need to gate logic on a confirmed connection (e.g., showing a “connected” indicator, or failing fast if the server is unreachable).

Transport selection

The SDK uses WebSocket by default. To opt in to WebTransport, set transport to 'auto' or 'webtransport':

Connection states

A connection moves through these states in order:

Properties

Once connected, several properties are available on the db instance:

Lifecycle events

Use callback methods to react to connection state changes:
All lifecycle callbacks return an unsubscribe function, so you can stop listening when you no longer need them.

Disconnecting

There are two ways to take a connection offline, and they behave differently:

disconnect()

Fully tears down the connection. Clears all cached data, removes all subscriptions, and resets the client. Use this when you’re done with the database entirely.

goOffline()

Pauses the connection but preserves your local cache and subscription registrations. When you call goOnline() later, subscriptions are re-established and you pick up where you left off.
After calling disconnect(), you need to create a new LarkDatabase instance and call connect() again to re-establish the connection. After goOffline(), a simple goOnline() is enough.