ESLint Plugin for Kysely
eslint-plugin-kysely is an ESLint plugin designed to enforce safe and best-practice database operations when using the Kysely SQL query builder. It helps developers prevent common SQL-related issues like accidental full table scans by enforcing WHERE clauses, ensures explicit SELECT statements to avoid ambiguity, and prevents ambiguous column references in JOIN queries. The plugin also provides rules for correct Kysely operator usage, such as `is` for null comparisons and `in` for array operations, rather than `=` or `!=`. The current stable version is 1.0.7, indicating an early but stable release. Its release cadence is likely driven by user feedback and Kysely's evolution. A key differentiator is its specific focus on Kysely's API, providing tailored linting for Kysely users rather than generic SQL linting.
Common errors
-
ESLint: Definition for rule 'kysely/enforce-where-clause' was not found.
cause The ESLint plugin is either not installed or not correctly listed in the `plugins` array of your ESLint configuration.fixEnsure `eslint-plugin-kysely` is installed (`npm install --save-dev eslint-plugin-kysely`) and add `"kysely"` to the `plugins` array in your `.eslintrc.*` file. -
Parsing error: 'import' and 'export' may appear only with 'sourceType: "module"'
cause Your ESLint configuration is missing or incorrectly configured `parserOptions.sourceType` for ECMAScript modules.fixAdd or update your `parserOptions` in `.eslintrc.*`: `"parserOptions": { "ecmaVersion": 2020, "sourceType": "module" }`. -
Parsing error: Unexpected token, expected ";" (or similar TypeScript parsing error)
cause ESLint is trying to parse TypeScript syntax without a TypeScript parser configured, or without `project` specified for type-aware linting.fixInstall `@typescript-eslint/parser` and configure it in your `.eslintrc.*`: `"parser": "@typescript-eslint/parser", "parserOptions": { "project": "./tsconfig.json" }`.
Warnings
- gotcha This plugin is a fork of `eslint-plugin-kysely-rules`. Users should ensure they are installing `eslint-plugin-kysely` to get the latest features and updates from this specific project. Installing the incorrect package may lead to outdated rules or unexpected behavior.
- gotcha The plugin's rules are designed for Kysely's specific API. Attempting to use these rules on raw SQL strings or other ORMs/query builders will not yield meaningful results and may produce false positives or negatives. Ensure your codebase primarily uses Kysely for database operations where these rules are applied.
- gotcha When using Kysely in a TypeScript project, ensure your ESLint configuration includes a TypeScript parser (e.g., `@typescript-eslint/parser`) and the relevant `parserOptions` to correctly parse Kysely's type-safe query syntax. Without this, ESLint may fail to parse your files or apply rules correctly.
Install
-
npm install eslint-plugin-kysely -
yarn add eslint-plugin-kysely -
pnpm add eslint-plugin-kysely
Imports
- plugins
import * as kyselyPlugin from 'eslint-plugin-kysely';
{ "plugins": ["kysely"] } - extends
import recommendedConfig from 'eslint-plugin-kysely/recommended';
{ "extends": ["plugin:kysely/recommended"] } - rules
import { enforceWhereClause } from 'eslint-plugin-kysely/rules/enforce-where-clause';{ "rules": { "kysely/enforce-where-clause": "error" } }
Quickstart
npm install --save-dev eslint eslint-plugin-kysely
// .eslintrc.json
{
"root": true,
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"extends": [
"eslint:recommended",
"plugin:kysely/recommended"
]
}
// To lint your project files (e.g., .ts, .js):
npx eslint . --ext .js,.ts