Skip to Content
Zenlev
Guides

Database linting

Check ZModel, application queries, migration SQL, and deployed objects against configurable policy.

Run database linting before you submit schema or migration changes. Zenlev ships the linter in the same package as the migration commands.

bun run zenlev lint
bun run zenlev lint --max-warnings 0 --json
bun run zenstack-zenlev lint

The offline command reads ZModel, migration source, configured SQL files, and application files selected by lint.source. It loads project configuration and lint rule plugins. It skips database acquisition, client-module loading, migration plugin initialization, and migration module imports.

TypeScript configuration and lint rule plugins execute as trusted project code. Keep their module-level code free of external effects.

Zenlev applies the recommended preset automatically. It requires PascalCase model names, camelCase field names, snake_case physical tables and columns, and id for single-column primary keys. It rejects database-generated sequential primary key values.

Zenlev parses and resolves ZModel with the installed @zenstackhq/language parser. It inspects the resolved AST before schema migration projection. Imports, inherited fields, relations, and physical mappings use ZenStack's own resolution.

model OrderLine {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now()) @map("created_at")

  @@map("order_line")
}

The model and field names follow ZenStack conventions. The mapping attributes establish the physical snake_case names. Findings point to a mapping attribute when present, or to the declaration that needs one. Relation and computed fields receive logical naming checks. Physical column rules apply to stored fields.

UUIDs, CUIDs, and application-assigned keys remain valid. Plural table names, join shapes, UUID database types, timestamps, stored-object restrictions, and access policies are configurable.

import { defineConfig, recommendedLintConfig } from '@mwillbanks/zenstack-zenlev'

export default defineConfig({
  lint: {
    ...recommendedLintConfig,
    sql: ['database/**/*.sql'],
    rules: {
      ...recommendedLintConfig.rules,
      'zenlev/naming/table-number': ['error', { form: 'plural' }],
      'zenlev/objects/restricted': [
        'error',
        { kinds: ['function', 'procedure'], actions: ['create', 'replace'] },
      ],
    },
  },
})

This rule permits cleanup drops while rejecting new routines. Use presets: [] to start with an explicit rule set. The built-in naming, objects, and access presets provide opt-in policy groups.

Check application queries

Set lint.source to application TypeScript and JavaScript globs relative to the effective project directory. Application scanning is opt-in. The recommended preset enables three query rules for selected files.

import { defineConfig } from '@mwillbanks/zenstack-zenlev'

export default defineConfig({
  lint: {
    source: ['src/**/*.{ts,tsx,js,jsx}'],
    rules: {
      'zenlev/queries/allowed-types': ['error', { allow: ['query-builder', 'query-api'] }],
      'zenlev/queries/no-direct-kysely': ['error', { mode: 'auto' }],
      'zenlev/queries/no-raw': 'error',
      'zenlev/queries/no-schema-changes': 'error',
    },
  },
})

allowed-types permits ZenStack's $qb query builder and model delegate query API by default. Restrict allow to either form when the application requires one style. Raw SQL and low-level client access have separate rules.

no-direct-kysely rejects $qbRaw, including extracting it without calling it. In ZenStack mode, it also rejects constructing or using a separate Kysely query client. Type-only imports and dialect setup remain valid. Auto mode uses available project configuration to distinguish ZenStack and standalone Kysely. Set mode: 'zenstack' or mode: 'standalone' when configuration cannot establish the intended convention. Standalone mode permits typed Kysely queries, but never exempts $qbRaw.

no-raw rejects recognized raw execution APIs, Kysely sql templates and raw fragments, and driver calls that accept SQL text. Bound parameters do not exempt raw SQL. Executing SQL loaded from a file remains a raw query.

The optional no-schema-changes rule rejects application-side schema builders. Put schema changes in migrations instead.

Discovered migrations are exempt from application query rules, even when lint.migrations: false. Existing migration, configured SQL, live catalog, and write-plan checks retain their behavior. Application scanning runs offline without importing application modules or acquiring a client. It does not add application scanning to lintPlugin() or lint --live.

The Oxc analysis follows statically traceable aliases, destructuring, bracket access, and transaction clients. Unrelated methods with matching names do not establish a database operation. Dynamic wrappers can leave coverage gaps. Review those gaps before claiming compliance.

Use existing lint.overrides to scope exceptions by file. For example, a reviewed maintenance command can disable one rule without disabling application checks elsewhere.

lint: {
  source: ['src/**/*.{ts,tsx,js,jsx}'],
  overrides: [
    {
      files: ['src/maintenance/rebuild.ts'],
      rules: { 'zenlev/queries/no-schema-changes': 'off' },
    },
  ],
}

Configure a standalone Kysely project

Disable ZModel discovery and declare the SQL dialect for a SQL-only project.

import { defineConfig } from '@mwillbanks/zenstack-zenlev'

export default defineConfig({
  lint: {
    zmodel: false,
    dialect: 'postgresql',
    sql: ['database/**/*.sql'],
  },
})

Offline lint uses Oxc to read both up and down migration handlers. It projects supported Kysely schema builders directly into lint evidence, and parses static SQL strings and Kysely SQL templates with bound values. Dynamic SQL, dynamic module imports, unresolved helper effects, and opaque procedural bodies produce coverage warnings. Plain SQL and unescaped TypeScript literals can point to the SQL token. Escaped strings, interpolated templates, and Kysely builders point to the enclosing source expression. PostgreSQL source signatures use authored argument text where available. Live signatures use catalog input types. Source call rules recognize explicit CALL and qualified calls in supported SQL queries. Unqualified function calls can produce unresolved-identity warnings. Function overload selection can depend on runtime argument types.

Install a parser for SQL text and SQLite catalogs

Install an optional peer for authored SQL text and SQLite catalog parsing of stored table definitions. Zenlev loads that parser when SQL text analysis first needs it. Structured ZModel, supported Kysely schema builders, and generated statement evidence do not need a SQL parser peer. A SQLite catalog audit during db push still attempts to parse each stored table definition that exists.

DialectParser peerInstall with Bun
PostgreSQLlibpg-querybun add --dev libpg-query@18
MySQLdt-sql-parserbun add --dev dt-sql-parser@4
SQLitesqlite3-parserbun add --dev sqlite3-parser@0.7

Use regular dependencies when migrations or schema push run in a deployment that omits dev dependencies. PostgreSQL uses its native grammar through WebAssembly. MySQL uses the MySQL grammar from dt-sql-parser. SQLite uses the published SQLite grammar. These parsers validate authored SQL syntax and provide statement boundaries. Oxc parses TypeScript migration source and ZenStack parses ZModel. Static migration analysis projects supported Kysely schema builders directly into normalized lint nodes. Schema push carries structured evidence alongside each generated SQL statement, in execution order. The SQL remains available to existing hooks unchanged.

A missing SQL parser produces a coverage warning when an input requires SQL text parsing, such as a .sql file or raw SQL in a migration. SQLite catalog checks read available metadata, including PRAGMA results, and attempt to parse every stored table definition that exists to recover facts metadata does not expose, such as named CHECK constraints. Without the SQLite parser, those definitions produce parse coverage gaps while metadata-based checks continue. If a table has no stored definition, only rule-specific facts that depend on that definition are unavailable. ZModel-only lint, supported Kysely builder analysis, and generated schema push evidence need no SQL parser. Set lint.dialect for standalone Kysely projects so builder details use the project's dialect.

Enforce policy before writes

Register the supplied migration plugin to check the final write plan.

import { defineConfig, lintPlugin } from '@mwillbanks/zenstack-zenlev'
import { db } from './db'

export default defineConfig({
  client: db,
  plugins: [lintPlugin()],
  lint: {
    rules: {
      'zenlev/objects/restricted': [
        'error',
        { kinds: ['function', 'procedure'], actions: ['create', 'replace'] },
      ],
    },
  },
})

The plugin uses the same lint settings as the standalone command. It checks the locked, validated apply plan after plan transformations. Generated rollback migrations include the original down handlers in execution order. Schema push checks the recomputed, confirmed SQL, per-statement structured evidence, and affected desired definitions. Diagnostics backed by generated statement evidence point to the start of the corresponding generated statement and use its SQL hash. ZModel diagnostics retain their ZModel source locations. Existing exceptions based on a previous generated-statement range may need to be refreshed.

An error rejects the entire plan before migration handlers or push SQL execute. Coverage warnings permit the write. Require lint --max-warnings 0 in pull request checks when all warnings need review.

The existing lock path can initialize Zenlev's internal ledger before a lint rejection. Dry runs, plan-only commands, SQL previews, and migration generation stay outside write enforcement. Static analysis reports supported effects. Review arbitrary migration code and external effects separately.

Audit deployed objects

Run the catalog audit with your configured environment connection.

bun run zenlev lint --live --env staging --json

Use an account with the metadata permissions needed for the selected schemas. The audit reads metadata and excludes Zenlev's configured ledger objects. PostgreSQL event triggers are database-wide and can appear in a namespace-scoped audit. Supplied connections remain caller-owned. Factory-owned connections close after the audit.

EvidencePostgreSQLMySQLSQLite
Tables, columns, keys, indexes, constraintsCatalog readersInformation schemaSchema catalog and pragmas
Stored objectsRoutines, views, materialized views, triggers, event triggers, sequences, extensionsRoutines, views, triggers, scheduled eventsPersisted views and triggers
Native RLSTable flags and policiesCapability unavailableCapability unavailable
Authored quoting and explicit namesSource evidenceSource evidenceSource evidence
Application-defined functionsConnection behavior needs separate reviewConnection behavior needs separate reviewConnection functions and extensions need separate review

Capabilities describe each input's coverage, including object kinds and attributes such as column:default and policy:with-check. Missing permissions or unavailable metadata produce coverage gaps. MySQL can hide metadata based on privileges. Catalog names cannot recover original identifier spelling or prove whether a constraint was explicitly named.

A custom lint.catalog provider can normalize another adapter's metadata into LintInput. Declare its dialect and expose the supported capabilities.

Access-policy checks

Scope PostgreSQL RLS checks to the intended tables and roles.

lint: {
  rules: {
    'zenlev/postgres/rls': [
      'error',
      {
        tables: ['public.accounts'],
        enabled: true,
        force: true,
        explicitWriteCheck: true,
        noBroad: true,
        allowedRoles: ['application'],
        roles: ['application'],
        commands: ['select', 'insert', 'update', 'delete'],
      },
    ],
    'zenlev/zenstack/policies': [
      'error',
      { commands: ['create', 'read', 'update', 'delete'], postUpdate: true, noBroad: true },
    ],
  },
}

PostgreSQL allows UPDATE and ALL policies to inherit USING for writes when WITH CHECK is absent. The explicit-write-check option enforces a stricter declaration convention. Set defaultDeny: true when denying all access is intentional. Table CHECK constraints and RLS WITH CHECK clauses have separate rules.

ZenStack policies run through application queries. V3 requires explicit post-update rules when that operation needs protection. Relation delegation through check() can be valid.

Role ownership, grants, membership, bypass privileges, and expression semantics affect access. Test tenant isolation with representative application roles on a disposable database.

Adopt existing databases

Use ordered overrides to scope settings by source, dialect, object kind, origin, or identity.

lint: {
  overrides: [
    {
      files: ['migrations/legacy/**'],
      rules: { 'zenlev/keys/no-sequential': 'warn' },
    },
  ],
  exceptions: [
    {
      ruleId: 'zenlev/naming/table-case',
      identity: 'public.LegacyAccount',
      reason: 'Keep the vendor-owned table name until its replacement is deployed.',
    },
  ],
}

Copy an exact identity or source fingerprint from a diagnostic. Exceptions require a reason. Source fingerprints reopen review when source changes. Reports include suppressed counts and stale exceptions.

Use rule errors for proven violations. Review coverage gaps before claiming full policy compliance. Exit code 0 means the configured checks passed. Exit code 1 means a policy error or warning budget failed. Exit code 2 means configuration, input, plugin, or audit execution failed.

See the lint rule reference, configuration reference, and plugin API.

On this page