Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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

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

19 changes: 19 additions & 0 deletions crates/bindings-typescript/src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export type UntypedSchemaDef = {
tables: readonly UntypedTableDef[];
};

let REGISTERED_SCHEMA: UntypedSchemaDef | null = null;

export function getRegisteredSchema(): UntypedSchemaDef {
if (REGISTERED_SCHEMA == null) {
throw new Error('Schema has not been registered yet. Call schema() first.');
}
return REGISTERED_SCHEMA;
}

/**
* Helper type to convert an array of TableSchema into a schema definition
*/
Expand Down Expand Up @@ -636,6 +645,16 @@ export function schema<const H extends readonly TableSchema<any, any, any>[]>(
// Modify the `MODULE_DEF` which will be read by
// __describe_module__
MODULE_DEF.tables.push(...tableDefs);
REGISTERED_SCHEMA = {
tables: handles.map(handle => ({
name: handle.tableName,
accessorName: handle.tableName,
columns: handle.rowType.row,
rowType: handle.rowSpacetimeType,
indexes: handle.idxs,
constraints: handle.constraints,
})),
};
// MODULE_DEF.typespace = typespace;
// throw new Error(
// MODULE_DEF.tables
Expand Down
5 changes: 2 additions & 3 deletions crates/bindings-typescript/src/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ type ColList = ColId[];
/**
* A helper type to extract the row type from a TableDef
*/
export type RowType<TableDef extends UntypedTableDef> = InferTypeOfRow<
TableDef['columns']
>;
export type RowType<TableDef extends Pick<UntypedTableDef, 'columns'>> =
InferTypeOfRow<TableDef['columns']>;

/**
* Coerces a column which may be a TypeBuilder or ColumnBuilder into a ColumnBuilder
Expand Down
9 changes: 9 additions & 0 deletions crates/bindings-typescript/src/lib/type_builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ export type InferTypeOfRow<T extends RowObj> = {
[K in keyof T & string]: InferTypeOfTypeBuilder<CollapseColumn<T[K]>>;
};

/**
* Helper type to extract the type of a row from an object.
*/
export type InferSpacetimeTypeOfRow<T extends RowObj> = {
[K in keyof T & string]: InferSpacetimeTypeOfTypeBuilder<
CollapseColumn<T[K]>
>;
};

/**
* Helper type to extract the Spacetime type from a row object.
*/
Expand Down
25 changes: 23 additions & 2 deletions crates/bindings-typescript/src/lib/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ import {
type TypeBuilder,
} from './type_builders';
import { bsatnBaseSize, toPascalCase } from './util';
import { type QueryBuilder, type RowTypedQuery } from '../server/query';

export type ViewCtx<S extends UntypedSchemaDef> = Readonly<{
sender: Identity;
db: ReadonlyDbView<S>;
from: QueryBuilder<S>;
}>;

export type AnonymousViewCtx<S extends UntypedSchemaDef> = Readonly<{
db: ReadonlyDbView<S>;
from: QueryBuilder<S>;
}>;

export type ReadonlyDbView<SchemaDef extends UntypedSchemaDef> = {
Expand All @@ -39,17 +42,34 @@ export type ViewOpts = {
public: true;
};

type FlattenedArray<T> = T extends readonly (infer E)[] ? E : never;

// // If we allowed functions to return either.
// type ViewReturn<Ret extends ViewReturnTypeBuilder> =
// | Infer<Ret>
// | RowTypedQuery<FlattenedArray<Infer<Ret>>>;

export type ViewFn<
S extends UntypedSchemaDef,
Params extends ParamsObj,
Ret extends ViewReturnTypeBuilder,
> = (ctx: ViewCtx<S>, params: InferTypeOfRow<Params>) => Infer<Ret>;
> =
| ((ctx: ViewCtx<S>, params: InferTypeOfRow<Params>) => Infer<Ret>)
| ((
ctx: ViewCtx<S>,
params: InferTypeOfRow<Params>
) => RowTypedQuery<FlattenedArray<Infer<Ret>>>);

export type AnonymousViewFn<
S extends UntypedSchemaDef,
Params extends ParamsObj,
Ret extends ViewReturnTypeBuilder,
> = (ctx: AnonymousViewCtx<S>, params: InferTypeOfRow<Params>) => Infer<Ret>;
> =
| ((ctx: AnonymousViewCtx<S>, params: InferTypeOfRow<Params>) => Infer<Ret>)
| ((
ctx: AnonymousViewCtx<S>,
params: InferTypeOfRow<Params>
) => RowTypedQuery<FlattenedArray<Infer<Ret>>>);

export type ViewReturnTypeBuilder =
| TypeBuilder<
Expand Down Expand Up @@ -97,6 +117,7 @@ export function defineView<
},
});

// If it is an option, we wrap the function to make the return look like an array.
if (returnType.tag == 'Sum') {
const originalFn = fn;
fn = ((ctx: ViewCtx<S>, args: InferTypeOfRow<Params>) => {
Expand Down
1 change: 1 addition & 0 deletions crates/bindings-typescript/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { reducers } from '../lib/reducers';
export { SenderError, SpacetimeHostError, errors } from './errors';
export { type Reducer, type ReducerCtx } from '../lib/reducers';
export { type DbView } from './db_view';
export { and, or, not } from './query';
export type { ProcedureCtx, TransactionCtx } from '../lib/procedures';

import './polyfills'; // Ensure polyfills are loaded
Expand Down
Loading