typeorm-express-query-filter
v0.0.13
Published
Filter models, load relations from express req.query
Downloads
5
Maintainers
Readme
typeorm-express-query-filter
Installation
npm i typeorm-express-query-filter
Usage:
- Extend class
QueryableRepository\<Entity\>
- Call function
findWithPagination(req, res, findOptions<Entity>)
- req - express request
- res - express response
- findOptions - typeorm find options object (optional)
Example model:
{
@PrimaryGeneratedColumn()
public id: number;
@Column()
public firstName: string;
@Column()
public lastName: string;
@ManyToMany(() => Roles)
@JoinTable({name: 'user_role'})
public roles: Role[];
@Column('date)
public createdAt: Date;
@OneToMany(() => Phone)
public phones: Phone[];
}
Example:
GET URL: /api/users?firstName.contains=john&phones.equals=phoneId&roles.in=${roleId}&page=0&size=20&relations=roles&sort=createdAt-
const roleId=1; // role id
const phoneId=1; // phone id
relations=roles; // will fetch user roles relation
page=0; // first page
size=20; // page size
sort=createdAt-; // sort by createdAt (-) descending
firstName.contains=john; // get all users with firstName containing 'john'
phones.equals=phoneId; // get user by phone id
In response headers under X-Total-Count
will be pagination total count.
build:
- npm run-script build