Implementation:AFK surf Open agent DatabaseBlockDataSource
| Knowledge Sources | |
|---|---|
| Domains | Database, Data_View, BlockSuite |
| Last Updated | 2026-10-25 00:00 GMT |
Overview
Concrete DataSource adapter that wraps a BlockSuite affine:database block model and exposes reactive property, row, cell, view-manager, and service-provider operations for the data-view system.
Description
DatabaseBlockDataSource is the canonical API surface for the database block. It implements the abstract DataSource interface (defined by DataSourceBase) and bridges the data-view layer to the underlying yjs block model. Construct it with a DatabaseBlockModel retrieved from the store; the resulting instance exposes a reactive viewManager, propertyAdd, cellValueChange, reactive signals (rows$, properties$, propertyMetas$), and a DI provider. Always go through the data source rather than mutating the block model directly so the view layer stays consistent.
Usage
Instantiate this adapter right after adding an affine:database block. Use it whenever you need to add typed properties, populate cells, add views, configure group-by, or query rows. It is the single integration point between the database block and the rest of the data-view (table/kanban/filter/sort) system.
Code Reference
Source Location
- Repository: Open-Agent
- File: usage blocksuite/playground/apps/starter/data/database.ts (L45); class in @blocksuite/affine/blocks/database; DataSource interface at blocksuite/affine/data-view/src/core/data-source/base.ts (L18-102), DataSourceBase at L106-305
Signature
class DatabaseBlockDataSource extends DataSourceBase implements DataSource {
constructor(database: DatabaseBlockModel);
// Key methods (inherited/implemented):
cellValueGet(rowId: string, propertyId: string): unknown;
cellValueChange(rowId: string, propertyId: string, value: unknown): void;
propertyAdd(
insertToPosition: InsertToPosition,
ops?: { type?: string; name?: string }
): string | undefined;
propertyDelete(id: string): void;
rowAdd(insertPosition: InsertToPosition | number): string;
rowDelete(ids: string[]): void;
// Reactive signals:
readonly$: ReadonlySignal<boolean>;
rows$: ReadonlySignal<string[]>;
properties$: ReadonlySignal<string[]>;
propertyMetas$: ReadonlySignal<PropertyMetaConfig[]>;
// Composition:
viewManager: ViewManager;
provider: ServiceProvider;
}
Import
import { DatabaseBlockDataSource, databaseBlockProperties } from '@blocksuite/affine/blocks/database';
import type { DataSource } from '@blocksuite/affine/data-view';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| database | DatabaseBlockModel | Yes | The database block model to wrap (retrieve via store.getModelById) |
Outputs
| Name | Type | Description |
|---|---|---|
| datasource | DatabaseBlockDataSource | Adapter exposing viewManager, propertyAdd, cellValueChange, rows$, properties$, provider |
Usage Examples
Wrap and Add a Table View
const databaseId = store.addBlock('affine:database', { columns: [], cells: {} }, noteId);
const database = store.getModelById(databaseId) as DatabaseBlockModel;
const datasource = new DatabaseBlockDataSource(database);
datasource.viewManager.viewAdd('table');
Add a Typed Property and a Cell Value
const richTextId = datasource.propertyAdd('end', {
type: databaseBlockProperties.richTextColumnConfig.type,
name: 'Rich Text',
});
const rowId = store.addBlock('affine:paragraph', { type: 'text', text: new Text('row') }, databaseId);
datasource.cellValueChange(rowId, richTextId, new Text('row'));