Schemalint

2.3.2 · active · verified Wed Apr 22

Schemalint is a Node.js-based command-line interface (CLI) tool designed to apply linting rules to PostgreSQL database schemas. It aims to enforce consistent naming conventions, best practices, and architectural patterns directly on the database structure, similar to how ESLint functions for JavaScript code. The package is currently at version 2.3.2 and receives regular updates, often bumping dependencies and occasionally adding new built-in rules or features, typically every few weeks or months. Its key differentiators include built-in rules for common PostgreSQL patterns (e.g., preferring `text` to `varchar`, `jsonb` to `json`), the ability to define custom rules via a plugin system, and a robust configuration model that allows for fine-grained control over rule severity and ignored identifiers. It integrates directly with `node-postgres` for schema extraction.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic installation and command-line execution of Schemalint, along with a complete example of a `.schemalintrc.js` configuration file for linting a PostgreSQL schema with common rules.

{
  // .schemalintrc.js
  // This file should be in your project root or specified via CLI.
  /** @type {import("schemalint").Config } */
  module.exports = {
    // Connection configuration. Uses environment variables for security.
    connection: {
      host: process.env.DB_HOST ?? 'localhost',
      user: process.env.DB_USER ?? 'postgres',
      password: process.env.DB_PASSWORD ?? 'postgres',
      database: process.env.DB_NAME ?? 'acme_database',
      charset: "utf8",
    },

    // Schemas to be linted (e.g., 'public').
    schemas: [{ name: "public" }],

    // Linting rules with severity ('error', 'off') and parameters.
    rules: {
      "name-casing": ["error", "snake"],
      "name-inflection": ["error", "singular"],
      "prefer-jsonb-to-json": ["error"],
      "prefer-text-to-varchar": ["error"],
      "mandatory-columns": ["error", { "columns": ["id", "created_at", "updated_at"] }]
    },

    // (Optional) Ignore specific targets or rules.
    ignores: [
      { identifier: "public.sessions", rule: "name-inflection" },
      { identifierPattern: "public\\.knex_migrations.*", rulePattern: ".*" },
    ],

    // (Optional) Load custom rules from local paths.
    plugins: [],
  };
}

// To install:
// npm install -D schemalint

// To run (from the directory containing .schemalintrc.js):
// npx schemalint

view raw JSON →