Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
6529827
Big prototype
cloutiertyler Nov 3, 2025
e9c0a6f
RemoteModule-ify
cloutiertyler Nov 3, 2025
541f493
Smaller fixes
cloutiertyler Nov 3, 2025
5da15d8
Cleaning up where code lives
cloutiertyler Nov 3, 2025
269ad2f
Small fixes
cloutiertyler Nov 3, 2025
051fb27
About to try new code gen
cloutiertyler Nov 4, 2025
83a23f9
Almost there, just in a bit of a pickle
cloutiertyler Nov 4, 2025
ae3556e
Fixed many small issues, now begin the AlgebraicType stuff
cloutiertyler Nov 5, 2025
05e663f
clusterfuck
cloutiertyler Nov 5, 2025
55fba2e
Sweet jesus it builds
cloutiertyler Nov 5, 2025
7099b65
Sweet heavens I think it's alive
cloutiertyler Nov 5, 2025
0bb2976
It finally builds successfully
cloutiertyler Nov 6, 2025
0b0b06d
Small comment
cloutiertyler Nov 6, 2025
08dba2f
Living the dream
cloutiertyler Nov 8, 2025
3f3e6a6
Progress!
cloutiertyler Nov 8, 2025
ac4e15a
It is magnifico
cloutiertyler Nov 9, 2025
16814cb
Small adjustments
cloutiertyler Nov 9, 2025
384f8b0
pnpm format
cloutiertyler Nov 9, 2025
cb16789
Removed generation of variant types since they can now be derived fro…
cloutiertyler Nov 9, 2025
6782e5e
merge master
cloutiertyler Nov 9, 2025
d398d25
pnpm format
cloutiertyler Nov 9, 2025
c61fae7
cargo fmt
cloutiertyler Nov 9, 2025
64b3404
misconfigured import
cloutiertyler Nov 9, 2025
31243a7
Fixed broken type tests
cloutiertyler Nov 9, 2025
57bd49b
Fixed up the API a bit to have fewer breaking changes
cloutiertyler Nov 10, 2025
dadd620
Implemented all the index stuff
cloutiertyler Nov 11, 2025
ffa5336
Good Heavens all the tests pass
cloutiertyler Nov 11, 2025
4d2aff4
pnpm format
cloutiertyler Nov 11, 2025
6d5e575
cargo fmt
cloutiertyler Nov 11, 2025
487d5a3
formatting
cloutiertyler Nov 11, 2025
256d04f
Builds, tests, works
cloutiertyler Nov 11, 2025
181e50f
Formatting
cloutiertyler Nov 11, 2025
fe91e78
Merged master
cloutiertyler Nov 11, 2025
6fbb8fc
Now generating module_bindings for examples and also moved pnpm scrip…
cloutiertyler Oct 30, 2025
ef52bb6
Fixed the testnet thing
cloutiertyler Nov 11, 2025
ed7a210
Fix quickstart docs to use the new APIs
cloutiertyler Nov 11, 2025
29913d4
Snap and format
cloutiertyler Nov 11, 2025
a2ca10a
pnpm format
cloutiertyler Nov 11, 2025
9f7fe2c
Code gen determinism and return value of useTable
cloutiertyler Nov 11, 2025
a5635fe
Merge remote-tracking branch 'origin/master' into tyler/ts-sdk-improv…
cloutiertyler Nov 11, 2025
0461c70
Fixing tests
cloutiertyler Nov 12, 2025
6d270cf
Fixed compile error
cloutiertyler Nov 12, 2025
9375240
Dovos
cloutiertyler Nov 12, 2025
767d5f9
Use views in codegen
coolreader18 Nov 12, 2025
3f1ec77
Merge branch 'master' into tyler/ts-sdk-improvements
cloutiertyler Nov 14, 2025
d7dd8ba
Fixed failing tests
cloutiertyler Nov 14, 2025
88ae277
Forgot to uncomment code
cloutiertyler Nov 14, 2025
efdddc3
Regenerated bindings
cloutiertyler Nov 14, 2025
903f3a8
cargo insta review
cloutiertyler Nov 14, 2025
0527836
Hopefully fix tests
cloutiertyler Nov 15, 2025
db58418
Somethin still ain'r raight
cloutiertyler Nov 16, 2025
7511d33
Fix errors
coolreader18 Nov 17, 2025
8242dda
Merge branch 'master' into tyler/ts-sdk-improvements
cloutiertyler Nov 18, 2025
178b1bb
typo
cloutiertyler Nov 18, 2025
9ccbb63
Merge branch 'master' into tyler/ts-sdk-improvements
cloutiertyler Nov 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ function App() {
const [settingName, setSettingName] = useState(false);
const [systemMessages, setSystemMessages] = useState([] as Message[]);
const [newMessage, setNewMessage] = useState('');

const conn = useSpacetimeDB<DbConnection>();

conn.setReducerFlags()
const { identity, isActive: connected } = conn;

// Subscribe to all messages in the chat
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RowType, UntypedTableDef } from './table';
import type { ColumnMetadata, IndexTypes } from './type_builders';
import type { CollapseTuple, Prettify } from './type_util';
import { Range } from './range';
import { Range } from '../server/range';
import type { ColumnIsUnique } from './constraints';

/**
Expand Down Expand Up @@ -56,30 +56,69 @@ export type Index<
? UniqueIndex<TableDef, I>
: RangedIndex<TableDef, I>;

/**
* A type representing a collection of read-only indexes defined on a table.
*/
export type ReadonlyIndexes<
TableDef extends UntypedTableDef,
I extends Record<string, UntypedIndex<keyof TableDef['columns'] & string>>,
> = {
[k in keyof I]: ReadonlyIndex<TableDef, I[k]>;
};

/**
* A type representing a read-only database index, which can be either unique or ranged.
* This type only exposes read-only operations.
*/
export type ReadonlyIndex<
TableDef extends UntypedTableDef,
I extends UntypedIndex<keyof TableDef['columns'] & string>,
> = I['unique'] extends true
? ReadonlyUniqueIndex<TableDef, I>
: ReadonlyRangedIndex<TableDef, I>;

/**
* A type representing a read-only unique index on a database table.
*/
export type ReadonlyUniqueIndex<
TableDef extends UntypedTableDef,
I extends UntypedIndex<keyof TableDef['columns'] & string>,
> = {
find(colVal: IndexVal<TableDef, I>): RowType<TableDef> | null;
};

/**
* A type representing a unique index on a database table.
* Unique indexes enforce that the indexed columns contain unique values.
*/
export type UniqueIndex<
TableDef extends UntypedTableDef,
I extends UntypedIndex<keyof TableDef['columns'] & string>,
> = {
find(col_val: IndexVal<TableDef, I>): RowType<TableDef> | null;
delete(col_val: IndexVal<TableDef, I>): boolean;
update(col_val: RowType<TableDef>): RowType<TableDef>;
> = ReadonlyUniqueIndex<TableDef, I> & {
delete(colVal: IndexVal<TableDef, I>): boolean;
update(colVal: RowType<TableDef>): RowType<TableDef>;
};

/**
* A type representing a ranged index on a database table.
* Ranged indexes allow for range queries on the indexed columns.
* A type representing a read-only ranged index on a database table.
*/
export type RangedIndex<
export type ReadonlyRangedIndex<
TableDef extends UntypedTableDef,
I extends UntypedIndex<keyof TableDef['columns'] & string>,
> = {
filter(
range: IndexScanRangeBounds<TableDef, I>
): IterableIterator<RowType<TableDef>>;
};

/**
* A type representing a ranged index on a database table.
* Ranged indexes allow for range queries on the indexed columns.
*/
export type RangedIndex<
TableDef extends UntypedTableDef,
I extends UntypedIndex<keyof TableDef['columns'] & string>,
> = ReadonlyRangedIndex<TableDef, I> & {
delete(range: IndexScanRangeBounds<TableDef, I>): number;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { ProductType } from '../lib/algebraic_type';
import Lifecycle from '../lib/autogen/lifecycle_type';
import type RawReducerDefV9 from '../lib/autogen/raw_reducer_def_v_9_type';
import type { ConnectionId } from '../lib/connection_id';
import type { Identity } from '../lib/identity';
import type { Timestamp } from '../lib/timestamp';
import type { ProductType } from './algebraic_type';
import Lifecycle from './autogen/lifecycle_type';
import type RawReducerDefV9 from './autogen/raw_reducer_def_v_9_type';
import type { ConnectionId } from './connection_id';
import type { Identity } from './identity';
import type { Timestamp } from './timestamp';
import type { UntypedReducersDef } from '../sdk/reducers';
import type { DbView } from '../server/db_view';
import { MODULE_DEF, type UntypedSchemaDef } from './schema';
import type { Table } from './table';
import type {
InferTypeOfRow,
RowBuilder,
RowObj,
StringBuilder,

Check failure on line 14 in crates/bindings-typescript/src/lib/reducers.ts

View workflow job for this annotation

GitHub Actions / build

'StringBuilder' is defined but never used. Allowed unused vars must match /^_/u
TypeBuilder,
} from './type_builders';

Expand Down Expand Up @@ -58,13 +60,6 @@
payload: ParamsAsObject<Params>
) => void | { tag: 'ok' } | { tag: 'err'; value: string };

/**
* A type representing the database view, mapping table names to their corresponding Table handles.
*/
export type DbView<SchemaDef extends UntypedSchemaDef> = {
readonly [Tbl in SchemaDef['tables'][number] as Tbl['name']]: Table<Tbl>;
};

/**
* Authentication information for the caller of a reducer.
*/
Expand Down Expand Up @@ -133,8 +128,9 @@
fn: Reducer<any, any>,
lifecycle?: RawReducerDefV9['lifecycle']
): void {
if (existingReducers.has(name))
if (existingReducers.has(name)) {
throw new TypeError(`There is already a reducer with the name '${name}'`);
}
existingReducers.add(name);

const paramType: ProductType = {
Expand Down Expand Up @@ -263,3 +259,111 @@
>(name: string, params: Params, fn: Reducer<S, Params>): void {
pushReducer(name, params, fn, Lifecycle.OnDisconnect);
}

/**
* Represents a handle to a database reducer, including its name and argument type.
*/
export type ReducerSchema<
ReducerName extends string,
Params extends ParamsObj,
> = {
/**
* The name of the reducer.
*/
readonly reducerName: ReducerName;

/**
* The type of the parameters object expected by the reducer.
*/
readonly paramsType: Params;

/**
*
*/
readonly paramsSpacetimeType: ProductType;

/**
* The {@link RawReducerDefV9} of the configured reducer.
*/
readonly reducerDef: RawReducerDefV9;
};

class Reducers<ReducersDef extends UntypedReducersDef> {
/**
* Phantom type to track the reducers definition
*/
reducersType!: ReducersDef;
}

/**
* Helper type to convert an array of TableSchema into a schema definition
*/
type ReducersToSchema<T extends readonly ReducerSchema<any, any>[]> = {
reducers: {
/** @type {UntypedReducerDef} */
readonly [i in keyof T]: {
name: T[i]['reducerName'];
params: T[i]['paramsType'];
paramsSpacetimeType: T[i]['paramsSpacetimeType'];
};
};
};

/**
* Creates a schema from table definitions
* @param handles - Array of table handles created by table() function
* @returns ColumnBuilder representing the complete database schema
* @example
* ```ts
* const s = schema(
* table({ name: 'user' }, userType),
* table({ name: 'post' }, postType)
* );
* ```
*/
export function reducers<const H extends readonly ReducerSchema<any, any>[]>(
...handles: H
): Reducers<ReducersToSchema<H>>;

/**
* Creates a schema from table definitions (array overload)
* @param handles - Array of table handles created by table() function
* @returns ColumnBuilder representing the complete database schema
*/
export function reducers<const H extends readonly ReducerSchema<any, any>[]>(
handles: H
): Reducers<ReducersToSchema<H>>;

export function reducers(
...args:

Check failure on line 338 in crates/bindings-typescript/src/lib/reducers.ts

View workflow job for this annotation

GitHub Actions / build

'args' is defined but never used. Allowed unused args must match /^_/u
| [readonly ReducerSchema<any, any>[]]
| readonly ReducerSchema<any, any>[]
): Reducers<UntypedReducersDef> {
return new Reducers();
}

export function reducerSchema<
ReducerName extends string,
Params extends ParamsObj,
>(
name: ReducerName,
params: Params
): ReducerSchema<ReducerName, Params> {
const paramType: ProductType = {
elements: Object.entries(params).map(([n, c]) => ({
name: n,
algebraicType: c.algebraicType,
})),
};
return {
reducerName: name,
paramsType: params,
paramsSpacetimeType: paramType,
reducerDef: {
name,
params: paramType,
lifecycle: undefined,
},
};
}

Loading
Loading