Skip to content
Open
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
1 change: 1 addition & 0 deletions crates/bindings-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"@vitest/coverage-v8": "^3.2.4",
"brotli-size-cli": "^1.0.0",
"eslint": "^9.33.0",
"eslint-plugin-jsdoc": "^61.5.0",
"globals": "^15.14.0",
"size-limit": "^11.2.0",
"ts-node": "^10.9.2",
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings-typescript/src/lib/constraints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { UntypedTableDef } from './table';
import type { table, UntypedTableDef } from './table';
import type { ColumnMetadata } from './type_builders';

/**
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings-typescript/src/lib/indexes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RowType, UntypedTableDef } from './table';
import type { RowType, table, UntypedTableDef } from './table';
import type { ColumnMetadata, IndexTypes } from './type_builders';
import type { CollapseTuple, Prettify } from './type_util';
import { Range } from '../server/range';
Expand Down
137 changes: 0 additions & 137 deletions crates/bindings-typescript/src/lib/procedures.ts
Original file line number Diff line number Diff line change
@@ -1,137 +0,0 @@
import { AlgebraicType, ProductType } from '../lib/algebraic_type';
import type { ConnectionId } from '../lib/connection_id';
import type { Identity } from '../lib/identity';
import type { Timestamp } from '../lib/timestamp';
import type { HttpClient } from '../server/http_internal';
import type { ParamsObj, ReducerCtx } from './reducers';
import {
MODULE_DEF,
registerTypesRecursively,
type UntypedSchemaDef,
} from './schema';
import {
type Infer,
type InferTypeOfRow,
type TypeBuilder,
} from './type_builders';
import type { CamelCase } from './type_util';
import {
bsatnBaseSize,
coerceParams,
toCamelCase,
type CoerceParams,
} from './util';

export type ProcedureFn<
S extends UntypedSchemaDef,
Params extends ParamsObj,
Ret extends TypeBuilder<any, any>,
> = (ctx: ProcedureCtx<S>, args: InferTypeOfRow<Params>) => Infer<Ret>;

export interface ProcedureCtx<S extends UntypedSchemaDef> {
readonly sender: Identity;
readonly identity: Identity;
readonly timestamp: Timestamp;
readonly connectionId: ConnectionId | null;
readonly http: HttpClient;
withTx<T>(body: (ctx: TransactionCtx<S>) => T): T;
}

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface TransactionCtx<S extends UntypedSchemaDef>
extends ReducerCtx<S> {}

export function procedure<
S extends UntypedSchemaDef,
Params extends ParamsObj,
Ret extends TypeBuilder<any, any>,
>(name: string, params: Params, ret: Ret, fn: ProcedureFn<S, Params, Ret>) {
const paramsType: ProductType = {
elements: Object.entries(params).map(([n, c]) => ({
name: n,
algebraicType: registerTypesRecursively(
'typeBuilder' in c ? c.typeBuilder : c
).algebraicType,
})),
};
const returnType = registerTypesRecursively(ret).algebraicType;

MODULE_DEF.miscExports.push({
tag: 'Procedure',
value: {
name,
params: paramsType,
returnType,
},
});

PROCEDURES.push({
fn,
paramsType,
returnType,
returnTypeBaseSize: bsatnBaseSize(MODULE_DEF.typespace, returnType),
});
}

export const PROCEDURES: Array<{
fn: ProcedureFn<any, any, any>;
paramsType: ProductType;
returnType: AlgebraicType;
returnTypeBaseSize: number;
}> = [];

export type UntypedProcedureDef = {
name: string;
accessorName: string;
params: CoerceParams<ParamsObj>;
returnType: TypeBuilder<any, any>;
};

export type UntypedProceduresDef = {
procedures: readonly UntypedProcedureDef[];
};

export function procedures<const H extends readonly UntypedProcedureDef[]>(
...handles: H
): { procedures: H };

export function procedures<const H extends readonly UntypedProcedureDef[]>(
handles: H
): { procedures: H };

export function procedures<const H extends readonly UntypedProcedureDef[]>(
...args: [H] | H
): { procedures: H } {
const procedures = (
args.length === 1 && Array.isArray(args[0]) ? args[0] : args
) as H;
return { procedures };
}

type ProcedureDef<
Name extends string,
Params extends ParamsObj,
ReturnType extends TypeBuilder<any, any>,
> = {
name: Name;
accessorName: CamelCase<Name>;
params: CoerceParams<Params>;
returnType: ReturnType;
};

export function procedureSchema<
ProcedureName extends string,
Params extends ParamsObj,
ReturnType extends TypeBuilder<any, any>,
>(
name: ProcedureName,
params: Params,
returnType: ReturnType
): ProcedureDef<ProcedureName, Params, ReturnType> {
return {
name,
accessorName: toCamelCase(name),
params: coerceParams(params),
returnType,
};
}
Loading
Loading