spacetimedb 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE.txt +2 -2
  2. package/dist/angular/index.cjs.map +1 -1
  3. package/dist/angular/index.mjs.map +1 -1
  4. package/dist/browser/angular/index.mjs.map +1 -1
  5. package/dist/browser/react/index.mjs +3 -0
  6. package/dist/browser/react/index.mjs.map +1 -1
  7. package/dist/browser/svelte/index.mjs.map +1 -1
  8. package/dist/browser/vue/index.mjs.map +1 -1
  9. package/dist/index.browser.mjs +11 -9
  10. package/dist/index.browser.mjs.map +1 -1
  11. package/dist/index.cjs +11 -9
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.mjs +11 -9
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/lib/schema.d.ts.map +1 -1
  16. package/dist/lib/util.d.ts +11 -11
  17. package/dist/lib/util.d.ts.map +1 -1
  18. package/dist/min/index.browser.mjs +1 -1
  19. package/dist/min/index.browser.mjs.map +1 -1
  20. package/dist/min/react/index.mjs +1 -1
  21. package/dist/min/react/index.mjs.map +1 -1
  22. package/dist/min/sdk/index.browser.mjs +1 -1
  23. package/dist/min/sdk/index.browser.mjs.map +1 -1
  24. package/dist/react/index.cjs +3 -0
  25. package/dist/react/index.cjs.map +1 -1
  26. package/dist/react/index.mjs +3 -0
  27. package/dist/react/index.mjs.map +1 -1
  28. package/dist/react/useTable.d.ts.map +1 -1
  29. package/dist/sdk/index.browser.mjs +11 -9
  30. package/dist/sdk/index.browser.mjs.map +1 -1
  31. package/dist/sdk/index.cjs +11 -9
  32. package/dist/sdk/index.cjs.map +1 -1
  33. package/dist/sdk/index.mjs +11 -9
  34. package/dist/sdk/index.mjs.map +1 -1
  35. package/dist/sdk/procedures.d.ts +1 -1
  36. package/dist/sdk/procedures.d.ts.map +1 -1
  37. package/dist/sdk/reducers.d.ts +2 -3
  38. package/dist/sdk/reducers.d.ts.map +1 -1
  39. package/dist/server/index.mjs +11 -9
  40. package/dist/server/index.mjs.map +1 -1
  41. package/dist/svelte/index.cjs.map +1 -1
  42. package/dist/svelte/index.mjs.map +1 -1
  43. package/dist/tanstack/SpacetimeDBQueryClient.d.ts +2 -3
  44. package/dist/tanstack/SpacetimeDBQueryClient.d.ts.map +1 -1
  45. package/dist/tanstack/hooks.d.ts +3 -6
  46. package/dist/tanstack/hooks.d.ts.map +1 -1
  47. package/dist/tanstack/index.cjs +4 -1
  48. package/dist/tanstack/index.cjs.map +1 -1
  49. package/dist/tanstack/index.mjs +4 -1
  50. package/dist/tanstack/index.mjs.map +1 -1
  51. package/dist/vue/index.cjs.map +1 -1
  52. package/dist/vue/index.mjs.map +1 -1
  53. package/package.json +1 -1
  54. package/src/lib/schema.ts +4 -1
  55. package/src/lib/util.ts +18 -21
  56. package/src/react/useTable.ts +7 -0
  57. package/src/sdk/procedures.ts +1 -3
  58. package/src/sdk/reducers.ts +5 -7
  59. package/src/tanstack/SpacetimeDBQueryClient.ts +4 -2
  60. package/src/tanstack/hooks.ts +3 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spacetimedb",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "API and ABI bindings for the SpacetimeDB TypeScript module library",
5
5
  "homepage": "https://github.com/clockworklabs/SpacetimeDB#readme",
6
6
  "bugs": {
package/src/lib/schema.ts CHANGED
@@ -91,7 +91,10 @@ export function tableToSchema<
91
91
 
92
92
  type AllowedCol = keyof T['rowType']['row'] & string;
93
93
  return {
94
- sourceName: accName,
94
+ // For client,`schama.tableName` will always be there as canonical name.
95
+ // For module, if explicit name is not provided via `name`, accessor name will
96
+ // be used, it is stored as alias in database, hence works in query builder.
97
+ sourceName: schema.tableName || accName,
95
98
  accessorName: accName,
96
99
  columns: schema.rowType.row, // typed as T[i]['rowType']['row'] under TablesToSchema<T>
97
100
  rowType: schema.rowSpacetimeType,
package/src/lib/util.ts CHANGED
@@ -1,20 +1,11 @@
1
+ import type { AlgebraicType } from './algebraic_type';
2
+ import type { Typespace } from './autogen/types';
1
3
  import BinaryReader from './binary_reader';
2
4
  import BinaryWriter from './binary_writer';
5
+ import type { ParamsObj } from './reducers';
6
+ import type { ColumnBuilder, TypeBuilder } from './type_builders';
3
7
  import type { CamelCase, SnakeCase } from './type_util';
4
8
 
5
- /**
6
- * Converts a string to PascalCase (UpperCamelCase).
7
- * @param str The string to convert
8
- * @returns The converted string
9
- */
10
- export function toPascalCase(s: string): string {
11
- const str = s.replace(/([-_][a-z])/gi, $1 => {
12
- return $1.toUpperCase().replace('-', '').replace('_', '');
13
- });
14
-
15
- return str.charAt(0).toUpperCase() + str.slice(1);
16
- }
17
-
18
9
  export function deepEqual(obj1: any, obj2: any): boolean {
19
10
  // If both are strictly equal (covers primitives and reference equality), return true
20
11
  if (obj1 === obj2) return true;
@@ -105,15 +96,26 @@ export function u256ToHexString(data: bigint): string {
105
96
  return uint8ArrayToHexString(u256ToUint8Array(data));
106
97
  }
107
98
 
99
+ /**
100
+ * Converts a string to PascalCase (UpperCamelCase).
101
+ * @param str The string to convert
102
+ * @returns The converted string
103
+ */
104
+ export function toPascalCase(s: string): string {
105
+ const str = toCamelCase(s);
106
+ return str.charAt(0).toUpperCase() + str.slice(1);
107
+ }
108
+
108
109
  /**
109
110
  * Type safe conversion from a string like "some_identifier-name" to "someIdentifierName".
110
111
  * @param str The string to convert
111
112
  * @returns The converted string
112
113
  */
113
- export function toCamelCase<T extends string>(str: T): CamelCase<T> {
114
- return str
114
+ export function toCamelCase<T extends string>(s: T): CamelCase<T> {
115
+ const str = s
115
116
  .replace(/[-_]+/g, '_') // collapse runs to a single separator (no backtracking issue)
116
- .replace(/_([a-zA-Z0-9])/g, (_, c) => c.toUpperCase()) as CamelCase<T>;
117
+ .replace(/_([a-zA-Z0-9])/g, (_, c) => c.toUpperCase());
118
+ return (str.charAt(0).toLowerCase() + str.slice(1)) as CamelCase<T>;
117
119
  }
118
120
 
119
121
  /** Type safe conversion from a string like "some_Identifier-name" to "some_identifier_name".
@@ -127,11 +129,6 @@ export function toSnakeCase<T extends string>(str: T): SnakeCase<T> {
127
129
  .toLowerCase() as SnakeCase<T>;
128
130
  }
129
131
 
130
- import type { AlgebraicType } from './algebraic_type';
131
- import type { Typespace } from './autogen/types';
132
- import type { ColumnBuilder, TypeBuilder } from './type_builders';
133
- import type { ParamsObj } from './reducers';
134
-
135
132
  export function bsatnBaseSize(typespace: Typespace, ty: AlgebraicType): number {
136
133
  const assumedArrayLength = 4;
137
134
  while (ty.tag === 'Ref') ty = typespace.types[ty.value];
@@ -107,6 +107,13 @@ export function useTable<TableDef extends UntypedTableDef>(
107
107
  // eslint-disable-next-line react-hooks/exhaustive-deps
108
108
  }, [connectionState, accessorName, querySql, subscribeApplied]);
109
109
 
110
+ // Invalidate the cached snapshot when computeSnapshot changes (e.g. when
111
+ // subscribeApplied flips to true) so getSnapshot() recomputes on the next
112
+ // render instead of returning a stale [rows, false] tuple.
113
+ useEffect(() => {
114
+ lastSnapshotRef.current = null;
115
+ }, [computeSnapshot]);
116
+
110
117
  useEffect(() => {
111
118
  const connection = connectionState.getConnection()!;
112
119
  if (connectionState.isActive && connection) {
@@ -19,9 +19,7 @@ export type ProceduresView<RemoteModule> = IfAny<
19
19
  RemoteModule extends UntypedRemoteModule
20
20
  ? // x: camelCase(name)
21
21
  {
22
- [K in RemoteModule['procedures'][number] as CamelCase<
23
- K['accessorName']
24
- >]: (
22
+ [K in RemoteModule['procedures'][number] as K['accessorName']]: (
25
23
  params: InferTypeOfRow<K['params']>
26
24
  ) => Promise<Infer<K['returnType']>>;
27
25
  }
@@ -3,7 +3,6 @@ import type { ReducerSchema } from '../lib/reducer_schema';
3
3
  import type { ParamsObj } from '../lib/reducers';
4
4
  import type { CoerceRow } from '../lib/table';
5
5
  import { RowBuilder, type InferTypeOfRow } from '../lib/type_builders';
6
- import type { CamelCase } from '../lib/type_util';
7
6
  import { toCamelCase } from '../lib/util';
8
7
  import type { SubscriptionEventContextInterface } from './event_context';
9
8
  import type { UntypedRemoteModule } from './spacetime_module';
@@ -25,11 +24,10 @@ export type ReducersView<RemoteModule> = IfAny<
25
24
  RemoteModule,
26
25
  ReducersViewLoose,
27
26
  RemoteModule extends UntypedRemoteModule
28
- ? // x: camelCase(name)
29
- {
30
- [K in RemoteModule['reducers'][number] as CamelCase<
31
- K['accessorName']
32
- >]: (params: InferTypeOfRow<K['params']>) => Promise<void>;
27
+ ? {
28
+ [K in RemoteModule['reducers'][number] as K['accessorName']]: (
29
+ params: InferTypeOfRow<K['params']>
30
+ ) => Promise<void>;
33
31
  }
34
32
  : never
35
33
  >;
@@ -69,7 +67,7 @@ type ReducersToSchema<T extends readonly ReducerSchema<any, any>[]> = {
69
67
  /** @type {UntypedReducerDef} */
70
68
  readonly [i in keyof T]: {
71
69
  name: T[i]['reducerName'];
72
- accessorName: CamelCase<T[i]['accessorName']>;
70
+ accessorName: T[i]['accessorName'];
73
71
  params: T[i]['params']['row'];
74
72
  paramsType: T[i]['paramsSpacetimeType'];
75
73
  };
@@ -4,13 +4,15 @@ import type {
4
4
  QueryFunction,
5
5
  } from '@tanstack/react-query';
6
6
  import {
7
+ type Query,
8
+ toSql,
7
9
  type BooleanExpr,
8
10
  evaluateBooleanExpr,
9
11
  getQueryAccessorName,
10
12
  getQueryWhereClause,
11
13
  } from '../lib/query';
12
14
 
13
- type QueryInput = { toSql(): string } & Record<string, any>;
15
+ type QueryInput = Query<any>;
14
16
 
15
17
  const queryRegistry = new Map<
16
18
  string,
@@ -51,7 +53,7 @@ export function spacetimeDBQuery(
51
53
  const query = queryOrSkip;
52
54
  const accessorName = getQueryAccessorName(query);
53
55
  const whereExpr = getQueryWhereClause(query);
54
- const querySql = query.toSql();
56
+ const querySql = toSql(query);
55
57
 
56
58
  queryRegistry.set(querySql, { accessorName, whereExpr });
57
59
 
@@ -6,6 +6,7 @@ import type {
6
6
  UseSuspenseQueryResult,
7
7
  } from '@tanstack/react-query';
8
8
  import type { UntypedTableDef, RowType } from '../lib/table';
9
+ import type { Query } from '../lib/query';
9
10
  import { spacetimeDBQuery } from './SpacetimeDBQueryClient';
10
11
 
11
12
  export type UseSpacetimeDBQueryResult<T> = [
@@ -29,7 +30,7 @@ export type UseSpacetimeDBSuspenseQueryResult<T> = [
29
30
  // useSpacetimeDBQuery(tables.user.where(r => r.online.eq(true)))
30
31
  // useSpacetimeDBQuery(condition ? tables.user : 'skip')
31
32
  export function useSpacetimeDBQuery<TableDef extends UntypedTableDef>(
32
- queryOrSkip: ({ toSql(): string } & Record<string, any>) | 'skip',
33
+ queryOrSkip: Query<TableDef> | 'skip',
33
34
  // any useQuery option (e.g. enabled, refetchInterval, select, placeholderData),
34
35
  // except queryKey, queryFn, and meta (managed internally)
35
36
  options?: Omit<
@@ -60,7 +61,7 @@ export function useSpacetimeDBQuery<TableDef extends UntypedTableDef>(
60
61
  // until data is ready, a parent <Suspense fallback={…}> handles the loading UI.
61
62
  // does not support 'skip' because useSuspenseQuery must always resolve
62
63
  export function useSpacetimeDBSuspenseQuery<TableDef extends UntypedTableDef>(
63
- query: { toSql(): string } & Record<string, any>,
64
+ query: Query<TableDef>,
64
65
  options?: Omit<
65
66
  UseSuspenseQueryOptions<
66
67
  RowType<TableDef>[],