From e78c009711ee7b7294bff2d4dd1845afd0df4752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Juli=C3=A1n=20L=C3=B3pez=20Ca=C3=B1a?= Date: Sun, 31 Mar 2019 21:57:36 +0200 Subject: [PATCH 1/2] test(express): add integration test to confirm it works with POST method & improve doc --- README.md | 77 +++++++++++++++++------- package.json | 1 + test/integration/express/express.spec.ts | 57 +++++++++++++++++- 3 files changed, 110 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 8b64572..1298187 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- Ngx Guardian logo + TypeORM Express Query Builder logo

TypeORM Express Query Builder

@@ -45,14 +45,12 @@ Use QueryBuilder export from package and pass your `req.query` as an argument: ```typescript import QueryBuilder from 'typeorm-express-query-builder'; -const builder = QueryBuilder(req.query); +const builder = new QueryBuilder(req.query); const builtQuery = builder.build(); // Now your query is built, pass it to your TypeORM repository const results = await fooRepository.find(builtQuery); ``` -## Building queries from url - Given the following url query string: `foo/?name__contains=foo&role__in=admin,common&age__gte=18&page=3&limit=10` @@ -71,25 +69,60 @@ It will be transformed into: } ``` +## Different ways of retrieve data + +### GET, POST method by url query string + +`GET foo/?name__contains=foo&role__in=admin,common&age__gte=18&page=3&limit=10` + +`POST foo/?name__contains=foo&role__in=admin,common&age__gte=18&page=3&limit=10` +```javascript +app.get('/foo', (req, res) => { + const queryBuilder = new QueryBuilder(req.query); // => Parsed into req.query + const built = queryBuilder.build(); +}) +``` + +### POST method by body + +```javascript +POST foo/, body: { + "name__contains": "foo", + "role__in": "admin,common", + "age__gte": 18, + "page": 3, + "limit": 10 +} +``` + +```javascript +app.post('/foo', (req, res) => { + const queryBuilder = new QueryBuilder(req.body); // => Parsed into req.body + const built = queryBuilder.build(); +}) +``` + ## Available Lookups | Lookup | Behaviour | Example | | --- | --- | --- | -_(none)_ | Return entries that match with value | `?foo=raul` -__contains__ | Return entries that contains value | `?foo__contains=lopez` -__startswith__ | Return entries that starts with value | `?foo__startswith=r` -__endswith__ | Return entries that ends with value | `?foo__endswith=dev` -__isnull__ | Return entries with null value | `?foo__isnull` -__lt__ | Return entries with value less than or equal to provided | `?foo__lt=18` -__lte__ | Return entries with value less than provided | `?foo__lte=18` -__gt__ | Returns entries with value greater tahn provided | `?foo__gt=18` -__gte__ | Return entries with value greater then or equal to provided | `?foo__gte=18` -__in__ | Return entries that match with values in list | `?foo__in=admin,common` -__between__ | Return entries in range | `?foo__between=1,27` - -## Extra options - -| Option | Default | Behaviour | -| --- | :---: | --- | -page | __1__ | Entries page -limit | __25__ | Page size +_(none)_ | Return entries that match with value | `foo=raul` +__contains__ | Return entries that contains value | `foo__contains=lopez` +__startswith__ | Return entries that starts with value | `foo__startswith=r` +__endswith__ | Return entries that ends with value | `foo__endswith=dev` +__isnull__ | Return entries with null value | `foo__isnull` +__lt__ | Return entries with value less than or equal to provided | `foo__lt=18` +__lte__ | Return entries with value less than provided | `foo__lte=18` +__gt__ | Returns entries with value greater than provided | `foo__gt=18` +__gte__ | Return entries with value greater than or equal to provided | `foo__gte=18` +__in__ | Return entries that match with values in list | `foo__in=admin,common` +__between__ | Return entries in range | `foo__between=1,27` + +## Options + +| Option | Default | Behaviour | Example | +| --- | :---: | --- | --- | +page | __1__ | Return entries for page `page` | `page=2` +limit | __25__ | Return entries for page `page` paginated by size `limit` | `limit=15` +order | - | Order for fields:
`+`: Ascendant
`-`: Descendant | `order=+foo,-name,+surname` + diff --git a/package.json b/package.json index efc3d64..5ae35a2 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "license": "MIT", "devDependencies": { "@types/jest": "^24.0.11", + "body-parser": "^1.18.3", "codecov": "^3.2.0", "express": "^4.16.4", "jest": "^24.5.0", diff --git a/test/integration/express/express.spec.ts b/test/integration/express/express.spec.ts index c767f67..e3a47a3 100644 --- a/test/integration/express/express.spec.ts +++ b/test/integration/express/express.spec.ts @@ -1,5 +1,6 @@ import express = require('express'); import request = require('supertest'); +import bodyParser = require('body-parser'); import { QueryBuilder } from '../../../src/query-builder'; import { Like } from 'typeorm'; @@ -9,19 +10,69 @@ describe('Test Express integration', () => { beforeAll((done) => { let app = express(); - app.get('/foo', (req, res) => { + app.use(bodyParser.json()); + app.use(bodyParser.urlencoded({extended: true})); + app.get('/get', (req, res) => { const queryBuilder = new QueryBuilder(req.query); const built = queryBuilder.build(); res.send(built); }); + app.post('/post_urlquery', (req, res) => { + const queryBuilder = new QueryBuilder(req.query); + const built = queryBuilder.build(); + res.send(built); + }); + app.post('/post_body', (req, res) => { + const queryBuilder = new QueryBuilder(req.body); + const built = queryBuilder.build(); + res.send(built); + }); server = app.listen(3000, () => { done(); }); }) - it('should return an appropiate query built', (done) => { + it('should return an appropiate query built for GET /get?...', (done) => { + request(server) + .get('/get?name=rjlopezdev&email__contains=@gmail.com') + .expect(200) + .end((err, res) => { + expect(JSON.parse(res.text)).toEqual({ + where: { + name: 'rjlopezdev', + email: Like('%@gmail.com%') + }, + skip: 0, + take: 25 + }); + done(); + }) + }) + + it('should return an appropiate query built for POST /post_urlquery?...', (done) => { request(server) - .get('/foo?name=rjlopezdev&email__contains=@gmail.com') + .post('/post_urlquery?name=rjlopezdev&email__contains=@gmail.com') + .expect(200) + .end((err, res) => { + expect(JSON.parse(res.text)).toEqual({ + where: { + name: 'rjlopezdev', + email: Like('%@gmail.com%') + }, + skip: 0, + take: 25 + }); + done(); + }) + }) + + it('should return an appropiate query built for POST /post_body, body: {...}', (done) => { + request(server) + .post('/post_body') + .send({ + name: 'rjlopezdev', + email__contains: '@gmail.com' + }) .expect(200) .end((err, res) => { expect(JSON.parse(res.text)).toEqual({ From cf64e124c77dbd194fc20f0d1a81b6c65160f5e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Juli=C3=A1n=20L=C3=B3pez=20Ca=C3=B1a?= Date: Sun, 31 Mar 2019 21:59:10 +0200 Subject: [PATCH 2/2] release: 1.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5ae35a2..8a82262 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typeorm-express-query-builder", - "version": "1.0.0", + "version": "1.0.1", "description": "Easily transform an Express req.query into TypeORM query", "main": "index.js", "scripts": {