https://github.com/cheplv/typeorm-orderable
Typeorm decorators to make columns orderable on table sql generation
https://github.com/cheplv/typeorm-orderable
columns ordering typeorm
Last synced: 9 months ago
JSON representation
Typeorm decorators to make columns orderable on table sql generation
- Host: GitHub
- URL: https://github.com/cheplv/typeorm-orderable
- Owner: cheplv
- License: mit
- Created: 2024-11-21T20:03:51.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-22T13:02:29.000Z (about 1 year ago)
- Last Synced: 2025-04-14T01:05:08.938Z (9 months ago)
- Topics: columns, ordering, typeorm
- Language: TypeScript
- Homepage:
- Size: 540 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeORM Decorators to provide field ordering in development and generation of migrations
This package solves problem with TypeORM column ordering - take a look:
- [Allow to order columns #541](https://github.com/typeorm/typeorm/issues/541)
- [Set column position/order in addColumn](https://github.com/typeorm/typeorm/issues/6167)



## Usage
1. Run `npm i typeorm-orderable`
2. Add decorators to your classes
With TypeORM your models look like this:
```typescript
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"
import { Orderable, Order } from "typeorm-orderable"
import { instanceToPlain, Exclude } from 'class-transformer';
import { createId } from '@paralleldrive/cuid2';
@Orderable()
export class EntityHelper extends BaseEntity {
@Column()
@Order({ priority: -1 })
_id: string = createId();
@Exclude()
__entity?: string;
@AfterLoad()
setEntityName() {
this.__entity = this.constructor.name;
}
getEntityName() {
return this.__entity;
}
@CreateDateColumn()
@Order({ priority: 100 })
createdAt: Date;
@UpdateDateColumn()
@Order({ priority: 101 })
updatedAt?: Date;
@DeleteDateColumn()
@Order({ priority: 102 })
@Exclude()
deletedAt?: Date;
}
@Entity('examples')
@Orderable()
export class Example extends EntityHelper {
@PrimaryGeneratedColumn()
id: number
@Column()
firstName: string
@Column()
lastName: string
@Column()
age: number
}
```
4. Your fields sequence in table 'example' will look like
_id, id, first_name, last_name, age, created_at, updated_at, deleted_at
5. optionally @Order directive supports "before" and "after" options with name of column in option property
## To be done
- Define guidelines for testing
## License
This project is licensed under the [MIT](https://github.com/cheplv/typeorm-orderable/blob/master/LICENSE).