interface IDataSource
Since 0.2.0

The data-access seam a backend provides per entity.

One instance is bound to one entity (table/model/collection). The data source owns query evaluation end to end: it applies where, orderBy, offset/limit and select itself, and the repository above it must not re-apply any of them.

Transaction-scoped instances may defer their writes. A backend whose store has no interactive transaction (Cloudflare D1, where a pre-declared batch() is the only unit of atomicity) has to buffer writes and apply them at commit. On such a backend the write methods below describe what WILL be written rather than what already is, and reads observe committed state only. A backend that defers must document it; D1Adapter is the worked example.

Methods

findAll(query: NormalizedQuery): Promise<Record<string, unknown>[]>

Find every entity matching the normalized query.

findById(id: EntityKey): Promise<Record<string, unknown> | null>

Find a single entity by its primary key value.

The key is an EntityKey: a scalar string/number or a composite record. A composite record is refused by the backend when it maps to a single column (e.g. Prisma's where: { id }), so widening this parameter is source-compatible for callers but breaking for an out-of-repo implementor whose method still declares the scalar-only form.

create(data: Partial<Record<string, unknown>>): Promise<Record<string, unknown>>

Insert a new entity.

update(
data: Partial<Record<string, unknown>>
): Promise<Record<string, unknown>>

Update an existing entity by primary key.

delete(id: EntityKey): Promise<boolean>

Delete an entity by primary key.

optional
findPage(query: NormalizedQuery): Promise<PageResult>

Find a page of entities by cursor pagination.

Optional. The member exists on every in-repo adapter but is declared optional so an out-of-repo implementor that cannot page by cursor keeps compiling. When it is absent, the repository above refuses cursor paging by name (UnsupportedQueryFeatureError): absence means "cannot page by cursor", never "there are no more rows" — the distinction an absent member cannot otherwise preserve.

count(
where: Record<string, unknown>,
): Promise<number>

Count entities matching a filter.

Usage

import { type IDataSource } from "common/src/index.ts";