0.12.1 • Published 9 months ago

@clockworklabs/spacetimedb-sdk v0.12.1

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

SpacetimeDB SDK

Overview

This repository contains the TypeScript SDK for SpacetimeDB. The SDK allows to interact with the database server and is prepared to work with code generated from a SpacetimeDB backend code.

Installation

The SDK is an NPM package, thus you can use your package manager of choice like NPM or Yarn, for example:

npm install --save @clockworklabs/spacetimedb-sdk

You can use the package in the browser, using a bundler like vite/parcel/rsbuild, in server-side applications like NodeJS, Deno, Bun and in Cloudflare Workers.

NOTE: For usage in NodeJS 18-21, you need to install the undici package as a peer dependency: npm install @clockworklabs/spacetimedb-sdk undici. Node 22 and later are supported out of the box.

Usage

In order to connect to a database you have to generate module bindings for your database.

import { DBConnection } from './module_bindings';

const connection = DBConnection.builder()
  .withUri('ws://localhost:3000')
  .withModuleName('MODULE_NAME')
  .onDisconnect(() => {
    console.log('disconnected');
  })
  .onConnectError(() => {
    console.log('client_error');
  })
  .onConnect((connection, identity, _token) => {
    console.log(
      'Connected to SpacetimeDB with identity:',
      identity.toHexString()
    );

    connection.subscriptionBuilder().subscribe(['SELECT * FROM player']);
  })
  .withCredentials([Identity.fromString('IDENTITY'), 'TOKEN'])
  .build();

If for some reason you need to disconnect the client:

connection.disconnect();

Typically, you will use the SDK with types generated from a backend DB service. For example, given a table named Player you can subscribe to player updates like this:

connection.db.player.onInsert((ctx, player) => {
  console.log(player);
});

Given a reducer called CreatePlayer you can call it using a call method:

connection.reducers.createPlayer();
0.12.0

9 months ago

0.12.1

9 months ago

0.11.0

11 months ago

0.11.1

11 months ago

0.10.0

1 year ago

0.9.0

1 year ago

0.8.2

1 year ago

0.8.1

2 years ago

0.8.1-alpha2

2 years ago

0.8.1-alpha1

2 years ago

0.8.0

2 years ago

0.8.0-alpha.5

2 years ago

0.8.0-alpha.1

2 years ago

0.8.0-alpha.4

2 years ago

0.8.0-alpha.3

2 years ago

0.7.2

2 years ago

0.7.1

2 years ago

0.6.2

2 years ago

0.5.0

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.5.2

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.4.0-next.1

2 years ago

0.4.0-alpha

2 years ago

0.4.0-next.0

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago