TSLint rules to lint tinypg usage in TypeScript.
When using tinypg with typescript, it's possible to reference a missing SQL file. It's easy to pass in an object with unused properties or to pass an object with missing properties. Using typescript's AST, we can find usages of tinypg and make sure that it is being used appropriately. In the future, we may be able to add more rules for other gotchas like SQL injection warnings or misuse of transactions.
npm install tslint-tinypg --save-dev
See the example tslint.json file for configuration.
- tslint-tinypg 1.x.x is compatible with tslint 5.x.x.
The following rules are available:
This rule enforces that the object passed to a tiny .sql
call must have every property used inside the SQL file.
This is typically an error because you think that you are using that property, but you actually aren't.
Given:
SELECT *
FROM user
WHERE user.name = :name
in users.fetch
import { TinyPg } from 'tinypg'
const db = new TinyPg({
connection_string: 'postgres://postgres@localhost:5432/mydb',
root_dir: __dirname + '/sql_files'
})
// NOT OK
db.sql('users.fetch', {
name: 'Joe',
enabled: false,
})
//OK
db.sql('users.fetch', {
name: 'Joe',
})
This rule enforces all tiny calls to .sql
reference a valid SQL file.
Given:
├── sql_files
│ ├── fetch_user.sql
│ └── users
│ └── fetch.sql
import { TinyPg } from 'tinypg'
const db = new TinyPg({
connection_string: 'postgres://postgres@localhost:5432/mydb',
root_dir: __dirname + '/sql_files'
})
// NOT OK
db.sql('users.create', {
name: 'Joe',
})
//OK
db.sql('users.fetch', {
name: 'Joe',
})
db.sql('fetch_user', {
name: 'Joe',
})
Here's a sample TSLint configuration file (tslint.json) that activates all the rules:
{
"rulesDirectory": ["./node_modules/tslint-tinypg/rules"],
"rules": {
// Tinypg rules
"no-missing-sql-file": true,
"no-unused-properties": true,
"no-missing-properties": true
}
}
For new features file an issue. For bugs, file an issue and optionally file a PR with a failing test. Tests are really easy to do, you just have to edit the *.ts.lint
files under the test directory. Read more here about tslint testing.
To execute the tests run yarn test
.
To release a new package version run yarn publish:patch
, yarn publish:minor
, or yarn publish:major
.
This work was originally inspired by vscode-tinypg. The template for this repo came from jonaskello/tslint-immutable.