SafeQL ✨ node-postgres
SafeQL is compatible with node-postgres which is the most popular postgres library in the ecosystem!
PLEASE NOTE
node-postgres doesn't come with a built-in SQL template tag (sql``
).
Thus, you'll need to install @ts-safeql/sql-tag in order to use SafeQL with node-postgres.
If you prefer using a different SQL template tag library, that's fine too! see sql-template-strings and sql-template-tag
First, Make sure you've added @ts-safeql/eslint-plugin
to your ESLint plugins:
json
// .eslintrc.json
{
"plugins": [..., "@ts-safeql/eslint-plugin"],
...
}
Second, add the following rule to your ESLint config:
json
// .eslintrc.json
{
// ...
"rules": {
// ...
"@ts-safeql/check-sql": [
"error",
{
"connections": [
{
// ...
// The name of the variable that holds the connection:
"name": "client",
// An array of operators that wraps the raw query:
"operators": ["query"]
}
]
}
]
}
}
Lastly, SafeQL will be able to lint your queries like so:
typescript
import { Client } from "pg";
const client = new Client();
// Before
const query = client.query("SELECT idd FROM users");
~~~ Error: column "idd" does not exist
// After bug fix
const query = client.query("SELECT id FROM users");
~~~~~~~~~~~~ Error: Query is missing type annotation
// After: ✅
const query = client.query(sql`SELECT idd FROM users`);