Skip to content

Commit 111ff87

Browse files
committed
[release/v1.4.0]: Manually apply open PR #3262: Removed @clockworklabs/typescript-sdk in favor of spacetimedb
2 parents 5738710 + edbf521 commit 111ff87

File tree

86 files changed

+404
-1545
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+404
-1545
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ jobs:
362362

363363
- name: Get pnpm store directory
364364
shell: bash
365-
working-directory: sdks/typescript
366365
run: |
367366
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
368367

.github/workflows/typescript-test.yml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626

2727
- name: Get pnpm store directory
2828
shell: bash
29-
working-directory: sdks/typescript
29+
working-directory: crates/bindings-typescript
3030
run: |
3131
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
3232
@@ -38,22 +38,14 @@ jobs:
3838
restore-keys: |
3939
${{ runner.os }}-pnpm-store-
4040
41-
- name: Build module library
41+
- name: Build module library and SDK
4242
working-directory: crates/bindings-typescript
4343
run: pnpm build
4444

45-
- name: Run module library tests
45+
- name: Run module library and SDK tests
4646
working-directory: crates/bindings-typescript
4747
run: pnpm test
4848

49-
- name: Build SDK
50-
working-directory: sdks/typescript
51-
run: pnpm build
52-
53-
- name: Run SDK tests
54-
working-directory: sdks/typescript
55-
run: pnpm test
56-
5749
# - name: Extract SpacetimeDB branch name from file
5850
# id: extract-branch
5951
# run: |
@@ -104,12 +96,12 @@ jobs:
10496
- name: Generate client bindings
10597
working-directory: modules/quickstart-chat
10698
run: |
107-
spacetime generate --lang typescript --out-dir ../../sdks/typescript/examples/quickstart-chat/src/module_bindings
108-
cd ../../sdks/typescript
109-
pnpm lint --write
99+
spacetime generate --lang typescript --out-dir ../../crates/bindings-typescript/examples/quickstart-chat/src/module_bindings
100+
cd ../../crates/bindings-typescript
101+
pnpm format
110102
111103
- name: Check for changes
112-
working-directory: sdks/typescript
104+
working-directory: crates/bindings-typescript
113105
run: |
114106
# This was copied from SpacetimeDB/tools/check-diff.sh.
115107
# It's required because `spacetime generate` creates lines with the SpacetimeDB commit
@@ -137,7 +129,7 @@ jobs:
137129
# spacetime logs quickstart-chat
138130

139131
- name: Check that quickstart-chat builds
140-
working-directory: sdks/typescript/examples/quickstart-chat
132+
working-directory: crates/bindings-typescript/examples/quickstart-chat
141133
run: pnpm build
142134

143135
# - name: Run quickstart-chat tests
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
src/sdk/client_api/*.ts linguist-generated=true
2+
src/lib/autogen/*.ts linguist-generated=true
3+
test-app/src/module_bindings/*.ts linguist-generated=true
4+
examples/quickstart/client/src/module_bindings/*.ts linguist-generated=true
File renamed without changes.
File renamed without changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Notes for maintainers
2+
3+
The directory `src/sdk/client_api` is generated from [the SpacetimeDB client-api-messages](https://github.com/clockworklabs/SpacetimeDB/tree/master/crates/client-api-messages).
4+
5+
The directory `src/lib/autogen` is generated from the SpacetimeDB `ModuleDef` definition using the `regen-typescript-moduledef` Rust program.
6+
7+
In order to regenerate both of these bindings, run `pnpm generate`.
8+
9+
Whenever the `client-api-messages` crate or the `ModuleDef` changes, you'll have to manually re-generate the definitions.
10+
11+
## Releases and publishing
12+
13+
In order to release and publish a new version of the package, update the version and run `npm publish`.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## SpacetimeDB Module Library and SDK
2+
3+
### Overview
4+
5+
This repository contains both the SpacetimeDB module library and the TypeScript SDK for SpacetimeDB. The SDK allows you to interact with the database server from a client and applies type information from your SpacetimeDB server module.
6+
7+
### Installation
8+
9+
The SDK is an NPM package, thus you can use your package manager of choice like NPM or Yarn, for example:
10+
11+
```
12+
npm add spacetimedb
13+
```
14+
15+
You can use the package in the browser, using a bundler like vite/parcel/rsbuild, in server-side applications like NodeJS, Deno, Bun, NextJS, Remix, and in Cloudflare Workers.
16+
17+
> NOTE: For usage in NodeJS 18-21, you need to install the `undici` package as a peer dependency: `npm add spacetimedb undici`. Node 22 and later are supported out of the box.
18+
19+
### Usage
20+
21+
In order to connect to a database you have to generate module bindings for your database.
22+
23+
```ts
24+
import { DbConnection } from './module_bindings';
25+
26+
const connection = DbConnection.builder()
27+
.withUri('ws://localhost:3000')
28+
.withModuleName('MODULE_NAME')
29+
.onDisconnect(() => {
30+
console.log('disconnected');
31+
})
32+
.onConnectError(() => {
33+
console.log('client_error');
34+
})
35+
.onConnect((connection, identity, _token) => {
36+
console.log(
37+
'Connected to SpacetimeDB with identity:',
38+
identity.toHexString()
39+
);
40+
41+
connection.subscriptionBuilder().subscribe('SELECT * FROM player');
42+
})
43+
.withToken('TOKEN')
44+
.build();
45+
```
46+
47+
If you need to disconnect the client:
48+
49+
```ts
50+
connection.disconnect();
51+
```
52+
53+
Typically, you will use the SDK with types generated from SpacetimeDB module. For example, given a table named `Player` you can subscribe to player updates like this:
54+
55+
```ts
56+
connection.db.player.onInsert((ctx, player) => {
57+
console.log(player);
58+
});
59+
```
60+
61+
Given a reducer called `CreatePlayer` you can call it using a call method:
62+
63+
```ts
64+
connection.reducers.createPlayer();
65+
```
66+
67+
#### React Usage
68+
69+
This module also include React hooks to subscribe to tables under the `spacetimedb/react` subpath. In order to use SpacetimeDB React hooks in your project, first add a `SpacetimeDBProvider` at the top of your component hierarchy:
70+
71+
```tsx
72+
const connectionBuilder = DbConnection.builder()
73+
.withUri('ws://localhost:3000')
74+
.withModuleName('MODULE_NAME')
75+
.withLightMode(true)
76+
.onDisconnect(() => {
77+
console.log('disconnected');
78+
})
79+
.onConnectError(() => {
80+
console.log('client_error');
81+
})
82+
.onConnect((conn, identity, _token) => {
83+
console.log(
84+
'Connected to SpacetimeDB with identity:',
85+
identity.toHexString()
86+
);
87+
88+
conn.subscriptionBuilder().subscribe('SELECT * FROM player');
89+
})
90+
.withToken('TOKEN');
91+
92+
ReactDOM.createRoot(document.getElementById('root')!).render(
93+
<React.StrictMode>
94+
<SpacetimeDBProvider connectionBuilder={connectionBuilder}>
95+
<App />
96+
</SpacetimeDBProvider>
97+
</React.StrictMode>
98+
);
99+
```
100+
101+
One you add a `SpacetimeDBProvider` to your hierarchy, you can use SpacetimeDB React hooks in your render function:
102+
103+
```tsx
104+
function App() {
105+
const conn = useSpacetimeDB<DbConnection>();
106+
const { rows: messages } = useTable<DbConnection, Message>('message');
107+
108+
...
109+
}
110+
```
111+
112+
### Developer notes
113+
114+
To run the tests, do:
115+
116+
```sh
117+
pnpm build && pnpm test
118+
```
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)