diff --git a/crates/bindings-typescript/src/lib/autogen/function_visibility_type.ts b/crates/bindings-typescript/src/lib/autogen/function_visibility_type.ts new file mode 100644 index 00000000000..0b94830a87d --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/function_visibility_type.ts @@ -0,0 +1,19 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; + +// The tagged union or sum type for the algebraic type `FunctionVisibility`. +const FunctionVisibility = __t.enum('FunctionVisibility', { + Internal: __t.unit(), + ClientCallable: __t.unit(), +}); + +export default FunctionVisibility; diff --git a/crates/bindings-typescript/src/lib/autogen/raw_column_default_value_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_column_default_value_v_10_type.ts new file mode 100644 index 00000000000..5f37afa4604 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_column_default_value_v_10_type.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; + +export default __t.object('RawColumnDefaultValueV10', { + colId: __t.u16(), + value: __t.byteArray(), +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_constraint_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_constraint_def_v_10_type.ts new file mode 100644 index 00000000000..76145329659 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_constraint_def_v_10_type.ts @@ -0,0 +1,19 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import RawConstraintDataV9 from './raw_constraint_data_v_9_type'; + +export default __t.object('RawConstraintDefV10', { + sourceName: __t.option(__t.string()), + get data() { + return RawConstraintDataV9; + }, +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_index_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_index_def_v_10_type.ts new file mode 100644 index 00000000000..2a1913b7ae4 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_index_def_v_10_type.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import RawIndexAlgorithm from './raw_index_algorithm_type'; + +export default __t.object('RawIndexDefV10', { + sourceName: __t.option(__t.string()), + accessorName: __t.option(__t.string()), + get algorithm() { + return RawIndexAlgorithm; + }, +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_life_cycle_reducer_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_life_cycle_reducer_def_v_10_type.ts new file mode 100644 index 00000000000..7ccc26d79a3 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_life_cycle_reducer_def_v_10_type.ts @@ -0,0 +1,19 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import Lifecycle from './lifecycle_type'; + +export default __t.object('RawLifeCycleReducerDefV10', { + get lifecycleSpec() { + return Lifecycle; + }, + functionName: __t.string(), +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_module_def_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_module_def_type.ts index e0f91b646ff..40255921356 100644 --- a/crates/bindings-typescript/src/lib/autogen/raw_module_def_type.ts +++ b/crates/bindings-typescript/src/lib/autogen/raw_module_def_type.ts @@ -11,6 +11,7 @@ import { } from '../../lib/type_builders'; import RawModuleDefV8 from './raw_module_def_v_8_type'; import RawModuleDefV9 from './raw_module_def_v_9_type'; +import RawModuleDefV10 from './raw_module_def_v_10_type'; // The tagged union or sum type for the algebraic type `RawModuleDef`. const RawModuleDef = __t.enum('RawModuleDef', { @@ -20,6 +21,9 @@ const RawModuleDef = __t.enum('RawModuleDef', { get V9() { return RawModuleDefV9; }, + get V10() { + return RawModuleDefV10; + }, }); export default RawModuleDef; diff --git a/crates/bindings-typescript/src/lib/autogen/raw_module_def_v_10_section_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_module_def_v_10_section_type.ts new file mode 100644 index 00000000000..dcf579b9fd6 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_module_def_v_10_section_type.ts @@ -0,0 +1,53 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import Typespace from './typespace_type'; +import RawRowLevelSecurityDefV9 from './raw_row_level_security_def_v_9_type'; +import RawTypeDefV10 from './raw_type_def_v_10_type'; +import RawTableDefV10 from './raw_table_def_v_10_type'; +import RawReducerDefV10 from './raw_reducer_def_v_10_type'; +import RawProcedureDefV10 from './raw_procedure_def_v_10_type'; +import RawViewDefV10 from './raw_view_def_v_10_type'; +import RawScheduleDefV10 from './raw_schedule_def_v_10_type'; +import RawLifeCycleReducerDefV10 from './raw_life_cycle_reducer_def_v_10_type'; + +// The tagged union or sum type for the algebraic type `RawModuleDefV10Section`. +const RawModuleDefV10Section = __t.enum('RawModuleDefV10Section', { + get Typespace() { + return Typespace; + }, + get Types() { + return __t.array(RawTypeDefV10); + }, + get Tables() { + return __t.array(RawTableDefV10); + }, + get Reducers() { + return __t.array(RawReducerDefV10); + }, + get Procedures() { + return __t.array(RawProcedureDefV10); + }, + get Views() { + return __t.array(RawViewDefV10); + }, + get Schedules() { + return __t.array(RawScheduleDefV10); + }, + get LifeCycleReducers() { + return __t.array(RawLifeCycleReducerDefV10); + }, + get RowLevelSecurity() { + return __t.array(RawRowLevelSecurityDefV9); + }, +}); + +export default RawModuleDefV10Section; diff --git a/crates/bindings-typescript/src/lib/autogen/raw_module_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_module_def_v_10_type.ts new file mode 100644 index 00000000000..f12b1e66523 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_module_def_v_10_type.ts @@ -0,0 +1,18 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import RawModuleDefV10Section from './raw_module_def_v_10_section_type'; + +export default __t.object('RawModuleDefV10', { + get sections() { + return __t.array(RawModuleDefV10Section); + }, +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_procedure_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_procedure_def_v_10_type.ts new file mode 100644 index 00000000000..1c2b312c126 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_procedure_def_v_10_type.ts @@ -0,0 +1,27 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import AlgebraicType from './algebraic_type_type'; +import ProductType from './product_type_type'; +import FunctionVisibility from './function_visibility_type'; + +export default __t.object('RawProcedureDefV10', { + sourceName: __t.string(), + get params() { + return ProductType; + }, + get returnType() { + return AlgebraicType; + }, + get visibility() { + return FunctionVisibility; + }, +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_reducer_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_reducer_def_v_10_type.ts new file mode 100644 index 00000000000..2c40195ebb9 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_reducer_def_v_10_type.ts @@ -0,0 +1,30 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import AlgebraicType from './algebraic_type_type'; +import ProductType from './product_type_type'; +import FunctionVisibility from './function_visibility_type'; + +export default __t.object('RawReducerDefV10', { + sourceName: __t.string(), + get params() { + return ProductType; + }, + get visibility() { + return FunctionVisibility; + }, + get okReturnType() { + return AlgebraicType; + }, + get errReturnType() { + return AlgebraicType; + }, +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_schedule_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_schedule_def_v_10_type.ts new file mode 100644 index 00000000000..7ad8b18bad1 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_schedule_def_v_10_type.ts @@ -0,0 +1,18 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; + +export default __t.object('RawScheduleDefV10', { + sourceName: __t.option(__t.string()), + tableName: __t.string(), + scheduleAtCol: __t.u16(), + functionName: __t.string(), +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_scoped_type_name_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_scoped_type_name_v_10_type.ts new file mode 100644 index 00000000000..91bf0da5915 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_scoped_type_name_v_10_type.ts @@ -0,0 +1,16 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; + +export default __t.object('RawScopedTypeNameV10', { + scope: __t.array(__t.string()), + sourceName: __t.string(), +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_sequence_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_sequence_def_v_10_type.ts new file mode 100644 index 00000000000..e5ebe17d853 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_sequence_def_v_10_type.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; + +export default __t.object('RawSequenceDefV10', { + sourceName: __t.option(__t.string()), + column: __t.u16(), + start: __t.option(__t.i128()), + minValue: __t.option(__t.i128()), + maxValue: __t.option(__t.i128()), + increment: __t.i128(), +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_table_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_table_def_v_10_type.ts new file mode 100644 index 00000000000..47d112d4506 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_table_def_v_10_type.ts @@ -0,0 +1,41 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import TableType from './table_type_type'; +import TableAccess from './table_access_type'; +import RawIndexDefV10 from './raw_index_def_v_10_type'; +import RawConstraintDefV10 from './raw_constraint_def_v_10_type'; +import RawSequenceDefV10 from './raw_sequence_def_v_10_type'; +import RawColumnDefaultValueV10 from './raw_column_default_value_v_10_type'; + +export default __t.object('RawTableDefV10', { + sourceName: __t.string(), + productTypeRef: __t.u32(), + primaryKey: __t.array(__t.u16()), + get indexes() { + return __t.array(RawIndexDefV10); + }, + get constraints() { + return __t.array(RawConstraintDefV10); + }, + get sequences() { + return __t.array(RawSequenceDefV10); + }, + get tableType() { + return TableType; + }, + get tableAccess() { + return TableAccess; + }, + get defaultValues() { + return __t.array(RawColumnDefaultValueV10); + }, +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_type_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_type_def_v_10_type.ts new file mode 100644 index 00000000000..e3800eff3d2 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_type_def_v_10_type.ts @@ -0,0 +1,20 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import RawScopedTypeNameV10 from './raw_scoped_type_name_v_10_type'; + +export default __t.object('RawTypeDefV10', { + get sourceName() { + return RawScopedTypeNameV10; + }, + ty: __t.u32(), + customOrdering: __t.bool(), +}); diff --git a/crates/bindings-typescript/src/lib/autogen/raw_view_def_v_10_type.ts b/crates/bindings-typescript/src/lib/autogen/raw_view_def_v_10_type.ts new file mode 100644 index 00000000000..755076ed315 --- /dev/null +++ b/crates/bindings-typescript/src/lib/autogen/raw_view_def_v_10_type.ts @@ -0,0 +1,26 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from '../../lib/type_builders'; +import AlgebraicType from './algebraic_type_type'; +import ProductType from './product_type_type'; + +export default __t.object('RawViewDefV10', { + sourceName: __t.string(), + index: __t.u32(), + isPublic: __t.bool(), + isAnonymous: __t.bool(), + get params() { + return ProductType; + }, + get returnType() { + return AlgebraicType; + }, +}); diff --git a/crates/bindings-typescript/src/lib/schema.ts b/crates/bindings-typescript/src/lib/schema.ts index 93efd8a40b2..1a4ad86bca5 100644 --- a/crates/bindings-typescript/src/lib/schema.ts +++ b/crates/bindings-typescript/src/lib/schema.ts @@ -5,8 +5,9 @@ import { type AlgebraicTypeType, type AlgebraicTypeVariants, } from './algebraic_type'; -import type RawModuleDefV9 from './autogen/raw_module_def_v_9_type'; -import type RawScopedTypeNameV9 from './autogen/raw_scoped_type_name_v_9_type'; +import type RawModuleDefV10Section from './autogen/raw_module_def_v_10_section_type'; +import type RawModuleDefV10 from './autogen/raw_module_def_v_10_type'; +import type RawScopedTypeNameV10 from './autogen/raw_scoped_type_name_v_10_type'; import type { UntypedIndex } from './indexes'; import type { UntypedTableDef } from './table'; import type { UntypedTableSchema } from './table_schema'; @@ -85,7 +86,7 @@ function tableToSchema( columns: schema.rowType.row, // typed as T[i]['rowType']['row'] under TablesToSchema rowType: schema.rowSpacetimeType, constraints: tableDef.constraints.map(c => ({ - name: c.name, + name: c.sourceName, constraint: 'unique', columns: c.data.value.columns.map(getColName) as [string], })), @@ -115,10 +116,17 @@ type CompoundTypeCache = Map< RefBuilder >; -type ModuleDef = Infer; +export type ModuleDef = { + [S in Infer as Uncapitalize< + S['tag'] + >]: S['value']; +}; + +type Section = Infer; export class ModuleContext { #compoundTypes: CompoundTypeCache = new Map(); + /** * The global module definition that gets populated by calls to `reducer()` and lifecycle hooks. */ @@ -127,14 +135,31 @@ export class ModuleContext { tables: [], reducers: [], types: [], - miscExports: [], rowLevelSecurity: [], + schedules: [], + procedures: [], + views: [], + lifeCycleReducers: [], }; - get moduleDef() { + get moduleDef(): ModuleDef { return this.#moduleDef; } + get RawModuleDefV10(): Infer { + const sections: Section[] = []; + + for (const [key, value] of Object.entries(this.#moduleDef)) { + const tag = key.charAt(0).toUpperCase() + key.slice(1); + sections.push({ + tag, + value, + } as Section); + } + + return { sections }; + } + get typespace() { return this.#moduleDef.typespace; } @@ -256,7 +281,7 @@ export class ModuleContext { } this.#moduleDef.types.push({ - name: splitName(name), + sourceName: splitName(name), ty: r.ref, customOrdering: true, }); @@ -272,7 +297,7 @@ function isUnit(typeBuilder: ProductBuilder): boolean { ); } -export function splitName(name: string): Infer { +export function splitName(name: string): Infer { const scope = name.split('.'); - return { name: scope.pop()!, scope }; + return { sourceName: scope.pop()!, scope }; } diff --git a/crates/bindings-typescript/src/lib/table.ts b/crates/bindings-typescript/src/lib/table.ts index 3e9d735d3fb..62d1d412499 100644 --- a/crates/bindings-typescript/src/lib/table.ts +++ b/crates/bindings-typescript/src/lib/table.ts @@ -1,9 +1,11 @@ import type { errors } from '../server/errors'; -import type RawConstraintDefV9 from './autogen/raw_constraint_def_v_9_type'; +import type RawConstraintDefV10 from './autogen/raw_constraint_def_v_10_type'; import RawIndexAlgorithm from './autogen/raw_index_algorithm_type'; -import type RawIndexDefV9 from './autogen/raw_index_def_v_9_type'; -import type RawSequenceDefV9 from './autogen/raw_sequence_def_v_9_type'; -import type RawTableDefV9 from './autogen/raw_table_def_v_9_type'; +import type RawIndexDefV10 from './autogen/raw_index_def_v_10_type'; +import type RawSequenceDefV10 from './autogen/raw_sequence_def_v_10_type'; +import type RawTableDefV10 from './autogen/raw_table_def_v_10_type'; +import type RawScheduleDefV10 from './autogen/raw_schedule_def_v_10_type'; +import type RawColumnDefaultValueV10 from './autogen/raw_column_default_value_v_10_type'; import type { AllUnique, ConstraintOpts } from './constraints'; import type { ColumnIndex, @@ -308,11 +310,12 @@ export function table>( // gather primary keys, per‑column indexes, uniques, sequences const pk: ColList = []; - const indexes: Infer[] = []; - const constraints: Infer[] = []; - const sequences: Infer[] = []; + const indexes: Infer[] = []; + const constraints: Infer[] = []; + const sequences: Infer[] = []; let scheduleAtCol: ColId | undefined; + const defaultValues: Infer[] = []; for (const [name, builder] of Object.entries(row.row)) { const meta: ColumnMetadata = builder.columnMetadata; @@ -337,7 +340,7 @@ export function table>( break; } indexes.push({ - name: undefined, // Unnamed indexes will be assigned a globally unique name + sourceName: undefined, // Unnamed indexes will be assigned a globally unique name accessorName: name, // The name of this column will be used as the accessor name algorithm, }); @@ -345,14 +348,14 @@ export function table>( if (isUnique) { constraints.push({ - name: undefined, + sourceName: undefined, data: { tag: 'Unique', value: { columns: [colIds.get(name)!] } }, }); } if (meta.isAutoIncrement) { sequences.push({ - name: undefined, + sourceName: undefined, start: undefined, minValue: undefined, maxValue: undefined, @@ -361,6 +364,13 @@ export function table>( }); } + if (meta.defaultValue) { + defaultValues.push({ + colId: colIds.get(name)!, + value: meta.defaultValue, + }); + } + // If this column is shaped like ScheduleAtAlgebraicType, mark it as the schedule‑at column if (scheduled) { const algebraicType = builder.typeBuilder.algebraicType; @@ -391,17 +401,21 @@ export function table>( // no actual way for the user to set the actual index name. // I think we should standardize: name and accessorName as the way to set // the name and accessor name of an index across all SDKs. - indexes.push({ name: undefined, accessorName: indexOpts.name, algorithm }); + indexes.push({ + sourceName: undefined, + accessorName: indexOpts.name, + algorithm, + }); } // add explicit constraints from options.constraints for (const constraintOpts of opts.constraints ?? []) { if (constraintOpts.constraint === 'unique') { - const data: Infer['data'] = { + const data: Infer['data'] = { tag: 'Unique', value: { columns: constraintOpts.columns.map(c => colIds.get(c)!) }, }; - constraints.push({ name: constraintOpts.name, data }); + constraints.push({ sourceName: constraintOpts.name, data }); continue; } } @@ -412,35 +426,38 @@ export function table>( ? [index.algorithm.value] : index.algorithm.value; const colS = cols.map(i => colNameList[i]).join('_'); - index.name = `${name}_${colS}_idx_${index.algorithm.tag.toLowerCase()}`; + index.sourceName = `${name}_${colS}_idx_${index.algorithm.tag.toLowerCase()}`; } // Temporarily set the type ref to 0. We will set this later // in the schema function. - const tableDef = (ctx: ModuleContext): Infer => ({ - name, + const tableDef = (ctx: ModuleContext): Infer => ({ + sourceName: name, productTypeRef: ctx.registerTypesRecursively(row).ref, primaryKey: pk, indexes, constraints, sequences, - schedule: - scheduled && scheduleAtCol !== undefined - ? { - name: undefined, - reducerName: scheduled, - scheduledAtColumn: scheduleAtCol, - } - : undefined, tableType: { tag: 'User' }, tableAccess: { tag: isPublic ? 'Public' : 'Private' }, + defaultValues, }); const productType = row.algebraicType.value as RowBuilder< CoerceRow >['algebraicType']['value']; + const schedule: Infer | undefined = + scheduled && scheduleAtCol !== undefined + ? { + sourceName: undefined, + tableName: name, + functionName: scheduled, + scheduleAtCol: scheduleAtCol, + } + : undefined; + return { rowType: row as RowBuilder>, tableName: name, @@ -448,5 +465,6 @@ export function table>( tableDef, idxs: {} as OptsIndices, constraints: constraints as OptsConstraints, + schedule, }; } diff --git a/crates/bindings-typescript/src/lib/table_schema.ts b/crates/bindings-typescript/src/lib/table_schema.ts index b48b33ca444..2c4c9ca9b8e 100644 --- a/crates/bindings-typescript/src/lib/table_schema.ts +++ b/crates/bindings-typescript/src/lib/table_schema.ts @@ -1,5 +1,6 @@ import type { ProductType } from './algebraic_type'; -import type RawTableDefV9 from './autogen/raw_table_def_v_9_type'; +import type RawTableDefV10 from './autogen/raw_table_def_v_10_type'; +import type RawScheduleDefV10 from './autogen/raw_schedule_def_v_10_type'; import type { IndexOpts } from './indexes'; import type { ModuleContext } from './schema'; import type { ColumnBuilder, Infer, RowBuilder } from './type_builders'; @@ -28,9 +29,9 @@ export type TableSchema< readonly rowSpacetimeType: RowBuilder['algebraicType']['value']; /** - * The {@link RawTableDefV9} of the configured table + * The {@link RawTableDefV10} of the configured table */ - tableDef(ctx: ModuleContext): Infer; + tableDef(ctx: ModuleContext): Infer; /** * The indexes defined on the table. @@ -45,6 +46,11 @@ export type TableSchema< constraint: 'unique'; columns: [any]; }[]; + + /** + * The schedule defined on the table, if any. + */ + readonly schedule?: Infer; }; export type UntypedTableSchema = TableSchema< diff --git a/crates/bindings-typescript/src/server/procedures.ts b/crates/bindings-typescript/src/server/procedures.ts index f0a2f7d8cab..f6ffc5b008f 100644 --- a/crates/bindings-typescript/src/server/procedures.ts +++ b/crates/bindings-typescript/src/server/procedures.ts @@ -4,6 +4,7 @@ import { type Deserializer, type Serializer, } from '../lib/algebraic_type'; +import FunctionVisibility from '../lib/autogen/function_visibility_type'; import BinaryReader from '../lib/binary_reader'; import BinaryWriter from '../lib/binary_writer'; import type { ConnectionId } from '../lib/connection_id'; @@ -67,13 +68,11 @@ export function procedure< }; const returnType = ctx.registerTypesRecursively(ret).algebraicType; - ctx.moduleDef.miscExports.push({ - tag: 'Procedure', - value: { - name, - params: paramsType, - returnType, - }, + ctx.moduleDef.procedures.push({ + sourceName: name, + params: paramsType, + returnType, + visibility: FunctionVisibility.ClientCallable, }); const { typespace } = ctx; diff --git a/crates/bindings-typescript/src/server/reducers.ts b/crates/bindings-typescript/src/server/reducers.ts index f149e3e002d..9b396accdaf 100644 --- a/crates/bindings-typescript/src/server/reducers.ts +++ b/crates/bindings-typescript/src/server/reducers.ts @@ -1,5 +1,6 @@ +import { AlgebraicType } from '../lib/algebraic_type'; +import FunctionVisibility from '../lib/autogen/function_visibility_type'; import Lifecycle from '../lib/autogen/lifecycle_type'; -import type RawReducerDefV9 from '../lib/autogen/raw_reducer_def_v_9_type'; import type { ParamsAsObject, ParamsObj, @@ -30,7 +31,7 @@ export function pushReducer( name: string, params: RowObj | RowBuilder, fn: Reducer, - lifecycle?: Infer['lifecycle'] + lifecycle?: Infer ): void { ctx.defineFunction(name); @@ -44,13 +45,25 @@ export function pushReducer( const ref = ctx.registerTypesRecursively(params); const paramsType = ctx.resolveType(ref).value; + const isLifecycle = lifecycle != null; ctx.moduleDef.reducers.push({ - name, + sourceName: name, params: paramsType, - lifecycle, // <- lifecycle flag lands here + //ModuleDef validation code is responsible to mark private reducers + visibility: FunctionVisibility.ClientCallable, + //Hardcoded for now - reducers do not return values yet + okReturnType: AlgebraicType.Product({ elements: [] }), + errReturnType: AlgebraicType.String, }); + if (isLifecycle) { + ctx.moduleDef.lifeCycleReducers.push({ + lifecycleSpec: lifecycle, + functionName: name, + }); + } + // If the function isn't named (e.g. `function foobar() {}`), give it the same // name as the reducer so that it's clear what it is in in backtraces. if (!fn.name) { diff --git a/crates/bindings-typescript/src/server/runtime.ts b/crates/bindings-typescript/src/server/runtime.ts index 4b1ae375cf5..b9a8e949507 100644 --- a/crates/bindings-typescript/src/server/runtime.ts +++ b/crates/bindings-typescript/src/server/runtime.ts @@ -7,8 +7,7 @@ import { type Deserializer, } from '../lib/algebraic_type'; import RawModuleDef from '../lib/autogen/raw_module_def_type'; -import type RawModuleDefV9 from '../lib/autogen/raw_module_def_v_9_type'; -import type RawTableDefV9 from '../lib/autogen/raw_table_def_v_9_type'; +import type RawTableDefV10 from '../lib/autogen/raw_table_def_v_10_type'; import type Typespace from '../lib/autogen/typespace_type'; import { ConnectionId } from '../lib/connection_id'; import { Identity } from '../lib/identity'; @@ -29,7 +28,7 @@ import { type JwtClaims, type ReducerCtx as IReducerCtx, } from '../lib/reducers'; -import { type UntypedSchemaDef } from '../lib/schema'; +import { type ModuleDef, type UntypedSchemaDef } from '../lib/schema'; import { type RowType, type Table, type TableMethods } from '../lib/table'; import type { Infer } from '../lib/type_builders'; import { hasOwn, toCamelCase } from '../lib/util'; @@ -275,7 +274,7 @@ export const hooks: ModuleHooks = { const writer = new BinaryWriter(128); RawModuleDef.serialize( writer, - RawModuleDef.V9(getRegisteredSchema().moduleDef) + RawModuleDef.V10(getRegisteredSchema().RawModuleDefV10) ); return writer.getBuffer(); }, @@ -381,11 +380,11 @@ function getDbView() { return DB_VIEW; } -function makeDbView(moduleDef: Infer): DbView { +function makeDbView(moduleDef: ModuleDef): DbView { return freeze( Object.fromEntries( moduleDef.tables.map(table => [ - toCamelCase(table.name), + toCamelCase(table.sourceName), makeTableView(moduleDef.typespace, table), ]) ) @@ -397,9 +396,9 @@ const BINARY_READER = new BinaryReader(new Uint8Array()); function makeTableView( typespace: Infer, - table: Infer + table: Infer ): Table { - const table_id = sys.table_id_from_name(table.name); + const table_id = sys.table_id_from_name(table.sourceName); const rowType = typespace.types[table.productTypeRef]; if (rowType.tag !== 'Product') { throw 'impossible'; @@ -493,7 +492,7 @@ function makeTableView( ) as Table; for (const indexDef of table.indexes) { - const index_id = sys.index_id_from_name(indexDef.name!); + const index_id = sys.index_id_from_name(indexDef.sourceName!); let column_ids: number[]; switch (indexDef.algorithm.tag) { diff --git a/crates/bindings-typescript/src/server/schema.ts b/crates/bindings-typescript/src/server/schema.ts index 8a69b055dcb..ca60f17b1cf 100644 --- a/crates/bindings-typescript/src/server/schema.ts +++ b/crates/bindings-typescript/src/server/schema.ts @@ -410,6 +410,8 @@ export function schema( const ctx = new SchemaInner(ctx => { const tableDefs = handles.map(h => h.tableDef(ctx)); ctx.moduleDef.tables.push(...tableDefs); + const schedules = handles.map(h => h.schedule).filter(s => s !== undefined); + ctx.moduleDef.schedules.push(...schedules); return tablesToSchema(ctx, handles); }); diff --git a/crates/bindings-typescript/src/server/views.ts b/crates/bindings-typescript/src/server/views.ts index dcd56d4e628..5535cfcb07c 100644 --- a/crates/bindings-typescript/src/server/views.ts +++ b/crates/bindings-typescript/src/server/views.ts @@ -106,16 +106,13 @@ export function defineView< ctx.registerTypesRecursively(paramsBuilder) ); - ctx.moduleDef.miscExports.push({ - tag: 'View', - value: { - name: opts.name, - index: (anon ? ctx.anonViews : ctx.views).length, - isPublic: opts.public, - isAnonymous: anon, - params: paramType, - returnType, - }, + ctx.moduleDef.views.push({ + sourceName: opts.name, + index: (anon ? ctx.anonViews : ctx.views).length, + isPublic: opts.public, + isAnonymous: anon, + params: paramType, + returnType, }); // If it is an option, we wrap the function to make the return look like an array. diff --git a/crates/lib/src/db/raw_def/v10.rs b/crates/lib/src/db/raw_def/v10.rs index 7474b6bedfe..102cf3916ae 100644 --- a/crates/lib/src/db/raw_def/v10.rs +++ b/crates/lib/src/db/raw_def/v10.rs @@ -912,43 +912,7 @@ impl RawModuleDefV10Builder { /// Finish building, consuming the builder and returning the module. /// The module should be validated before use. - /// - /// This method automatically marks functions used in lifecycle or schedule functions - /// as `Internal` visibility. - pub fn finish(mut self) -> RawModuleDefV10 { - let internal_functions = self - .module - .lifecycle_reducers() - .cloned() - .into_iter() - .flatten() - .map(|lcr| lcr.function_name.clone()) - .chain( - self.module - .schedules() - .cloned() - .into_iter() - .flatten() - .map(|sched| sched.function_name.clone()), - ); - - for internal_function in internal_functions { - if let Some(r) = self - .reducers_mut() - .iter_mut() - .find(|r| r.source_name == internal_function) - { - r.visibility = FunctionVisibility::Private; - } - - if let Some(p) = self - .procedures_mut() - .iter_mut() - .find(|p| p.source_name == internal_function) - { - p.visibility = FunctionVisibility::Private; - } - } + pub fn finish(self) -> RawModuleDefV10 { self.module } } diff --git a/crates/schema/src/def/validate/v10.rs b/crates/schema/src/def/validate/v10.rs index 1667bd63b1f..b4897b8b582 100644 --- a/crates/schema/src/def/validate/v10.rs +++ b/crates/schema/src/def/validate/v10.rs @@ -156,8 +156,8 @@ pub fn validate(def: RawModuleDefV10) -> Result { .combine_errors() .and_then( |(mut tables, types, reducers, procedures, views, schedules, lifecycles)| { - let (mut reducers, procedures, views) = check_function_names_are_unique(reducers, procedures, views)?; - + let (mut reducers, mut procedures, views) = + check_function_names_are_unique(reducers, procedures, views)?; // Attach lifecycles to their respective reducers attach_lifecycles_to_reducers(&mut reducers, lifecycles)?; @@ -165,11 +165,11 @@ pub fn validate(def: RawModuleDefV10) -> Result { attach_schedules_to_tables(&mut tables, schedules)?; check_scheduled_functions_exist(&mut tables, &reducers, &procedures)?; + change_scheduled_functions_and_lifetimes_visibility(&tables, &mut reducers, &mut procedures)?; Ok((tables, types, reducers, procedures, views)) }, ); - let CoreValidator { stored_in_table_def, typespace_for_generate, @@ -205,6 +205,50 @@ pub fn validate(def: RawModuleDefV10) -> Result { }) } +/// Change the visibility of scheduled functions and lifecycle reducers to Internal. +/// +fn change_scheduled_functions_and_lifetimes_visibility( + tables: &HashMap, + reducers: &mut IndexMap, + procedures: &mut IndexMap, +) -> Result<()> { + for sched_def in tables.iter().filter_map(|(_, t)| t.schedule.as_ref()) { + match sched_def.function_kind { + FunctionKind::Reducer => { + let def = reducers.get_mut(&sched_def.function_name).ok_or_else(|| { + ValidationError::MissingScheduledFunction { + schedule: sched_def.name.clone(), + function: sched_def.function_name.clone(), + } + })?; + + def.visibility = crate::def::FunctionVisibility::Private; + } + + FunctionKind::Procedure => { + let def = procedures.get_mut(&sched_def.function_name).ok_or_else(|| { + ValidationError::MissingScheduledFunction { + schedule: sched_def.name.clone(), + function: sched_def.function_name.clone(), + } + })?; + + def.visibility = crate::def::FunctionVisibility::Private; + } + + FunctionKind::Unknown => {} + } + } + + for red_def in reducers.iter_mut().map(|(_, r)| r) { + if red_def.lifecycle.is_some() { + red_def.visibility = crate::def::FunctionVisibility::Private; + } + } + + Ok(()) +} + struct ModuleValidatorV10<'a> { core: CoreValidator<'a>, }