API
All of the options that are mentioned below should be configured in your eslintrc
config file under the rule "@ts-safeql/check-sql"
. Be sure to read the Configuration guide first.
useConfigFile
If set to true
, SafeQL will look for a safeql.config.ts
file in the root of your project.
{
"useConfigFile": true
}
// safeql.config.ts
import { defineConfig } from "@ts-safeql/eslint-plugin";
export default defineConfig({
connections: {
// ...
},
});
INFO
This option cannot be used with connections
.
connections
Can be either an object that represents a single connection, or an array that represents multiple connections.
In most use cases, when you have only one database connection, you can pass a single object that represents the type of the connection. For example:
{
"connections": {
"databaseUrl": "postgres://user:pass@localhost:5432/dbname",
"tagName": "sql"
}
}
When working with multiple connections, you can pass an array of connections:
{
"connections": [
{
"databaseUrl": "postgres://user:pass@localhost:5432/dbname",
"tagName": "sql"
},
{
"databaseUrl": "postgres://user:pass@localhost:5432/dbname2",
"name": "Prisma",
"operators": ["$queryRaw", "$executeRaw"]
}
]
}
connections.databaseUrl
The database URL that the plugin will use to infer the types of the queries, and report any errors.
INFO
- this option cannot be used with
migrationsDir
,connectionUrl
, anddatabaseName
connections.migrationsDir
The path to the directory where the database migrations are located. For example:
{
"connections": {
"migrationsDir": "prisma/migrations",
"databaseName": "..."
// ...
}
}
INFO
- this option must be used with
databaseName
. - this option can be used with
connectionUrl
. - this option cannot be used with
databaseUrl
.
connections.connectionUrl
When using migrations, we don't have a database URL. In order to connect to Postgres, we need to connect to an existing database. The default value is:
postgres://postgres:postgres@localhost:5432/postgres
A connection URL is required to create a shadow database (and drop it after) to query the database metadata so it can infer the types of the queries, and report any errors.
SHADOW DATABASE
Shadow database is a database that is being used to query the migrations folder metadata. It is created and dropped automatically by SafeQL.
INFO
- this option must be used with
migrationsDir
, anddatabaseName
. - this option cannot be used with
databaseUrl
.
connections.databaseName
The name of the shadow database that will be created and dropped automatically by SafeQL. Read more in connectionUrl
option. For example:
{
"connections": {
"migrationsDir": "...",
"databaseName": "my_shadow_db"
// ...
}
}
INFO
- this option must be used with
migrationsDir
. - this option can be used with
connectionUrl
. - this option cannot be used with
databaseUrl
.
connections.tagName
The name of the tag that SafeQL will use to analyze the queries. For example, "tagName": "sql"
for:
const sql = postgres();
sql`SELECT id FROM users`;
// will be fixed to
sql<{ id: number }>`SELECT id FROM users`;
TIP
If you're using Postgres.js, then be sure to check out the guide on how to use SafeQL with Postgres.js.
connections.name
The name of the variable the holds the connection. For example, "name": "conn"
for:
const conn = new Client();
conn.query(...);
connections.operators
The names of the operators that SafeQL will use to analyze the queries. For example, "operators": ["$queryRaw", "$executeRaw"]
for:
const conn = new Client();
conn.$queryRaw(...); // will be fixed to conn.$queryRaw<{ ... }>(...);
conn.$executeRaw(...); // will be fixed to conn.$executeRaw<{ ... }>(...);
connections.transform
Transform the end result of the query. For example, if you want to transform the result of the query to be an array of objects, you can use the following:
{
"connections": {
// ...
"transform": "${type}[]"
}
}
EXAMPLES
"${type}[]"
will transform the type to an array of the type.["Nullable", "Maybe"]
will replaceNullable
withMaybe
in the type.["${type}[]", ["Nullable", "Maybe"]]
will do both
connections.fieldTransform
Transform the (column) field key. Can be one of the following:
"snake"
-user_id
→userId
"camel"
-user_id
→userId
"pascal"
-user_id
→UserId
"screaming snake"
-user_id
→USER_ID
connections.keepAlive
True by default. If set to false, the connection will be closed after the query is executed. This is not recommended, and should only be used if you're sure that the connection should be closed.
connections.overrides.types
Override the default type mapping. For example, if you want to use LocalDate
instead of Date
:
{
"connections": {
"overrides": {
"types": {
"date": "LocalDate"
}
}
}
}
INFO
Please note that SafeQL won't actually parse the type, since SafeQL runs only in the tooling system (i.e, not in runtime).