# Full-stack blocks

Database, server functions, cron, and CLI in one folder.

[Read the guide](https://blockstudio.dev/guides/full-stack-blocks/)

## db.php

Define schema, get CRUD endpoints

db.php

```
<?php
return [
  'storage'    => 'sqlite',
  'userScoped' => true,
  'capability' => [
    'create' => true,
    'read'   => true,
    'update' => true,
    'delete' => true,
  ],
  'fields' => [
    'text' => ['type' => 'string', 'required' => true],
    'done' => ['type' => 'boolean', 'default' => false],
  ],
];
```

## rpc.php

Custom server functions

rpc.php

```
<?php
use Blockstudio\Db;

return [
  'toggle' => function (array $params): array {
    $db   = Db::get('my-theme/todo');
    $id   = (int) ($params['id'] ?? 0);
    $todo = $db->get_record($id);
    if ($todo) {
      $db->update($id, ['done' => !$todo['done']]);
    }
    return ['success' => (bool) $todo];
  },
  'clear_done' => function (): array {
    $db = Db::get('my-theme/todo');
    foreach ($db->list(['done' => true], 500) as $todo) {
      $db->delete((int) $todo['id']);
    }
    return ['cleared' => true];
  },
];
```

## Every capability is a file

Each file adds one capability. No configuration and no registration code.

Application block

blockstudio/

└── my-app/

├── block.json Block definition

├── index.php Template + Interactivity API

├── style.css Scoped styles

├── script-inline.js Client logic

├── db.php Data model + CRUD

├── rpc.php Server functions

├── cron.php Scheduled tasks

└── db/default.sqlite

## Database

Define schemas, get CRUD endpoints.

## [6 storage modes](https://blockstudio.dev/docs/blocks/block-api/database/#storage-modes)

table, sqlite, jsonc, storh, meta, post_type. Choose by portability and query needs.

## [Storh files](https://blockstudio.dev/docs/blocks/block-api/database/#storh-files-storage-storh)

One file per record under uploads, with indexes built from the schema and safer concurrent writes. wp bs db migrate –to=storh moves JSONC records over and keeps their IDs.

## [Tables and post types](https://blockstudio.dev/docs/blocks/block-api/database/#custom-table-storage-table)

table storage manages a MySQL table with dbDelta and adds columns for new fields. post_type storage makes each record a post, so WP_Query, REST API plugins, and GraphQL tools can read it.

## [Multiple schemas](https://blockstudio.dev/docs/blocks/block-api/database/#multiple-schemas)

Return named schemas from one db.php. Each gets its own storage, endpoints, and access control.

## [User-scoped data](https://blockstudio.dev/docs/blocks/block-api/database/#user-scoped-data)

Set userScoped to isolate records automatically, with no manual filtering.

## [Validation and hooks](https://blockstudio.dev/docs/blocks/block-api/database/#validation)

Types, required values, enums, formats, lengths, callbacks, and lifecycle hooks.

## RPC

Server functions callable from the frontend.

## [bs.fn() client](https://blockstudio.dev/docs/blocks/block-api/rpc/#calling-from-javascript)

Call functions from JavaScript; inline scripts auto-detect the block name.

## [Access control](https://blockstudio.dev/docs/blocks/block-api/rpc/#access-control)

Public with CSRF, open for webhooks, or capability-based, per function.

## [Cross-block calls](https://blockstudio.dev/docs/blocks/block-api/rpc/#cross-block-calling)

Call any block function from JavaScript or PHP and compose logic.

## Client data layer

Read, cache, and write records from the browser.

## [bs.db() client](https://blockstudio.dev/docs/blocks/block-api/database/#javascript-client)

Create, list, get, update, and delete records from JavaScript. list takes field filters, limit, and offset.

## [bs.query()](https://blockstudio.dev/docs/blocks/block-api/database/#query)

Cached fetches by key. Calls in flight share one promise, and staleTime skips the request while the data is fresh.

## [bs.mutate()](https://blockstudio.dev/docs/blocks/block-api/database/#mutate)

Optimistic create, update, and delete on a reactive state array, rolled back to a snapshot when the request fails.

## [bs.cache](https://blockstudio.dev/docs/blocks/block-api/database/#cache)

The in-memory cache behind bs.query() and bs.mutate(). Read, set, invalidate, or clear entries directly.

## [Realtime polling](https://blockstudio.dev/docs/blocks/block-api/database/#realtime-polling)

Set realtime in db.php and the client compares a data hash on an interval, then updates the Interactivity API store. Polling pauses in hidden tabs.

## [Cached pages](https://blockstudio.dev/docs/blocks/block-api/rpc/#cached-pages)

bs.fn() and bs.db() fetch fresh credentials from an uncached endpoint on the first call, so public calls keep working on pages served from long-lived caches.

## Forms and app screens

Forms rendered from the schema, and bundled UI to build on.

## [Schema forms](https://blockstudio.dev/docs/blocks/block-api/database/#form-rendering)

Give a field a component key and bs_db_form() renders the form through those blocks, passing each one its field definition.

## [Bundled form](https://blockstudio.dev/guides/ui-app-screen/#step-4-the-template)

With ui.enabled, bsui/form block=”…” sends its fields to that block’s create endpoint, shows each validation error under its field, and dispatches formsuccess or formerror.

## [Example apps](https://blockstudio.dev/docs/general/settings/#ui)

The ui setting also registers the app/* examples, such as app/kitchen-sink and app/chat, built from the same bsui components.

## Infrastructure

Cron, CLI, setup code, and portability.

## [Scheduled tasks](https://blockstudio.dev/docs/blocks/block-api/cron/)

cron.php jobs register with WP-Cron. Composer installs remove them with wp bs teardown.

## [Database CLI](https://blockstudio.dev/docs/dev/cli/#database)

wp bs db lists records with field filters, gets, creates, updates, and deletes them, and prints tables, JSON, or CSV.

## [Test from the terminal](https://blockstudio.dev/docs/dev/cli/#call-a-function)

wp bs rpc call runs a function with –key=value parameters, and wp bs cron run fires a job now, whatever its schedule.

## [Init files](https://blockstudio.dev/docs/blocks/block-api/initialization/)

init.php and other init*.php files in a block folder run once on init, for setup such as registering a post type.

## [PHP-native definitions](https://blockstudio.dev/docs/blocks/block-api/rpc/#php-native-definitions)

rpc.php and cron.php can return a class with #[Rpc] and #[Cron] methods and the Access, Method, and Schedule enums. db.php can use Schema::make() and Field builders.

## [Portability](https://blockstudio.dev/guides/full-stack-blocks/)

SQLite and JSONC travel with the code: copy one folder to deploy the app.

Plus

## The official extension kit

Premium site templates, AI system instructions, and a private Discord community. One-time purchase, lifetime updates.

- 500+ site templates
- AI system instructions
- Lifetime updates
- Private Discord community

[Get Plus](https://blockstudio.dev/plus/)
