# Rules

> Tenant-editable rules: a filter tree over a subject type's field catalog, tested when a trigger event happens, with action references the app runs. rule_matches() evaluates a tree in SQL the same way evaluateFilterQuery does in TypeScript.

- Category: Automation
- Module: `rules`
- Modes: `managed`, `custom`
- Docs: https://bettersupabase.com/docs/blocks/rules
- Page: https://bettersupabase.com/blocks/rules

## Install

```bash
npx better-supabase add rules
```

The command also installs `updated-at`, `tenant`, `access`.

## Configure

Add the block to `better-supabase.config.ts`:

```ts
import { defineConfig } from "better-supabase/config";

export default defineConfig({
  blocks: {
    // Required by rules; add installs them too.
    "updated-at": {},
    tenant: {},
    access: {},
    // Modes: "managed", "custom". The default is "managed".
    rules: { mode: "managed" },
  },
});
```

## Client

The React hooks are in `better-supabase/blocks/rules/react`. They return data and actions and render no UI. Source: https://github.com/ScaleDockHQ/better-supabase/blob/main/packages/better-supabase/src/blocks/rules/react/index.ts

## Events

- `rule.disabled`
- `rule.enabled`
- `rule.matched`
- `rule.removed`
- `rule.saved`

## Related blocks

Requires: [tenant](https://bettersupabase.com/blocks/tenant.md), [access](https://bettersupabase.com/blocks/access.md)

## Recipes

- [Rule builder](https://bettersupabase.com/recipes/rule-builder.md): A condition tree with field, operator and value rows, nested And and Or groups, drag to reorder and a fixed first condition, saved as a versioned rule.
