An open API service indexing awesome lists of open source software.

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

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)

![GitHub](https://img.shields.io/github/license/cheplv/typeorm-orderable?style=flat)
![Build](https://github.com/cheplv/typeorm-orderable/actions/workflows/main.yaml/badge.svg)
![NPMJS](https://github.com/cheplv/typeorm-orderable/actions/workflows/publish.yaml/badge.svg)

## 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).