94 lines
1.4 KiB
Markdown
94 lines
1.4 KiB
Markdown
---
|
|
tags: repos
|
|
templateEngineOverride: md
|
|
title: "json-query-chain"
|
|
description: Chain queries onto POJOs to return precise results.
|
|
date: 2018-02-23
|
|
---
|
|
|
|
[Fork on Gitlab](https://gitlab.com/gaiety/json-query-chain)
|
|
|
|
[](https://badge.fury.io/js/json-query-chain)
|
|
|
|
100% test covered and developed with Test-Driven Development.
|
|
|
|
## Usage
|
|
|
|
```javascript
|
|
import Query from 'json-query-chain';
|
|
|
|
let myQ = new Query(someJsonData)
|
|
.search('isActiveUser', true)
|
|
.results;
|
|
```
|
|
|
|
### Chainable Methods
|
|
|
|
#### Search
|
|
|
|
Currently supports booleans and strings. (See [#1](https://github.com/hergaiety/json-query-chain/issues/1) for Integer Support)
|
|
|
|
##### By Boolean
|
|
|
|
```javascript
|
|
.search('isActiveUser', true)
|
|
```
|
|
|
|
##### By String
|
|
|
|
```javascript
|
|
.search('name', 'steele')
|
|
```
|
|
|
|
#### Filter
|
|
|
|
Simpler version of search using a custom function in the chain.
|
|
|
|
```javascript
|
|
.filter(a => a.age >= 21)
|
|
```
|
|
|
|
##### By Key
|
|
|
|
```javascript
|
|
.filterBy('age', x => x >= 21)
|
|
```
|
|
|
|
#### Sort
|
|
|
|
##### By Boolean
|
|
|
|
```javascript
|
|
.sort('isActiveUser', true)
|
|
```
|
|
|
|
##### By String
|
|
|
|
```javascript
|
|
.sort('name')
|
|
```
|
|
|
|
##### By Number
|
|
|
|
```javascript
|
|
.sort('netWorth')
|
|
```
|
|
|
|
#### Pagination
|
|
|
|
Page 1 with 5 results per page.
|
|
|
|
```javascript
|
|
.paginate(1, 5)
|
|
```
|
|
|
|
Page 2 wtih default of 10 results per page.
|
|
|
|
```javascript
|
|
.paginate(2)
|
|
```
|
|
|
|
## Tests
|
|
|
|
`npm test` runs tests through Jest
|
|
|