spacetimedb 2.3.0 → 2.4.1
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/index.browser.mjs +64 -4
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +64 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +64 -4
- package/dist/index.mjs.map +1 -1
- package/dist/lib/autogen/types.d.ts +675 -2
- package/dist/lib/autogen/types.d.ts.map +1 -1
- package/dist/lib/schema.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/sdk/index.browser.mjs +1 -1
- package/dist/min/sdk/index.browser.mjs.map +1 -1
- package/dist/sdk/decompress.d.ts.map +1 -1
- package/dist/sdk/index.browser.mjs +64 -4
- package/dist/sdk/index.browser.mjs.map +1 -1
- package/dist/sdk/index.cjs +64 -4
- package/dist/sdk/index.cjs.map +1 -1
- package/dist/sdk/index.mjs +64 -4
- package/dist/sdk/index.mjs.map +1 -1
- package/dist/server/http.d.ts +1 -2
- package/dist/server/http.d.ts.map +1 -1
- package/dist/server/http.test-d.d.ts +2 -0
- package/dist/server/http.test-d.d.ts.map +1 -0
- package/dist/server/http_handlers.d.ts +82 -0
- package/dist/server/http_handlers.d.ts.map +1 -0
- package/dist/server/http_internal.d.ts +1 -32
- package/dist/server/http_internal.d.ts.map +1 -1
- package/dist/server/http_shared.d.ts +44 -0
- package/dist/server/http_shared.d.ts.map +1 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.mjs +628 -136
- package/dist/server/index.mjs.map +1 -1
- package/dist/server/runtime.d.ts +1 -0
- package/dist/server/runtime.d.ts.map +1 -1
- package/dist/server/schema.d.ts +19 -4
- package/dist/server/schema.d.ts.map +1 -1
- package/dist/server/views.d.ts +17 -1
- package/dist/server/views.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/lib/autogen/types.ts +38 -0
- package/src/lib/schema.ts +21 -0
- package/src/sdk/decompress.ts +19 -4
- package/src/sdk/logger.ts +1 -1
- package/src/server/http.test-d.ts +80 -0
- package/src/server/http.ts +14 -2
- package/src/server/http_handlers.ts +413 -0
- package/src/server/http_internal.ts +15 -142
- package/src/server/http_shared.ts +186 -0
- package/src/server/index.ts +11 -0
- package/src/server/procedures.ts +8 -30
- package/src/server/runtime.ts +137 -1
- package/src/server/schema.ts +86 -4
- package/src/server/sys.d.ts +7 -0
- package/src/server/view.test-d.ts +22 -0
- package/src/server/views.ts +127 -0
- package/dist/lib/http_types.d.ts +0 -2
- package/dist/lib/http_types.d.ts.map +0 -1
- package/src/lib/http_types.ts +0 -8
package/src/server/views.ts
CHANGED
|
@@ -10,10 +10,15 @@ import type { OptionAlgebraicType } from '../lib/option';
|
|
|
10
10
|
import type { ParamsObj } from '../lib/reducers';
|
|
11
11
|
import { type UntypedSchemaDef } from '../lib/schema';
|
|
12
12
|
import {
|
|
13
|
+
ArrayBuilder,
|
|
14
|
+
OptionBuilder,
|
|
13
15
|
RowBuilder,
|
|
16
|
+
type ColumnBuilder,
|
|
17
|
+
type ColumnMetadata,
|
|
14
18
|
type Infer,
|
|
15
19
|
type InferSpacetimeTypeOfTypeBuilder,
|
|
16
20
|
type InferTypeOfRow,
|
|
21
|
+
type RowObj,
|
|
17
22
|
type TypeBuilder,
|
|
18
23
|
} from '../lib/type_builders';
|
|
19
24
|
import { bsatnBaseSize, toPascalCase } from '../lib/util';
|
|
@@ -90,6 +95,77 @@ export type ViewOpts = {
|
|
|
90
95
|
|
|
91
96
|
type FlattenedArray<T> = T extends readonly (infer E)[] ? E : never;
|
|
92
97
|
|
|
98
|
+
// Compile-time mirror of `viewReturnRow` below. Views currently return either
|
|
99
|
+
// `array(row(...))` or `option(row(...))`; this extracts the row object type
|
|
100
|
+
// from those builders so we can inspect column metadata at the type level.
|
|
101
|
+
// Non-row returns collapse to `never`, which makes the primary-key validation
|
|
102
|
+
// below a no-op for unsupported shapes.
|
|
103
|
+
type ViewReturnRow<Ret extends ViewReturnTypeBuilder> =
|
|
104
|
+
Ret extends ArrayBuilder<infer Element>
|
|
105
|
+
? Element extends RowBuilder<infer Row>
|
|
106
|
+
? Row
|
|
107
|
+
: never
|
|
108
|
+
: Ret extends OptionBuilder<infer Value>
|
|
109
|
+
? Value extends RowBuilder<infer Row>
|
|
110
|
+
? Row
|
|
111
|
+
: never
|
|
112
|
+
: never;
|
|
113
|
+
|
|
114
|
+
// Produces a union of the returned row's column names marked with
|
|
115
|
+
// `.primaryKey()`. For example, `{ id: t.u32().primaryKey(), name: t.string() }`
|
|
116
|
+
// becomes `"id"`, while two marked columns becomes `"id" | "name"`.
|
|
117
|
+
type PrimaryKeyColumnNames<Row extends RowObj> = {
|
|
118
|
+
[K in keyof Row & string]: Row[K] extends ColumnBuilder<any, any, infer M>
|
|
119
|
+
? M extends { isPrimaryKey: true }
|
|
120
|
+
? K
|
|
121
|
+
: never
|
|
122
|
+
: never;
|
|
123
|
+
}[keyof Row & string];
|
|
124
|
+
|
|
125
|
+
// Standard conditional-type trick for distinguishing a single type from a
|
|
126
|
+
// union. We use it because zero or one primary-key column is valid, but a union
|
|
127
|
+
// of two or more column names means the row builder marked multiple primary
|
|
128
|
+
// keys.
|
|
129
|
+
type IsUnion<T, U = T> = [T] extends [never]
|
|
130
|
+
? false
|
|
131
|
+
: T extends any
|
|
132
|
+
? [U] extends [T]
|
|
133
|
+
? false
|
|
134
|
+
: true
|
|
135
|
+
: false;
|
|
136
|
+
|
|
137
|
+
// In generic code, row keys may widen from literal names like "id" | "name"
|
|
138
|
+
// to plain `string`. That means "unknown column name", not "multiple primary
|
|
139
|
+
// keys", so avoid a false-positive type error and rely on the runtime check.
|
|
140
|
+
type HasMultiplePrimaryKeys<Row extends RowObj> =
|
|
141
|
+
string extends PrimaryKeyColumnNames<Row>
|
|
142
|
+
? false
|
|
143
|
+
: IsUnion<PrimaryKeyColumnNames<Row>>;
|
|
144
|
+
|
|
145
|
+
type MultiplePrimaryKeyColumns<Ret extends ViewReturnTypeBuilder> =
|
|
146
|
+
PrimaryKeyColumnNames<ViewReturnRow<Ret>>;
|
|
147
|
+
|
|
148
|
+
type ERROR_view_return_type_can_have_at_most_one_primaryKey<
|
|
149
|
+
Columns extends string,
|
|
150
|
+
> = {
|
|
151
|
+
_primaryKeyColumns: Columns;
|
|
152
|
+
_fix: 'Remove primaryKey() from all but one column on the returned row type';
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// Used as a rest parameter type on `Schema.view` and `Schema.anonymousView`.
|
|
156
|
+
// Valid return builders produce `[]`, so callers pass no extra arguments. If
|
|
157
|
+
// the returned row has multiple `.primaryKey()` columns, this becomes a
|
|
158
|
+
// one-element tuple containing an explanatory error type, which makes the
|
|
159
|
+
// normal three-argument call fail to type-check.
|
|
160
|
+
export type ValidateViewPrimaryKey<Ret extends ViewReturnTypeBuilder> =
|
|
161
|
+
HasMultiplePrimaryKeys<ViewReturnRow<Ret>> extends true
|
|
162
|
+
? [
|
|
163
|
+
error: ERROR_view_return_type_can_have_at_most_one_primaryKey<
|
|
164
|
+
MultiplePrimaryKeyColumns<Ret>
|
|
165
|
+
>,
|
|
166
|
+
]
|
|
167
|
+
: [];
|
|
168
|
+
|
|
93
169
|
// // If we allowed functions to return either.
|
|
94
170
|
// type ViewReturn<Ret extends ViewReturnTypeBuilder> =
|
|
95
171
|
// | Infer<Ret>
|
|
@@ -143,6 +219,7 @@ export function registerView<
|
|
|
143
219
|
? AnonymousViewFn<S, Params, Ret>
|
|
144
220
|
: ViewFn<S, Params, Ret>
|
|
145
221
|
) {
|
|
222
|
+
ctx.defineFunction(exportName);
|
|
146
223
|
const paramsBuilder = new RowBuilder(params, toPascalCase(exportName));
|
|
147
224
|
|
|
148
225
|
// Register return types if they are product types
|
|
@@ -163,6 +240,22 @@ export function registerView<
|
|
|
163
240
|
returnType,
|
|
164
241
|
});
|
|
165
242
|
|
|
243
|
+
// Runtime counterpart to `ValidateViewPrimaryKey`: the type-level check gives
|
|
244
|
+
// users an early diagnostic in normal code, but this still protects dynamic
|
|
245
|
+
// or widened builders and is the source of the raw module-def metadata.
|
|
246
|
+
const primaryKeyColumns = viewPrimaryKeyColumns(ret);
|
|
247
|
+
if (primaryKeyColumns.length > 1) {
|
|
248
|
+
throw new TypeError(
|
|
249
|
+
`View '${exportName}' can have at most one primaryKey() column on its returned row type; found ${primaryKeyColumns.join(', ')}`
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
if (primaryKeyColumns.length === 1) {
|
|
253
|
+
ctx.moduleDef.viewPrimaryKeys.push({
|
|
254
|
+
viewSourceName: exportName,
|
|
255
|
+
columns: primaryKeyColumns,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
166
259
|
if (opts.name != null) {
|
|
167
260
|
ctx.moduleDef.explicitNames.entries.push({
|
|
168
261
|
tag: 'Function',
|
|
@@ -193,6 +286,40 @@ export function registerView<
|
|
|
193
286
|
});
|
|
194
287
|
}
|
|
195
288
|
|
|
289
|
+
// Inspect the returned row builder and collect the column property names marked
|
|
290
|
+
// with `.primaryKey()`. These names are the TypeScript row-builder keys, which
|
|
291
|
+
// are also the raw column names in the module definition emitted by the TS SDK.
|
|
292
|
+
function viewPrimaryKeyColumns(ret: ViewReturnTypeBuilder): string[] {
|
|
293
|
+
const row = viewReturnRow(ret);
|
|
294
|
+
if (row == null) {
|
|
295
|
+
return [];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return Object.entries(row.row)
|
|
299
|
+
.filter(
|
|
300
|
+
(
|
|
301
|
+
entry
|
|
302
|
+
): entry is [string, ColumnBuilder<any, any, ColumnMetadata<any>>] =>
|
|
303
|
+
entry[1].columnMetadata.isPrimaryKey === true
|
|
304
|
+
)
|
|
305
|
+
.map(([name]) => name);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Views can return either `array(row(...))` or `option(row(...))`. The primary
|
|
309
|
+
// key marker lives on the inner `RowBuilder`, so unwrap those two supported
|
|
310
|
+
// shapes and ignore anything else.
|
|
311
|
+
function viewReturnRow(
|
|
312
|
+
ret: ViewReturnTypeBuilder
|
|
313
|
+
): RowBuilder<any> | undefined {
|
|
314
|
+
if (ret instanceof ArrayBuilder && ret.element instanceof RowBuilder) {
|
|
315
|
+
return ret.element;
|
|
316
|
+
}
|
|
317
|
+
if (ret instanceof OptionBuilder && ret.value instanceof RowBuilder) {
|
|
318
|
+
return ret.value;
|
|
319
|
+
}
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
|
|
196
323
|
type ViewInfo<F> = {
|
|
197
324
|
fn: F;
|
|
198
325
|
deserializeParams: Deserializer<any>;
|
package/dist/lib/http_types.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"http_types.d.ts","sourceRoot":"","sources":["../../src/lib/http_types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,EACX,UAAU,EACV,WAAW,EACX,YAAY,EACZ,WAAW,GACZ,MAAM,iBAAiB,CAAC"}
|