|
1 | | -# foxify-restify-odin |
| 1 | +# foxify-restify-odin <!-- omit in toc --> |
| 2 | + |
2 | 3 | Easily restify odin databases |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/foxify-restify-odin) |
| 6 | +[](https://nodejs.org) |
| 7 | +[](https://www.typescriptlang.org) |
| 8 | +[](https://packagequality.com/#?package=foxify-restify-odin) |
| 9 | +[](https://www.npmjs.com/package/foxify-restify-odin) |
| 10 | +[](https://www.npmjs.com/package/foxify-restify-odin) |
| 11 | +[](https://github.com/foxifyjs/foxify-restify-odin/issues?q=is%3Aopen+is%3Aissue) |
| 12 | +[](https://github.com/foxifyjs/foxify-restify-odin/issues?q=is%3Aissue+is%3Aclosed) |
| 13 | +[](https://snyk.io/test/github/foxifyjs/foxify-restify-odin?targetFile=package.json) |
| 14 | +[](https://david-dm.org/foxifyjs/foxify-restify-odin) |
| 15 | +[](https://github.com/foxifyjs/foxify-restify-odin/pulls) |
| 16 | +[](https://github.com/foxifyjs/foxify-restify-odin/blob/master/LICENSE) |
| 17 | +[](https://travis-ci.com/foxifyjs/foxify-restify-odin) |
| 18 | +[](https://codecov.io/gh/foxifyjs/foxify-restify-odin) |
| 19 | +[](https://github.com/foxifyjs/foxify-restify-odin) |
| 20 | +[](https://github.com/foxifyjs/foxify-restify-odin) |
| 21 | + |
| 22 | +## Table on Contents <!-- omit in toc --> |
| 23 | + |
| 24 | +- [Getting Started](#getting-started) |
| 25 | + - [Prerequisites](#prerequisites) |
| 26 | + - [Installation](#installation) |
| 27 | + - [Usage](#usage) |
| 28 | +- [Documentation](#documentation) |
| 29 | + - [Filters](#filters) |
| 30 | + - [include](#include) |
| 31 | + - [sort](#sort) |
| 32 | + - [skip](#skip) |
| 33 | + - [limit](#limit) |
| 34 | +- [Versioning](#versioning) |
| 35 | +- [Authors](#authors) |
| 36 | +- [License](#license) |
| 37 | +- [Support](#support) |
| 38 | + |
| 39 | +## Getting Started |
| 40 | + |
| 41 | +### Prerequisites |
| 42 | + |
| 43 | +- [Node.js](https://nodejs.org/en/download) `8.12` or higher is required. |
| 44 | +- [foxify](https://github.com/foxifyjs/foxify) `0.10.14` or higher is required. |
| 45 | +- [@foxify/odin](https://github.com/foxifyjs/odin) `0.2.2` or higher is required. |
| 46 | + |
| 47 | +### Installation |
| 48 | + |
| 49 | +```bash |
| 50 | +npm i -s foxify-restify-odin |
| 51 | +``` |
| 52 | + |
| 53 | +### Usage |
| 54 | + |
| 55 | +```javascript |
| 56 | +const Foxify = require('foxify'); |
| 57 | +const restify = require('foxify-restify-odin'); |
| 58 | +const User = require('./models/User'); |
| 59 | + |
| 60 | +let app = new Foxify(); |
| 61 | + |
| 62 | +app.get('/users', restify(User), async (req, res) => { |
| 63 | + res.json({ |
| 64 | + users: await req.fro.query.get(), |
| 65 | + total_users: await req.fro.counter.count(), |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +app.start(); |
| 70 | +``` |
| 71 | + |
| 72 | +## Documentation |
| 73 | + |
| 74 | +This middleware parses url query string and executes a query on the given model accordingly and passes the `query` to you (since you might need to do some modifications on the query, too) |
| 75 | + |
| 76 | +It also passes a `counter` which is exactly like `query` but without applying `skip`, `limit`, `sort` just because you might want to send a total count in your response as well |
| 77 | + |
| 78 | +Lastly it passes the a `decoded` key in `req.fro` which is the parsed query string that is used in the middleware |
| 79 | + |
| 80 | +**Stringify all query params using [qs](https://www.npmjs.com/package/qs) default options** |
| 81 | + |
| 82 | +All the possible query modifiers are explained as a single modification but they all can be used together |
| 83 | + |
| 84 | +`/users?sort%5B0%5D=age` |
| 85 | + |
| 86 | +### Filters |
| 87 | + |
| 88 | +```javascript |
| 89 | +qs.stringify({ |
| 90 | + filter: { |
| 91 | + field: "username", |
| 92 | + operator: "eq", |
| 93 | + value: "ardalanamini", |
| 94 | + } |
| 95 | +}) |
| 96 | +``` |
| 97 | + |
| 98 | +```javascript |
| 99 | +qs.stringify({ |
| 100 | + filter: { |
| 101 | + or: [ |
| 102 | + { |
| 103 | + field: "username", |
| 104 | + operator: "eq", |
| 105 | + value: "ardalanamini", |
| 106 | + }, |
| 107 | + { |
| 108 | + and: [ |
| 109 | + { |
| 110 | + field: "age", |
| 111 | + operator: "gte", |
| 112 | + value: 18, |
| 113 | + }, |
| 114 | + { |
| 115 | + field: "email", |
| 116 | + operator: "ex", |
| 117 | + value: true, |
| 118 | + }, |
| 119 | + ], |
| 120 | + }, |
| 121 | + ], |
| 122 | + }, |
| 123 | +}) |
| 124 | +``` |
| 125 | + |
| 126 | +filter can be a single filter object or `and`/`or` of Array\<filter object\> |
| 127 | + |
| 128 | +possible operators: |
| 129 | + |
| 130 | +`lt` | `lte` | `eq` | `ne` | `gte` | `gt` | `ex` | `in` | `nin` | `bet` | `nbe` |
| 131 | + |
| 132 | +### include |
| 133 | + |
| 134 | +```javascript |
| 135 | +qs.stringify({ |
| 136 | + include: [ |
| 137 | + "relation1", |
| 138 | + "relation2", |
| 139 | + ] |
| 140 | +}) |
| 141 | +``` |
| 142 | + |
| 143 | +### sort |
| 144 | + |
| 145 | +```javascript |
| 146 | +qs.stringify({ |
| 147 | + sort: [ |
| 148 | + "field1", // same as "+field1" |
| 149 | + "-field2", |
| 150 | + "+field3", |
| 151 | + ] |
| 152 | +}) |
| 153 | +``` |
| 154 | + |
| 155 | +### skip |
| 156 | + |
| 157 | +```javascript |
| 158 | +qs.stringify({ |
| 159 | + skip: 100, |
| 160 | +}) |
| 161 | +``` |
| 162 | + |
| 163 | +### limit |
| 164 | + |
| 165 | +```javascript |
| 166 | +qs.stringify({ |
| 167 | + limit: 10, |
| 168 | +}) |
| 169 | +``` |
| 170 | + |
| 171 | +## Versioning |
| 172 | + |
| 173 | +We use [SemVer](http://semver.org) for versioning. For the versions available, see the [tags on this repository](https://github.com/foxifyjs/foxify/tags). |
| 174 | + |
| 175 | +## Authors |
| 176 | + |
| 177 | +- **Ardalan Amini** - *Owner/Developer* - [@ardalanamini](https://github.com/ardalanamini) |
| 178 | + |
| 179 | +See also the list of [contributors](https://github.com/foxifyjs/foxify/contributors) who participated in this project. |
| 180 | + |
| 181 | +## License |
| 182 | + |
| 183 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details |
| 184 | + |
| 185 | +## Support |
| 186 | + |
| 187 | +If my work helps you, please consider |
| 188 | + |
| 189 | +[](https://www.patreon.com/ardalanamini) |
| 190 | + |
| 191 | +[](https://www.buymeacoffee.com/ardalanamini) |
0 commit comments