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.
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
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>;
}
Leverage @bleed-believer/kendo-grid-server
to handle query parameters for filtering, sorting, and pagination directly in your Express route:
import { ODataQuery } 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 ODataQuery(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');
});
Here's an example demonstrating how to use the ODataEntity
class for handling query parameters:
import { ODataEntity } from '@bleed-believer/kendo-grid-server';
import { DataSource } from 'typeorm';
import express from 'express';
import { Dummy } from '@entities/dummy.entity.js';
const app = express();
const dataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'user',
password: 'password',
database: 'test',
entities: [Dummy],
synchronize: true,
});
dataSource.initialize().then(() => {
app.get('/dummy', async (req, res) => {
const odataEntity = new ODataEntity(Dummy, dataSource, req);
const result = await odataEntity.getMany({});
res.contentType('json');
res.end(JSON.stringify(result));
});
app.listen(8080, () => {
console.log('Server is ready and listening on port 8080');
});
});