Skip to content

ESLint plugin that can validate SQLite queries and automatically generate types for query parameters and results

License

Notifications You must be signed in to change notification settings

merceyz/eslint-plugin-sqlite

Repository files navigation

eslint-plugin-sqlite

Description

An ESLint plugin that can validate SQLite queries and automatically generate types for query parameters and results when using the better-sqlite3 library.

Usage

Note that the following examples exclude the setup of TypeScript linting.

// eslint.config.js
import { createSqlitePlugin } from "eslint-plugin-sqlite";

const sqlitePlugin = createSqlitePlugin({
	getDatabase() {
		return new URL("./database.db", import.meta.url);
	},
});

export default [sqlitePlugin.configs.recommended];

If you have multiple databases you can return a different URL based on the name of the database and/or name of the file that is being linted.

// eslint.config.js
import { createSqlitePlugin } from "eslint-plugin-sqlite";

const sqlitePlugin = createSqlitePlugin({
	getDatabase({ name, filename }) {
		if (filename.includes("authentication") && name === "users") {
			return new URL("./users_database.db", import.meta.url);
		} else {
			return new URL("./database.db", import.meta.url);
		}
	},
});

export default [sqlitePlugin.configs.recommended];

If you require additional setup for the database you can return a Database instance instead of a URL.

// eslint.config.js
import { createSqlitePlugin } from "eslint-plugin-sqlite";
import Database from "better-sqlite3";

const sqlitePlugin = createSqlitePlugin({
	getDatabase() {
		const db = new Database("my_database.db");
		db.loadExtension("mod_spatialite");
		return db;
	},
});

export default [sqlitePlugin.configs.recommended];

Editor support

VSCode

By default the ESLint extension for VSCode uses the Node.js version included with VSCode, if that version isn't the same major version as the one used by your project then you need to configure the extension to use the version of Node.js that your project uses.

You can do that by adding the following to your .vscode/settings.json file:

{
	"eslint.runtime": "node"
}

Configs

recommended

The recommended config enables the valid-query, typed-input, and typed-result rules.

Rules

valid-query

Validates that the query can be prepared by SQLite.

// Bad - Error: in prepare, no such table: user (1)
const users = db.prepare("SELECT * FROM user").all();

// Good
const users = db.prepare("SELECT * FROM users").all();

typed-input

Generates types for the input parameters of a query.

The type of an input parameter is set to unknown and for named parameters you can replace that unknown with a more specific type.

// Bad
const user = db.prepare("SELECT * FROM users WHERE id = :id").get({ id: 1 });

// Good
const user = db
	.prepare<{ id: unknown }>("SELECT * FROM users WHERE id = :id")
	.get({ id: 1 });

typed-result

Generates types for the result of a query.

If the type of a result column can't be determined then it will be typed as unknown which you can replace with a more specific type.

// Bad
const user = db.prepare("SELECT * FROM users").all();

// Good
const user = db
	.prepare<[], { id: number; name: string }>("SELECT * FROM users")
	.all();

parameter-prefix

Enforce that all queries use the same prefix for named parameters. Can be configured to one of : (default), @, or $.

// Bad
/* eslint "sqlite/parameter-prefix": ["error", ":"] */
db.prepare("SELECT * FROM users WHERE id = @id");

// Good
/* eslint "sqlite/parameter-prefix": ["error", ":"] */
db.prepare("SELECT * FROM users WHERE id = :id");

License

eslint-plugin-sqlite is licensed under the MIT License and uses various Rust crates compiled to WebAssembly and bundled with the plugin, their licenses can be found in Third Party Licenses.

About

ESLint plugin that can validate SQLite queries and automatically generate types for query parameters and results

Resources

License

Stars

Watchers

Forks