Skip to content

Latest commit

 

History

History
116 lines (94 loc) · 3.41 KB

File metadata and controls

116 lines (94 loc) · 3.41 KB

@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 with ODataQuery

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');
});

Endpoint with ODataEntity

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');
    });
});