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.
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.
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.
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.
const users = db.prepare("SELECT * FROM user").all();
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.
const user = db.prepare("SELECT * FROM users WHERE id = :id").get({ id: 1 });
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.
const user = db.prepare("SELECT * FROM users").all();
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 $
.
db.prepare("SELECT * FROM users WHERE id = @id");
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.