Skip to content

@adrianhall/cloudflare-toolkit


@adrianhall/cloudflare-toolkit / index / sqlCount

Function: sqlCount()

sqlCount(row, countProperty?): number

Defined in: src/lib/guards/guards.ts:55

Extract a numeric count from a D1 .first() result for the SELECT COUNT(*) AS count FROM t pattern. Validates that row is a non-null object with a numeric countProperty; throws NullError (via throwIfNull) if row itself is null/undefined, or InvalidShapeError if row is non-null but does not have the expected shape (not an object, or countProperty on it missing/not a number) — since the whole point of this guard is "this should never happen — if it does, that's a bug, not a 0".

Parameters

row

unknown

The value returned by D1's .first()null when no rows match, otherwise whatever shape the query produced. Typed unknown because a D1 result is never trustworthy input.

countProperty?

string = "count"

The property on row holding the count. Defaults to "count".

Returns

number

The numeric count read from row[countProperty].

Throws

If row is null or undefined.

Throws

If row is non-null but not an object, or countProperty on it is missing or not a number.