Skip to content

Commit

Permalink
bump eslint-plugin-mmkal
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Mar 22, 2024
1 parent 98f869d commit 69655d9
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 137 deletions.
2 changes: 1 addition & 1 deletion packages/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.15",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.3-0",
"eslint-plugin-mmkal": "0.5.0",
"strip-indent": "^4.0.0",
"tsx": "^4.7.1",
"typescript": "^5.2.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@types/pg": "^8.11.0",
"@types/toposort": "^2.0.7",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.3-0",
"eslint-plugin-mmkal": "0.5.0",
"execa": "^8.0.1",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
Expand Down
2 changes: 1 addition & 1 deletion packages/migra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@types/lodash": "^4.14.202",
"@types/node": "^20.11.10",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.3-0",
"eslint-plugin-mmkal": "0.5.0",
"execa": "^8.0.1",
"lodash": "^4.17.21",
"quicktype-core": "^23.0.81",
Expand Down
10 changes: 1 addition & 9 deletions packages/migrator/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
module.exports = [
...require('eslint-plugin-mmkal').recommendedFlatConfigs,
{
files: ['**/*.md/*commented-out.js'],
rules: {
'prettier/processed': 'off', // mmkal filter out rules other than codegen on the codegen-preprocessed file
},
},
]
module.exports = require('eslint-plugin-mmkal').recommendedFlatConfigs
2 changes: 1 addition & 1 deletion packages/migrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@types/node": "^20.11.10",
"dedent": "^1.5.1",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.3-0",
"eslint-plugin-mmkal": "0.5.0",
"execa": "^8.0.1",
"fs-syncer": "^0.4.0",
"js-yaml": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/migrator/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ Assuming `migrate.js` is a script setup something like:
```js
const {Migrator} = require('@pgkit/migrator')

const migrator = new Migrator(...)
const migrator = new Migrator(/* ... */)
migrator.runAsCLI()
```

Expand Down
2 changes: 1 addition & 1 deletion packages/pgkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@types/node": "^20.11.20",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.3-0",
"eslint-plugin-mmkal": "0.5.0",
"vitest": "^1.2.2"
},
"dependencies": {
Expand Down
9 changes: 1 addition & 8 deletions packages/schemainspect/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
module.exports = [
...require('eslint-plugin-mmkal').recommendedFlatConfigs,
{
rules: {
'no-redeclare': 'off', // mmkal: typescript has valid redeclare use cases
},
},
]
module.exports = require('eslint-plugin-mmkal').recommendedFlatConfigs
2 changes: 1 addition & 1 deletion packages/schemainspect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"devDependencies": {
"@types/node": "^20.11.20",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.3-0",
"eslint-plugin-mmkal": "0.5.0",
"quicktype-core": "^23.0.81",
"ts-node": "^10.9.2",
"vitest": "^1.2.2"
Expand Down
9 changes: 8 additions & 1 deletion packages/typegen/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
module.exports = require('eslint-plugin-mmkal').recommendedFlatConfigs
module.exports = [
...require('eslint-plugin-mmkal').recommendedFlatConfigs,
{
rules: {
'@typescript-eslint/no-namespace': 'off',
},
},
]
2 changes: 1 addition & 1 deletion packages/typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@types/memoizee": "0.4.8",
"@types/pluralize": "0.0.29",
"eslint": "^8.57.0",
"eslint-plugin-mmkal": "0.4.3-0",
"eslint-plugin-mmkal": "0.5.0",
"execa": "^5.1.1",
"expect-type": "^0.17.3",
"fs-syncer": "^0.5.2",
Expand Down
53 changes: 32 additions & 21 deletions packages/typegen/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Select statements, joins, and updates/inserts/deletes using `returning` are all
- [Enhancing Return Types](#enhancing-return-types)
- [Ignoring Queries](#ignoring-queries)
- [Examples](#examples)
- [Migration from v0.8.0](#migration-from-v080)
- [Migration from @slonik/typgen](#migration-from-sloniktypgen)
- [Migration from v0.8.0 of @slonik/typegen](#migration-from-v080-of-sloniktypegen)
- [SQL files](#sql-files)
- [Usage with `@typescript-eslint`](#usage-with-typescript-eslint)
- [Limitations](#limitations)
Expand Down Expand Up @@ -91,7 +92,9 @@ import {sql, createPool} from '@pgkit/client'
export default async () => {
const pool = createPool('...connection string...')

const results = await pool.query(sql<queries.TestTable>`select foo, bar from test_table`)
const results = await pool.query(
sql<queries.TestTable>`select foo, bar from test_table`,
)

results.rows.forEach(r => {
console.log(r.foo) // foo has type 'number'
Expand Down Expand Up @@ -120,10 +123,10 @@ export declare namespace queries {

## Configuration

The CLI can run with zero config, but there will usually be customisations needed depending on your project's setup.
The CLI can run with zero config, but there will usually be customisations needed depending on your project's setup.
By default, the CLI will look for `typegen.config.js` file in the working directory, exporting an object containing the properties below.

Some options are only available via CLI, some are only available in the config.
Some options are only available via CLI, some are only available in the config.
CLI arguments will always have precedence over config options.

|Option|CLI&nbsp;Argument&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|Type|Default|Description|
Expand Down Expand Up @@ -188,13 +191,14 @@ The `writeTypes` option allows you to tweak what's written to disk. Note that th
By default, interfaces for SQL queries are added to a module at the end of the typescript file they're found in. You can tell the CLI to write the interfaces to a separate file instead using `writeTypes`:

```js
const path = require('path')
const typegen = require('@pgkit/typegen')
const path = require('path')

/** @type {import('@pgkit/typegen').Options} */
module.exports.default = {
writeTypes: typegen.defaultWriteTypes({
queriesPathFromTS: filepath => path.join(path.dirname(filepath), '__sql__', path.basename(filepath)),
queriesPathFromTS: filepath =>
path.join(path.dirname(filepath), '__sql__', path.basename(filepath)),
}),
}
```
Expand All @@ -216,14 +220,18 @@ module.exports.default = {
queries.forEach(query => {
query.fields.forEach(field => {
// add a `_brand` to all string id fields:
if (field.typescript === 'string' && field.column && field.column.name === '.id') {
if (
field.typescript === 'string' &&
field.column &&
field.column.name === '.id'
) {
field.typescript = `(${field.typescript} & { _brand: ${JSON.stringify(field.column)} })`
}
})
})

return typegen.defaultWriteTypes()(queries)
}
},
}
```

Expand All @@ -242,7 +250,7 @@ module.exports.default = {
})

return typegen.defaults.defaultWriteTypes()(queries)
}
},
}
```

Expand Down Expand Up @@ -270,7 +278,7 @@ module.exports.default = {
})

return typegen.defaults.defaultWriteTypes()(queries)
}
},
}
```

Expand All @@ -292,7 +300,7 @@ module.exports.default = {
})

return typegen.defaults.defaultWriteTypes()(queries)
}
},
}
```

Expand All @@ -308,11 +316,14 @@ module.exports.default = {
writeTypes: typegen.defaultWriteTypes({
writeFile: async (filepath, content) => {
content = content
.replace(/declare module queries/g, 'declare module some_other_naming_convention')
.replace(/queries\./g, 'some_other_naming_convention.')
.replaceAll(
'declare module queries',
'declare module some_other_naming_convention',
)
.replaceAll('queries.', 'some_other_naming_convention.')
await typegen.defaults.defaultWriteFile(filepath, content)
},
})
}),
}
```

Expand All @@ -332,20 +343,20 @@ module.exports.default = {
await fs.promises.mkdir(path.dirname(filepath), {recursive: true}) // since you're not using the built-in `writeFile` you should explicitly call mkdir with {recursive: true}
await fs.promises.writeFile(filepath, content)
},
})
}),
}
```

## Enhancing Return Types

Typegen is designed to output types only to the degree it's certain they are correct.
Typegen is designed to output types only to the degree it's certain they are correct.

Let's say in a complex query it can determine that a specific column will return a `string`, but isn't sure if it is also nullable, it will extract the type as `{ column: string | null }`, just to be on the safe side. When it encounters columns where it is unable to even determine the basic type, i.e. `json` columns, it will return :shrug: (Ok, actually the typescript equivalent, which is `unknown`).

In these cases you likely know more about the actual return type than typegen and you might feel the urge to overwrite the types.
In these cases you likely know more about the actual return type than typegen and you might feel the urge to overwrite the types.
Yet you shouldn't touch generated code, as your changes will be removed again on the next run.

Instead what you should do is add (one or more) intersection types to the sql literal, specifying the columns where you want to help typegen out by increasing specificity. The resulting type will be a combination of the extracted types and your enhancements.
Instead what you should do is add (one or more) intersection types to the sql literal, specifying the columns where you want to help typegen out by increasing specificity. The resulting type will be a combination of the extracted types and your enhancements.
Check out the [typescript docs on intersection types](https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types) to learn more.

Imagine this is your code after running typegen.
Expand Down Expand Up @@ -391,8 +402,8 @@ type ResultingType = {
This also means you can make the column `string_col` non-nullable by intersecting it with `{ string_col: string }`.
Note that you can't completely change a property type (say from `string` to `number`) this way.
This is by design, because if you could, a change in the underlying table might cause typegen to detect a new type, which would be ignored, had you overwritten it. This would cause type changes to go unnoticed and we can't have that.
Note that you can't completely change a property type (say from `string` to `number`) this way.
This is by design, because if you could, a change in the underlying table might cause typegen to detect a new type, which would be ignored, had you overwritten it. This would cause type changes to go unnoticed and we can't have that.
With intersections, the resulting property will be of type `never`, when an underlying column type changes. This will alert you to the change, so you can update your manual enhancements.
## Ignoring Queries
Expand Down Expand Up @@ -480,7 +491,7 @@ import {sql} from '@pgkit/client'
sql`this is not even valid SQL!`
```

If you see errors being logged for SQL that you think is valid, feel free to [raise an issue](https://github.com/mmkal/pgkit/issues/new).
If you see errors being logged for SQL that you think is valid, feel free to [raise an issue](https://github.com/mmkal/pgkit/issues/new).
In the meantime, you can use of of the [ignore options](#ignoring-queries) to skip processing the concerned queries.

___
Expand Down

0 comments on commit 69655d9

Please sign in to comment.