# @lomray/typeorm-json-query

> Service for convert JSON query to TypeORM query builder

Latest version **2.7.0** (published 2023-12-13) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @lomray/typeorm-json-query
pnpm add @lomray/typeorm-json-query
yarn add @lomray/typeorm-json-query
bun add @lomray/typeorm-json-query
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.7.0 |
| Published | 2023-12-13 |
| First published | 2022-01-08 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 67.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Mikhail Yarmaliuk |
| Maintainers | matthew_patell, danial031193 |
| Keywords | js, service, helpers, json, typeorm, typescript, convert |

## Links

- npm: https://www.npmjs.com/package/@lomray/typeorm-json-query
- Repository: https://github.com/Lomray-Software/typeorm-json-query
- Issues: https://github.com/Lomray-Software/typeorm-json-query/issues
- npm.io page: https://npm.io/package/@lomray/typeorm-json-query

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) *
- [@lomray/microservices-types](https://npm.io/package/@lomray/microservices-types.md) ^1.15.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.7.0 (latest) — 2023-12-13
- 2.6.0 — 2023-12-13
- 2.5.5 — 2023-11-28
- 2.5.4 — 2023-07-28
- 2.5.3 — 2023-05-24
- 2.5.2 — 2023-05-15
- 2.5.1 — 2023-03-17
- 2.5.0 — 2023-03-16
- 2.4.0 — 2023-03-16
- 2.3.1 — 2023-03-02
- 2.3.0 — 2023-02-28
- 2.2.0 — 2023-02-09
- 2.1.2 — 2023-02-08
- 2.1.1 — 2023-02-08
- 2.1.0 — 2023-02-08
- … 22 more at https://npm.io/package/@lomray/typeorm-json-query/versions

## README

# TypeORM JSON Query

### Converting JSON query to TypeORM query builder.

![npm](https://img.shields.io/npm/v/@lomray/typeorm-json-query)
![GitHub](https://img.shields.io/github/license/Lomray-Software/typeorm-json-query)
![GitHub package.json dependency version (dev dep on branch)](https://img.shields.io/github/package-json/dependency-version/Lomray-Software/typeorm-json-query/dev/typescript/master)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Lomray-Software_typeorm-json-query&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Lomray-Software_typeorm-json-query)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=Lomray-Software_typeorm-json-query&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=Lomray-Software_typeorm-json-query)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=Lomray-Software_typeorm-json-query&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=Lomray-Software_typeorm-json-query)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=Lomray-Software_typeorm-json-query&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=Lomray-Software_typeorm-json-query)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=Lomray-Software_typeorm-json-query&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=Lomray-Software_typeorm-json-query)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=Lomray-Software_typeorm-json-query&metric=coverage)](https://sonarcloud.io/summary/new_code?id=Lomray-Software_typeorm-json-query)

## Install
```bash
npm i --save @lomray/typeorm-json-query
```

## Usage

Pass request JSON query to `TypeormJsonQuery`.

__Request:__
```http 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:__
```typescript
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:__ 
```typescript
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.

---
_Source: https://npm.io/package/@lomray/typeorm-json-query · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
