Skip to content

Commit

Permalink
Merge pull request #3 from rjlopezdev/feat/usage-methods
Browse files Browse the repository at this point in the history
Usage methods [v1.0.1]
  • Loading branch information
rjlopezdev authored Mar 31, 2019
2 parents f71b23b + cf64e12 commit a2f1b9f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 26 deletions.
77 changes: 55 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="logo.jpg" alt="Ngx Guardian logo" width="200" height="200">
<img src="https://raw.githubusercontent.com/rjlopezdev/typeorm-express-query-builder/master/logo.jpg" alt="TypeORM Express Query Builder logo" width="200" height="200">
</p>

<h1 align="center"> TypeORM Express Query Builder </h1>
Expand Down Expand Up @@ -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`
Expand All @@ -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:<br>`+`: Ascendant <br> `-`: Descendant | `order=+foo,-name,+surname`

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
57 changes: 54 additions & 3 deletions test/integration/express/express.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 protected]')
.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 protected]')
.post('/post_urlquery?name=rjlopezdev&[email protected]')
.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({
Expand Down

0 comments on commit a2f1b9f

Please sign in to comment.