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.
- package/LICENSE.txt +2 -2
- package/dist/angular/index.cjs.map +1 -1
- package/dist/angular/index.mjs.map +1 -1
- package/dist/browser/angular/index.mjs.map +1 -1
- package/dist/browser/react/index.mjs +3 -0
- package/dist/browser/react/index.mjs.map +1 -1
- package/dist/browser/svelte/index.mjs.map +1 -1
- package/dist/browser/vue/index.mjs.map +1 -1
- package/dist/index.browser.mjs +11 -9
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +11 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +11 -9
- package/dist/index.mjs.map +1 -1
- package/dist/lib/schema.d.ts.map +1 -1
- package/dist/lib/util.d.ts +11 -11
- package/dist/lib/util.d.ts.map +1 -1
- package/dist/min/index.browser.mjs +1 -1
- package/dist/min/index.browser.mjs.map +1 -1
- package/dist/min/react/index.mjs +1 -1
- package/dist/min/react/index.mjs.map +1 -1
- package/dist/min/sdk/index.browser.mjs +1 -1
- package/dist/min/sdk/index.browser.mjs.map +1 -1
- package/dist/react/index.cjs +3 -0
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.mjs +3 -0
- package/dist/react/index.mjs.map +1 -1
- package/dist/react/useTable.d.ts.map +1 -1
- package/dist/sdk/index.browser.mjs +11 -9
- package/dist/sdk/index.browser.mjs.map +1 -1
- package/dist/sdk/index.cjs +11 -9
- package/dist/sdk/index.cjs.map +1 -1
- package/dist/sdk/index.mjs +11 -9
- package/dist/sdk/index.mjs.map +1 -1
- package/dist/sdk/procedures.d.ts +1 -1
- package/dist/sdk/procedures.d.ts.map +1 -1
- package/dist/sdk/reducers.d.ts +2 -3
- package/dist/sdk/reducers.d.ts.map +1 -1
- package/dist/server/index.mjs +11 -9
- package/dist/server/index.mjs.map +1 -1
- package/dist/svelte/index.cjs.map +1 -1
- package/dist/svelte/index.mjs.map +1 -1
- package/dist/tanstack/SpacetimeDBQueryClient.d.ts +2 -3
- package/dist/tanstack/SpacetimeDBQueryClient.d.ts.map +1 -1
- package/dist/tanstack/hooks.d.ts +3 -6
- package/dist/tanstack/hooks.d.ts.map +1 -1
- package/dist/tanstack/index.cjs +4 -1
- package/dist/tanstack/index.cjs.map +1 -1
- package/dist/tanstack/index.mjs +4 -1
- package/dist/tanstack/index.mjs.map +1 -1
- package/dist/vue/index.cjs.map +1 -1
- package/dist/vue/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/lib/schema.ts +4 -1
- package/src/lib/util.ts +18 -21
- package/src/react/useTable.ts +7 -0
- package/src/sdk/procedures.ts +1 -3
- package/src/sdk/reducers.ts +5 -7
- package/src/tanstack/SpacetimeDBQueryClient.ts +4 -2
- package/src/tanstack/hooks.ts +3 -2
package/package.json
CHANGED
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
|
-
|
|
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>(
|
|
114
|
-
|
|
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())
|
|
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];
|
package/src/react/useTable.ts
CHANGED
|
@@ -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) {
|
package/src/sdk/procedures.ts
CHANGED
|
@@ -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
|
|
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
|
}
|
package/src/sdk/reducers.ts
CHANGED
|
@@ -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
|
-
?
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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:
|
|
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 =
|
|
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 =
|
|
56
|
+
const querySql = toSql(query);
|
|
55
57
|
|
|
56
58
|
queryRegistry.set(querySql, { accessorName, whereExpr });
|
|
57
59
|
|
package/src/tanstack/hooks.ts
CHANGED
|
@@ -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:
|
|
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:
|
|
64
|
+
query: Query<TableDef>,
|
|
64
65
|
options?: Omit<
|
|
65
66
|
UseSuspenseQueryOptions<
|
|
66
67
|
RowType<TableDef>[],
|