clockworklabs/SpacetimeDB · error · TypeError
too many elements
Error message
too many elements
What it means
For btree range scans, `serializeRange` accepts a 'range' array of up to `numColumns` equality prefix elements, optionally ending in a `Range` object for the final column. If the array is longer than the number of indexed columns it throws `too many elements` before serializing. Note the asymmetry: too FEW elements is legal (it becomes a prefix scan); too MANY never is.
Solutions
- Trim the array to at most the index's column count: for an index on `[a, b]`, valid scans are `[a]`, `[a, b]`, `[a, Range...]`, `[Range...]` — never `[a, b, c]`.
- In generic query builders, clamp or reject filter arrays longer than the index width before calling the scan.
- If you need to constrain more columns than the index covers, scan the index with a prefix and filter the remaining columns on the returned rows.
- Write the arity rule into the type of your helper (tuple of length <= index columns) so TypeScript catches it.
Example fix
// before — index on (tenantId, createdAt)
for (const row of byTenantCreated.iter([tenantId, from, to])) {} // 3 elems > 2 cols
// after — equality prefix + Range terminal on last column
import { Range } from '...' ;
for (const row of byTenantCreated.iter([tenantId, Range.from(from).to(to)])) {} Defensive patterns
Strategy: validation
Validate before calling
// Clamp/reject bound arrays wider than the index before scanning.
function checkRangeArity(range: unknown[], indexCols: number) {
if (range.length > indexCols) {
throw new Error(`range has ${range.length} elements but index has ${indexCols} columns`);
}
} Type guard
type RangePrefix<N extends number> = N extends 1 ? [unknown] : N extends 2 ? [unknown] | [unknown, unknown] | [unknown, Range<unknown>] | [Range<unknown>] : unknown[];
Try / catch
try { iterate(range); } catch (e) { if (e instanceof TypeError && e.message === 'too many elements') { /* trim trailing equality elems or move filters into row predicates */ } else throw e; } Prevention
- Remember the rule: at most index-column-count elements, last may be a Range, shorter is a prefix scan.
- Put remaining predicates on returned rows instead of overloading the bound array.
- Encode the index width in the type of helper functions that build scan arguments.
When it happens
Trigger: Calling a range scan on an index with N columns but passing N+1 or more elements/bounds, e.g. `idx.iter([a, b, c], ...)` on a two-column index; appending a `Range` bound plus equality values that exceed the column count; using the same bound array against indexes of different widths.
Common situations: Copy-pasting a scan from a wider index to a narrower one; adding equality prefixes in front of a `Range` without recounting; generic query helpers that build bound arrays dynamically from user filters.
Related errors
- wrong number of elements
- AddIndex: index ` ` not found in new module def
- AddIndex: ` ` not found in new module def
- ERROR: Failed to read from BytesSource
- Index ` ` not found in table
AI-assisted analysis of clockworklabs/SpacetimeDB@eddf9f5014 (2026-08-20).
Data as JSON: /api/errors/4bd266564bc3a627.
Report an issue: GitHub.
Appendix: source
Thrown at crates/bindings-typescript/src/server/runtime.ts:1430
},
} as PointIndex<any, any>;
} else {
// numColumns != 1
const isCompleteScalarKey = (range: any[]): boolean => {
// Preserve the point-scan path only for complete scalar keys.
// A complete key with a Range in the final position is still a range
// scan over that column with equality over the preceding prefix.
return (
range.length === numColumns &&
!(range[range.length - 1] instanceof Range)
);
};
const serializeRange = (
buffer: ResizableBuffer,
range: any[]
): IndexScanArgs => {
if (range.length > numColumns) throw new TypeError('too many elements');
BINARY_WRITER.reset(buffer);
const writer = BINARY_WRITER;
const prefix_elems = range.length - 1;
for (let i = 0; i < prefix_elems; i++) {
indexSerializers[i](writer, range[i]);
}
const rstartOffset = writer.offset;
const term = range[range.length - 1];
const serializeTerm = indexSerializers[range.length - 1];
if (term instanceof Range) {
const writeBound = (bound: Bound<any>) => {
const tags = { included: 0, excluded: 1, unbounded: 2 };
writer.writeU8(tags[bound.tag]);
if (bound.tag !== 'unbounded') serializeTerm(writer, bound.value);
};
writeBound(term.from);
const rstartLen = writer.offset - rstartOffset;View on GitHub (pinned to eddf9f5014)