Skip to content
72 changes: 39 additions & 33 deletions packages/3-extensions/sql-orm-client/src/collection-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import { paramRefForStorageColumn } from './where-binding';

type CodecExecutionContext = CollectionContext<Contract<SqlStorage>>['context'];

interface DescribeCollectionRowsOptions {
export interface DescribeCollectionRowsOptions {
context: CodecExecutionContext;
state: CollectionState;
modelName: string;
Expand All @@ -90,41 +90,47 @@ export function describeCollectionRows<Row>(
function describeExecutionRows<Row>(
options: DescribeCollectionRowsOptions,
): Preparable<Record<string, unknown>, AsyncIterableResult<Row>> {
const { context, state, modelName, namespaceId } = options;
const { context, state, modelName } = options;
const { contract } = context;
const polyInfo = resolvePolymorphismInfo(contract, namespaceId, modelName);
const plan =
state.includes.length === 0
? compileSelect(contract, modelName, state)
: compileSelectWithIncludes(contract, context.aggregateDescriptors, modelName, state);
return { plan, consume: (rows) => consumeCollectionRows<Row>(options, rows) };
}

if (state.includes.length === 0) {
const compiled = compileSelect(contract, modelName, state);
const mapper = polyInfo
? (rawRow: Record<string, unknown>) =>
blindCast<
Row,
'collection row generic is supplied by the caller and matched to the selected model shape'
>(
mapPolymorphicRow(
contract,
namespaceId,
modelName,
polyInfo,
state.tables,
rawRow,
state.variantValue,
),
)
: (rawRow: Record<string, unknown>) =>
blindCast<
Row,
'collection row generic is supplied by the caller and matched to the selected model shape'
>(mapStorageRowToModelFields(contract, namespaceId, modelName, rawRow));
return { plan: compiled, consume: (rows) => mapResultRows(rows, mapper) };
export function consumeCollectionRows<Row>(
options: DescribeCollectionRowsOptions,
rows: AsyncIterableResult<Record<string, unknown>>,
): AsyncIterableResult<Row> {
const { context, state, modelName, namespaceId } = options;
const { contract } = context;
if (state.includes.length > 0) {
return consumeIncludeRows<Row>(context, state, namespaceId, modelName, rows);
}

const plan = compileSelectWithIncludes(contract, context.aggregateDescriptors, modelName, state);
return {
plan,
consume: (rows) => consumeIncludeRows<Row>(context, state, namespaceId, modelName, rows),
};
const polyInfo = resolvePolymorphismInfo(contract, namespaceId, modelName);
const mapper = polyInfo
? (rawRow: Record<string, unknown>) =>
blindCast<
Row,
'collection row generic is supplied by the caller and matched to the selected model shape'
>(
mapPolymorphicRow(
contract,
namespaceId,
modelName,
polyInfo,
state.tables,
rawRow,
state.variantValue,
),
)
: (rawRow: Record<string, unknown>) =>
blindCast<
Row,
'collection row generic is supplied by the caller and matched to the selected model shape'
>(mapStorageRowToModelFields(contract, namespaceId, modelName, rawRow));
return mapResultRows(rows, mapper);
}

function deferResolution<T>(resolve: () => T): () => T {
Expand Down
Loading
Loading