Giter Club home page Giter Club logo

spacetimedb-typescript-sdk's Introduction

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 webpack of vite, and in terminal applications

Usage

In order to connect to a database you have to create a new client:

import { SpacetimeDBClient } from "@clockworklabs/spacetimedb-sdk";

let client = new SpacetimeDBClient("spacetimedb.com/spacetimedb", "<db-name>");

If you would like to connect to the client you can call the below method. This also takes optional parameters to override the host or credentials:

client.connect();

If for some reason you need to disconnect the client:

client.disconnect();

This will connect to a database instance without a specified identity. If you want to persist an identity fetched on connection you can register an onConnect callback, which will receive a new assigned identity as an argument:

client.onConnect((identity: string) => {
  console.log(identity);
  console.log(client.token);
});

You may also pass credentials as an optional third argument:

let credentials = { identity: "<identity>", token: "<token>" };
let client = new SpacetimeDBClient(
  "spacetimedb.com/spacetimedb",
  "<db-name>",
  credentials
);

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

client.registerComponent(Player, "Player");

Then you will be able to register callbacks on insert and delete events, for example:

Player.onInsert((newPlayer: Player) => {
  console.log(newPlayer);
});

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

CreatePlayer.call("Nickname");

spacetimedb-typescript-sdk's People

Contributors

drogus avatar natethedev1 avatar dbrinkmanncw avatar gefjon avatar hsreina avatar jdetter avatar jkbz64 avatar mamcx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.