Skip to content

Commit

Permalink
Add customized example
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel7grant committed Dec 6, 2024
1 parent 3f8ae33 commit 1aa1c20
Show file tree
Hide file tree
Showing 8 changed files with 2,883 additions and 2 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ jobs:
run: npm test
- name: Build TypeScript files
run: npm run build
- name: Test ESLint 9 example for compatibility
- name: Test ESLint 9 recommended example for compatibility
run: |
cd examples/recommended
npm ci
(npm run lint || true) | grep -aq "Type of name in User is not matching the TypeORM column type (expected type: string)"
- name: Test ESLint 9 customized example for compatibility
run: |
cd examples/customized
npm ci
(npm run lint || true) | grep -aq "The nullability of name in User should be set to the default value"
- name: Test legacy example for compatibility
run: |
# Workaround until they fix scoping
# Workaround for scoping preferring new config files
mv eslint.config.mjs _eslint.config.mjs
cd examples/legacy
npm ci
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Changed

- Fix circular dependecy issue between plugin and recommended, making customization fail
- Add `customized` example (with tests) to display the customization options
- **Breaking**: `bigint` and `decimal` are now parsed correctly according to the driver ([#5](https://github.com/daniel7grant/eslint-plugin-typeorm-typescript/issues/5#issuecomment-2452988205))
- There is new option `driver` for `enforce-column-types`: can be 'postgres', 'mysql' or 'sqlite'
- If the driver is empty (default) or set to MySQL and PostgreSQL, bigint and decimal are parsed to be strings
Expand Down
46 changes: 46 additions & 0 deletions examples/customized/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Flat configuration

This is the new way to configure ESLint ("flat configuration"). Supported from ESLint >= 9.

## Usage

Install the package for the plugin:

```sh
npm install -D eslint-plugin-typeorm-typescript
```

Add the recommended configuration to `eslint.config.mjs`:

```js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import typeormTypescriptRecommended from 'eslint-plugin-typeorm-typescript/recommended';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
typeormTypescriptRecommended,
);
```

If you want to change the options, enable the plugin and the rules manually:

```js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import typeormTypescriptPlugin from 'eslint-plugin-typeorm-typescript';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
{
plugins: {'typeorm-typescript': typeormTypescriptPlugin},
rules: {
"typeorm-typescript/enforce-column-types": "error",
"typeorm-typescript/enforce-relation-types": "warn",
"typeorm-typescript/enforce-consistent-nullability": ["error", { "specifyNullable": "always" }]
}
}
);
```
28 changes: 28 additions & 0 deletions examples/customized/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import typeormTypescriptPlugin from 'eslint-plugin-typeorm-typescript';

export default tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended, {
plugins: {
'typeorm-typescript': typeormTypescriptPlugin,
},
rules: {
'typeorm-typescript/enforce-column-types': [
'error',
// It will check bigint and decimal correctly
{ driver: 'sqlite' },
],
'typeorm-typescript/enforce-relation-types': [
'error',
// It will force Relation<...> wrappers
{ specifyRelation: 'always' }
],
'typeorm-typescript/enforce-consistent-nullability': [
'error',
// It will force nullable everywhere
{ specifyNullable: 'always' },
],
},
});
25 changes: 25 additions & 0 deletions examples/customized/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Entity, Column, OneToMany, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: 'number' })
userId: number;
}

@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: 'string' })
name: number;

@Column({ type: 'decimal' })
wage: string;

@OneToMany(() => Post, (post) => post.userId)
posts: Post;
}
Loading

0 comments on commit 1aa1c20

Please sign in to comment.