Skip to content

BeechCMS


BeechCMS / @beechcms/core / ContentRepository

Interface: ContentRepository ​

Interface defining the standard operations for content persistence. This is platform-agnostic and should be implemented for specific databases (e.g., D1).

Methods ​

bulkPurge() ​

bulkPurge(seed, ids, options?): Promise<BulkDeleteResult & object>

purge applied per id. Returns the purged rows so the caller can collect R2 media keys.

Parameters ​

seed ​

Seed

ids ​

string[]

options? ​

RepositoryOptions

Returns ​

Promise<BulkDeleteResult & object>


bulkRestore() ​

bulkRestore(seed, ids, options?): Promise<BulkDeleteResult>

restore applied per id. Never partially fails the batch: each id reports its own outcome.

Parameters ​

seed ​

Seed

ids ​

string[]

options? ​

RepositoryOptions

Returns ​

Promise<BulkDeleteResult>


bulkUpdate() ​

bulkUpdate(seed, ids, fields): Promise<{ failed: object[]; updated: number; }>

Apply the same field update to many entries. Returns per-id outcome. Caller is responsible for validation; this method assumes the payload is already shape-checked against the seed.

Parameters ​

seed ​

Seed

ids ​

string[]

fields ​

Record<string, BulkFieldUpdate>

Returns ​

Promise<{ failed: object[]; updated: number; }>


create() ​

create(seed, id, slug, status, data, options?): Promise<void>

Creates a new content entry in the live table. Throws SlugConflictError if the slug is already taken.

Parameters ​

seed ​

Seed

id ​

string

slug ​

string

status ​

string

data ​

Record<string, any>

options? ​

RepositoryOptions

Returns ​

Promise<void>


deleteDraft() ​

deleteDraft(seed, entryId): Promise<void>

Discards the pending draft.

Parameters ​

seed ​

Seed

entryId ​

string

Returns ​

Promise<void>


exists() ​

exists(seed, id, options?): Promise<boolean>

Cheap existence probe by id: no row is loaded or decrypted. Same trashed visibility as ContentRepository.findById.

Parameters ​

seed ​

Seed

id ​

string

options? ​
trashed? ​

TrashedMode

Returns ​

Promise<boolean>


existsSlug() ​

existsSlug(seed, slug, excludeId?): Promise<boolean>

Checks if a slug is already taken by another entry.

Parameters ​

seed ​

Seed

slug ​

string

excludeId? ​

string

Returns ​

Promise<boolean>


findById() ​

findById(seed, id, options?): Promise<Record<string, any>>

Finds a single entry by its unique ID. Trashed rows are invisible unless options.trashed says otherwise. Throws EntryNotFoundError if not found.

Parameters ​

seed ​

Seed

id ​

string

options? ​
trashed? ​

TrashedMode

Returns ​

Promise<Record<string, any>>


findBySlug() ​

findBySlug(seed, slug): Promise<Record<string, any>>

Finds a single entry by its unique slug. Throws EntryNotFoundError if not found.

Parameters ​

seed ​

Seed

slug ​

string

Returns ​

Promise<Record<string, any>>


findExpiredByRetention() ​

findExpiredByRetention(seed, retentionDays, now, limit): Promise<string[]>

Pure query, no side effects: ids of trashed entries whose retention window has elapsed (deleted_at + retentionDays * 86400 <= now), oldest deletion first, at most limit. retentionDays is the EFFECTIVE value (resolveRetentionDays); 0 = never purge → [] without querying. now is unix seconds supplied by the caller's IClock; never read the clock here.

Parameters ​

seed ​

Seed

retentionDays ​

number

now ​

number

limit ​

number

Returns ​

Promise<string[]>


findMany() ​

findMany(seed, options): Promise<{ items: Record<string, any>[]; total: number; }>

Retrieves a paginated and filtered list of entries.

Parameters ​

seed ​

Seed

options ​

SelectOptions

Returns ​

Promise<{ items: Record<string, any>[]; total: number; }>


findParentIdsByRelation() ​

findParentIdsByRelation(seed, branchAlias, targetIds, limit): Promise<string[]>

Resolves the parent entry ids that reference any of targetIds through a MULTI relation branch (junction table). Single-value relations need no lookup — their target id lives in a column on the parent row and is filterable directly.

Returns at most limit distinct parent ids, in no guaranteed order. Callers that must distinguish "complete" from "truncated" pass limit = cap + 1 and compare the length.

Parameters ​

seed ​

Seed

branchAlias ​

string

targetIds ​

string[]

limit ​

number

Returns ​

Promise<string[]>

Throws ​

RepositoryError if branchAlias is not a multiple: true relation branch of seed.


findPendingDrafts() ​

findPendingDrafts(seeds): Promise<DraftSummary[]>

Aggregates pending drafts across every draft-enabled seed in a single round-trip. Exists so the unified /drafts view never issues one query per seed.

Parameters ​

seeds ​

Seed[]

The draft-enabled seeds to scan (caller passes seedRegistry.draftEnabled()).

Returns ​

Promise<DraftSummary[]>

Drafts newest-first; empty array when no seeds or no drafts.


getDraft() ​

getDraft(seed, entryId): Promise<Record<string, any> | null>

Retrieves the pending draft for a given entry. Returns null if no draft exists.

Parameters ​

seed ​

Seed

entryId ​

string

Returns ​

Promise<Record<string, any> | null>


getDraftUpdatedAt() ​

getDraftUpdatedAt(seed, entryId): Promise<number | null>

Retrieves the pending draft row's updated_at, for use as an optimistic-concurrency guard by callers that read the draft before merging into it. Returns null if no draft exists (or drafts are disallowed for the seed).

Parameters ​

seed ​

Seed

entryId ​

string

Returns ​

Promise<number | null>


getFacets() ​

getFacets(seed): Promise<{ statuses: Record<string, number>; tagsByColumn: Record<string, string[]>; }>

Computes facets (status counts and distinct tags) for a content type.

Parameters ​

seed ​

Seed

Returns ​

Promise<{ statuses: Record<string, number>; tagsByColumn: Record<string, string[]>; }>


hasDraft() ​

hasDraft(seed, entryId): Promise<boolean>

Checks if an entry has a pending draft.

Parameters ​

seed ​

Seed

entryId ​

string

Returns ​

Promise<boolean>


mutateField() ​

mutateField(seed, id, fieldName, operation, options?): Promise<{ newValue: number; }>

Atomically increments/decrements a numeric field with optional min/max guards. Bypasses document-level lifecycle hooks — it's a single UPDATE statement, used to prevent race conditions on counters (stock, balances). Throws RepositoryError if fieldName is not a numeric branch of seed, or if the guard conditions fail / the row does not exist.

Parameters ​

seed ​

Seed

id ​

string

fieldName ​

string

operation ​
type ​

"increment" | "decrement"

value ​

number

options? ​
max? ​

number

min? ​

number

Returns ​

Promise<{ newValue: number; }>


publishDraft() ​

publishDraft(seed, entryId): Promise<void>

Atomic promotion of a draft to the live table. Must use a transaction (db.batch) to update live and delete draft.

Parameters ​

seed ​

Seed

entryId ​

string

Returns ​

Promise<void>


purge() ​

purge(seed, id, options?): Promise<PurgeResult>

Irreversible erasure: runs beforeDelete, reads the row, deletes it (junction and _drafts rows follow via ON DELETE CASCADE), appends the ledger event, runs afterDelete. Only erases a trashed row unless PurgeOptions.trashed is 'any'. R2 media deletion is NOT performed here — the caller owns it (VSA: no external I/O in the repository beyond the ledger port).

Parameters ​

seed ​

Seed

id ​

string

options? ​

PurgeOptions

Returns ​

Promise<PurgeResult>

Throws ​

EntryNotFoundError if no row matching the PurgeOptions.trashed mode with id exists.


restore() ​

restore(seed, id, options?): Promise<{ row: Record<string, any>; }>

Clears deleted_at and restores fromTrashedSlug(slug). When that slug was taken by a live entry in the meantime the UNIQUE constraint rejects the update; the implementation catches it and falls back to <original>-restored-<id8> rather than failing the operation (feature brief §4).

Parameters ​

seed ​

Seed

id ​

string

options? ​

RepositoryOptions

Returns ​

Promise<{ row: Record<string, any>; }>

The restored row, carrying the slug it actually ended up with.

Throws ​

EntryNotFoundError if no TRASHED row with id exists.


runBatch() ​

runBatch(operations): Promise<void>

Executes a list of write operations atomically via a single db.batch. Document-level lifecycle hooks do NOT run for operations inside this call.

Parameters ​

operations ​

BatchWrite[]

Returns ​

Promise<void>


saveDraft() ​

saveDraft(seed, entryId, data, options?): Promise<void>

Saves or updates a pending draft in the mirror table. Throws DraftSaveConflictError if options.ifMatch is set and no longer matches the pending draft row's updated_at.

Parameters ​

seed ​

Seed

entryId ​

string

data ​

Record<string, any>

options? ​

RepositoryOptions

Returns ​

Promise<void>


softDelete() ​

softDelete(seed, id, options?): Promise<{ row: Record<string, any>; }>

Reversible delete: stamps deleted_at and returns the row as it was. Stamps deleted_at from the repository's IClock and renames the slug with toTrashedSlug. Runs beforeDelete/afterDelete. Leaves junction rows, _drafts rows and R2 media untouched — a trashed entry must be restorable whole.

Parameters ​

seed ​

Seed

id ​

string

options? ​

RepositoryOptions

Returns ​

Promise<{ row: Record<string, any>; }>

Throws ​

EntryNotFoundError if no live row with id exists.


update() ​

update(seed, id, data, status?, options?): Promise<void>

Partially updates an existing entry in the live table. Throws EntryNotFoundError if the ID does not exist. Throws EntryConflictError if options.ifMatch is set and no longer matches the live row's updated_at.

Parameters ​

seed ​

Seed

id ​

string

data ​

Record<string, any>

status? ​

string

options? ​

RepositoryOptions

Returns ​

Promise<void>


updateWithKanbanPosition() ​

updateWithKanbanPosition(seed, id, patch, position, axisBranchId, ctx): Promise<{ success: boolean; }>

Atomically applies an axis-value patch AND a kanban_positions upsert in one DB batch (KB-S04e). patch is null for same-column reorders (position only).

Parameters ​

seed ​

Seed

id ​

string

patch ​

Record<string, unknown> | null

position ​

string

axisBranchId ​

string

ctx ​
actor ​

string

ifMatch? ​

number

Optimistic concurrency guard for the axis-value patch: the updated_at the caller last read. When set and patch is non-null, the write only applies if the live row's updated_at still matches; otherwise throws EntryConflictError. Ignored when patch is null, since a position-only reorder does not read-modify-write column data.

Returns ​

Promise<{ success: boolean; }>

Throws ​

EntryConflictError if ctx.ifMatch is set, patch is non-null, and the live row's updated_at no longer matches it.