https://github.com/foxifyjs/foxify-restify-odin
Easily restify odin databases
https://github.com/foxifyjs/foxify-restify-odin
filter foxify middleware odin restify sort
Last synced: 2 months ago
JSON representation
Easily restify odin databases
- Host: GitHub
- URL: https://github.com/foxifyjs/foxify-restify-odin
- Owner: foxifyjs
- License: mit
- Created: 2018-11-14T23:19:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T17:44:01.000Z (over 2 years ago)
- Last Synced: 2024-10-29T19:03:59.574Z (8 months ago)
- Topics: filter, foxify, middleware, odin, restify, sort
- Language: TypeScript
- Size: 967 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# foxify-restify-odin
Easily restify odin databases
[](https://www.npmjs.com/package/foxify-restify-odin)
[](https://nodejs.org)
[](https://www.typescriptlang.org)
[](https://packagequality.com/#?package=foxify-restify-odin)
[](https://www.npmjs.com/package/foxify-restify-odin)
[](https://www.npmjs.com/package/foxify-restify-odin)
[](https://github.com/foxifyjs/foxify-restify-odin/issues?q=is%3Aopen+is%3Aissue)
[](https://github.com/foxifyjs/foxify-restify-odin/issues?q=is%3Aissue+is%3Aclosed)
[](https://snyk.io/test/github/foxifyjs/foxify-restify-odin?targetFile=package.json)
[](https://david-dm.org/foxifyjs/foxify-restify-odin)
[](https://github.com/foxifyjs/foxify-restify-odin/pulls)
[](https://github.com/foxifyjs/foxify-restify-odin/blob/master/LICENSE)
[](https://travis-ci.com/foxifyjs/foxify-restify-odin)
[](https://codecov.io/gh/foxifyjs/foxify-restify-odin)
[](https://github.com/foxifyjs/foxify-restify-odin)
[](https://github.com/foxifyjs/foxify-restify-odin)## Table on Contents
- [Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Usage](#usage)
- [Documentation](#documentation)
- [Filters](#filters)
- [include](#include)
- [sort](#sort)
- [skip](#skip)
- [limit](#limit)
- [Versioning](#versioning)
- [Authors](#authors)
- [License](#license)
- [Support](#support)## Getting Started
### Prerequisites
- [Node.js](https://nodejs.org/en/download) `8.12` or higher is required.
- [foxify](https://github.com/foxifyjs/foxify) `0.10.20` or higher is required.
- [@foxify/odin](https://github.com/foxifyjs/odin) `0.8.0` or higher is required.### Installation
```bash
npm i -s foxify-restify-odin
```### Usage
```javascript
const Foxify = require('foxify');
const restify = require('foxify-restify-odin');
const User = require('./models/User');let app = new Foxify();
app.get('/users', restify(User), async (req, res) => {
res.json({
users: await req.fro.query.get(),
total_users: await req.fro.counter.count(),
});
});app.start();
```## Documentation
```typescript
type Operator = "lt" | "lte" | "eq" | "ne" | "gte" | "gt" |
"exists" | "in" | "nin" | "bet" | "nbe" | "like" | "nlike";interface FilterObject {
field: string;
operator: Operator;
value: string | number | boolean | any[] | object | Date;
}interface Filter {
and?: Array;
or?: Array;
has?: string | { relation: string, filter: Filter | FilterObject };
}interface Query {
filter?: Filter | FilterObject;
include?: string[];
sort?: string[];
skip?: number;
limit?: number;
}interface RouteOptions {
pre?: Foxify.Handler | Foxify.Handler[];
post?: Foxify.Handler | Foxify.Handler[];
}interface RoutesOptions {
index: RouteOptions & { lean?: boolean; } | false;
count: RouteOptions | false;
store: RouteOptions | false;
show: RouteOptions | false;
update: RouteOptions | false;
delete: RouteOptions | false;
}interface Options {
name: string;
prefix: string;
qs: QSParseOptions;
defaults: Query;
pre: Foxify.Handler | Foxify.Handler[];
routes: Partial;
}restify(model: typeof Odin, options: Partial = {}): Router;
```This module's 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)
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
Lastly it passes the a `decoded` key in `req.fro` which is the parsed query string that is used in the middleware
**Stringify all query params using [qs](https://www.npmjs.com/package/qs) default options**
All the possible query modifiers are explained as a single modification but they all can be used together
`/users?sort%5B0%5D=age`
### Filters
```javascript
qs.stringify({
filter: {
field: "username",
operator: "eq",
value: "ardalanamini",
}
})
``````javascript
qs.stringify({
filter: {
or: [
{
field: "username",
operator: "eq",
value: "ardalanamini",
},
{
and: [
{
field: "age",
operator: "gte",
value: 18,
},
{
field: "email",
operator: "exists",
value: true,
},
],
},
],
},
})
```filter can be a single filter object or `and`/`or` of Array\
possible operators:
`lt` | `lte` | `eq` | `ne` | `gte` | `gt` | `exists` | `in` | `nin` | `bet` | `nbe` | `like` | `nlike`
### include
```javascript
qs.stringify({
include: [
"relation1",
"relation2.subRelation1.subRelation2",
]
})
```### sort
```javascript
qs.stringify({
sort: [
"field1", // same as "+field1"
"-field2",
"+field3",
]
})
```### skip
```javascript
qs.stringify({
skip: 100,
})
```### limit
```javascript
qs.stringify({
limit: 10,
})
```## Versioning
We use [SemVer](http://semver.org) for versioning. For the versions available, see the [tags on this repository](https://github.com/foxifyjs/foxify-restify-odin/tags).
## Authors
- **Ardalan Amini** - *Owner/Developer* - [@ardalanamini](https://github.com/ardalanamini)
See also the list of [contributors](https://github.com/foxifyjs/foxify-restify-odin/contributors) who participated in this project.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
## Support
If my work helps you, please consider
[](https://www.patreon.com/ardalanamini)
[](https://www.buymeacoffee.com/ardalanamini)