Security

Security is built into the core of the TableCraft Engine. You can easily hide sensitive data, enforce tenant isolation, and manage soft deletes.

1. Hiding Sensitive Data

You often have columns like password_hash, stripe_token, or internal_notes that should never be exposed via the API.

Manual Hiding

You can explicitly hide columns using .hide().

// src/config/users.ts
import { defineTable } from '@tablecraft/engine';
import { users } from '../db/schema';

export const userConfig = defineTable(users)
  .hide('password', 'salt', 'resetToken') // Always excluded from SELECT *
  .toConfig();

The engine can automatically detect and hide common sensitive column names (like password, token, secret, key).

export const userConfig = defineTable(users)
  .autoHide() // Automatically hides 'password', 'api_key', etc.
  .toConfig();
circle-info

You can inspect what would be hidden without applying it:

2. Multi-Tenancy (Tenant Isolation)

If you are building a SaaS application, ensuring users only see their own organization's data is critical. The engine handles this automatically via the .tenant() configuration.

Configuration

Tell the engine which column stores the Tenant ID.

Usage

When creating the engine instance, pass the tenantId in the context. The engine will automatically append WHERE org_id = ? to every query, ensuring isolation.

circle-check

3. Soft Deletes

Soft deleting allows you to mark a row as deleted without removing it from the database. The engine respects this automatically.

Configuration

Tell the engine which column tracks deletion.

Behavior

By default, queries will automatically filter out deleted rows (WHERE deleted_at IS NULL).

If you need to query deleted items (e.g., for an admin trash can view), you can pass includeDeleted: true in the query params options (if allowed) or via the engine method options.

4. Access Control (RBAC)

You can define role-based access control directly on the table definition. This relies on the context object passed to the engine during execution.

Configuration

Define which roles or permissions are required to access the table:

Passing Context (Crucial)

For RBAC to work, you must provide the user's roles and permissions in the context.

circle-info

The engine checks context.user.roles and context.user.permissions against your configuration.

Adapter Configuration

Use the getContext function in your adapter setup to extract user info from the request (e.g., from a JWT or session).

Direct Engine Usage

If you use the engine directly, pass the context as the second argument to query.

Next Steps

Learn how to extend the engine with Raw SQL & Custom Logic for complex use cases.

Last updated

Was this helpful?