Heuristic:AFK surf Open agent Datasource Adapter Over Direct Mutation
| Knowledge Sources | |
|---|---|
| Domains | Reactivity, Data_Model |
| Last Updated | 2026-10-25 00:00 GMT |
Overview
Always mutate database cells/properties through the DataSource adapter (`cellValueChange`, `propertyAdd`, ...) rather than poking the `DatabaseBlockModel` directly, so signals and view layers stay reactive.
Description
BlockSuite's data-view layer wraps the underlying CRDT block model in a reactive `DataSource` whose methods (`cellValueChange`, `propertyAdd`, `propertyTypeSet`, `rowAdd`, ...) are the only sanctioned write path. The adapter emits the Preact signals (`cellValueGet$`, `properties$`, `rows$`) that the table/kanban views subscribe to. Writing the model directly bypasses those signals, so the UI silently desyncs from the data.
Usage
Apply this invariant whenever you write cell values or property definitions in the database block. Use `datasource.cellValueChange(rowId, propertyId, value)` and `datasource.propertyAdd(...)` — never `model.props.cells[...] = value`.
The Insight (Rule of Thumb)
- Action: Read/write through the `DataSource` instance (`DatabaseBlockDataSource` for the database block), not the raw `DatabaseBlockModel`.
- Value: Use `cellValueChange(rowId, propertyId, value)` for writes; `cellValueGet$` / `cellValueGet` for reads.
- Trade-off: Slightly more indirection than direct model access — but you keep reactivity and view consistency.
- Compatibility: The `DataSource` interface is abstract; `DatabaseBlockDataSource` is the concrete implementation for the database block.
Reasoning
The view layers (table, kanban) subscribe to `DataSource` signals derived from the underlying Yjs doc. Direct model mutations update the CRDT but do not fire the adapter's computed signals, so views render stale data until a full re-render. The adapter is the single funnel that both writes the CRDT and updates the signals.
Code Evidence
The adapter write surface from `blocksuite/affine/data-view/src/core/data-source/base.ts:28,140-150`:
cellValueChange(rowId: string, propertyId: string, value: unknown): void;
...
abstract cellValueChange(
rowId: string,
propertyId: string,
value: unknown
): void;
Correct usage in the database preset from `blocksuite/playground/apps/starter/data/database.ts:45-48,86-92`:
const datasource = new DatabaseBlockDataSource(database);
...
const richTextId = datasource.propertyAdd('end', {
type: databaseBlockProperties.richTextColumnConfig.type,
name: 'Rich Text',
});
...
datasource.cellValueChange(
id,
richTextId,
new Text(`Paragraph type ${type}`)
);