Socket
Socket
Sign inDemoInstall

@lomray/typeorm-json-query

Package Overview
Dependencies
437
Maintainers
2
Versions
37
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @lomray/typeorm-json-query

Service for convert JSON query to TypeORM query builder


Version published
Weekly downloads
665
increased by168.15%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

TypeORM JSON Query

Converting JSON query to TypeORM query builder.

npm GitHub GitHub package.json dependency version (dev dep on branch) semantic-release

Quality Gate Status Reliability Rating Security Rating Vulnerabilities Lines of Code Coverage

Install

npm i --save @lomray/typeorm-json-query

Usage

Pass request JSON query to TypeormJsonQuery.

Request:

POST http://127.0.0.1:3000
Content-Type: application/json

{
  "attributes": ["id", "param"], // or empty for select all
  "orderBy": { "id": "DESC", { "param": { "order": "ASC", "nulls": "last" } } },
  "relations": ["demo"],
  "where": { "id": 1, "or": [{ "param": "hello" }, { "param": "world" }] },
}

Server implementation:

import TypeormJsonQuery from '@lomray/typeorm-json-query';
import express from 'express';
import { getRepository } from 'typeorm';
import TestEntity from './entities/test-entity';

express()
  .get('/demo-endpoint', (req, res) => {
    const jsonQuery = req.body;
    const typeormQuery = TypeormJsonQuery.init({
      queryBuilder: getRepository(TestEntity).createQueryBuilder(),
      query: jsonQuery,
    });
    
    console.log(typeormQuery.toQuery().getSql());

    res.send('Ok.');
  });

Also, you can use IJsonQuery interface for support build JSON query on client side:

import { IJsonQuery } from '@lomray/typeorm-json-query';
import ITestEntity from './interfaces/i-test-entity';
import axios from 'axios';

const body: IJsonQuery<ITestEntity> = {
  relations: [{ name: 'test', where: { id: 1 } }],
  where: { and: [{ id: { '<': 5 } }, { param: { like: '%hello%' } }] },
};

axios.request({
  method: 'POST',
  body,
});

Check out tests/index-test.ts or src/index.ts for more info.

Keywords

FAQs

Last updated on 13 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc