@bleed-believer/kendo-grid-server
This library is designed to seamlessly work with TypeORM, applying filtering, sorting, and pagination to a SelectQueryBuilder
instance based on query strings generated by @bleed-believer/kendo-grid-client
. It serves as a powerful backend companion, enabling dynamic data retrieval that's perfectly aligned with frontend requests.
Installation
First, ensure that typeorm
is installed in your project:
npm i --save typeorm
Then, install this library to integrate advanced query manipulation capabilities:
npm i --save @bleed-believer/kendo-grid-server
Usage Example
Entities
Define your entities as you normally would with TypeORM. Here's an example with Category
and Dummy
entities:
import { BaseEntity, Column, Entity, OneToMany, PrimaryGeneratedColumn, ManyToOne, Relation } from 'typeorm';
@Entity({ name: 'Category' })
export class Category extends BaseEntity {
@PrimaryGeneratedColumn({ type: 'int' })
id!: number;
@Column({ type: 'varchar' })
cod!: string;
@Column({ type: 'nvarchar' })
descript!: string;
@OneToMany(() => Dummy, dummy => dummy.category)
dummies?: Relation<Dummy[]>;
}
@Entity({ name: 'Dummy' })
export class Dummy extends BaseEntity {
@PrimaryGeneratedColumn({ type: 'int' })
id!: number;
@Column({ type: 'nvarchar', length: 512 })
text!: string;
@Column({ type: 'numeric', precision: 18, scale: 2 })
value!: number;
@Column({ type: 'date' })
date!: Date;
@ManyToOne(() => Category, category => category.dummies)
category?: Relation<Category>;
}
Endpoint
Leverage @bleed-believer/kendo-grid-server
to handle query parameters for filtering, sorting, and pagination directly in your Express route:
import { OData } from '@bleed-believer/kendo-grid-server';
import { Dummy } from '@entities/dummy.entity.js';
import express from 'express';
const app = express();
app.get('/dummy', async (req, res) => {
// This is the query builder to be enhanced
const query = Dummy
.createQueryBuilder('dummy')
.innerJoinAndSelect('dummy.category', 'category');
const odata = new OData(query, req, {
date: v => v != null ? new Date(v) : v
});
const result = await odata.getRawMany();
res.contentType('json');
res.end(JSON.stringify(result));
});
app.listen(8080, () => {
console.log('Server is ready and listening on port 8080');
});