Package Exports
- @lomray/typeorm-json-query
- @lomray/typeorm-json-query/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@lomray/typeorm-json-query) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
TypeORM JSON Query
Converting JSON query to TypeORM query builder.
Install
npm i --save @lomray/typeorm-json-queryUsage
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.