Skip to content

Commit 826fd91

Browse files
committed
First Release
1 parent bfcbcbb commit 826fd91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+9215
-1
lines changed

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: node_js
2+
3+
os:
4+
# - windows
5+
- linux
6+
- osx
7+
8+
node_js:
9+
- "8"
10+
- "9"
11+
- "10"
12+
- "11"
13+
14+
script:
15+
- npm run codecov
16+
17+
cache:
18+
directories:
19+
- "node_modules"
20+
21+
notifications:
22+
email:
23+
on_success: never
24+
on_failure: always

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

README.md

Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,191 @@
1-
# foxify-restify-odin
1+
# foxify-restify-odin <!-- omit in toc -->
2+
23
Easily restify odin databases
4+
5+
[![Npm Version](https://img.shields.io/npm/v/foxify-restify-odin.svg)](https://www.npmjs.com/package/foxify-restify-odin)
6+
[![Node Version](https://img.shields.io/node/v/foxify-restify-odin.svg)](https://nodejs.org)
7+
[![TypeScript Version](https://img.shields.io/npm/types/foxify-restify-odin.svg)](https://www.typescriptlang.org)
8+
[![Package Quality](https://npm.packagequality.com/shield/foxify-restify-odin.svg)](https://packagequality.com/#?package=foxify-restify-odin)
9+
[![Npm Total Downloads](https://img.shields.io/npm/dt/foxify-restify-odin.svg)](https://www.npmjs.com/package/foxify-restify-odin)
10+
[![Npm Monthly Downloads](https://img.shields.io/npm/dm/foxify-restify-odin.svg)](https://www.npmjs.com/package/foxify-restify-odin)
11+
[![Open Issues](https://img.shields.io/github/issues-raw/foxifyjs/foxify-restify-odin.svg)](https://github.com/foxifyjs/foxify-restify-odin/issues?q=is%3Aopen+is%3Aissue)
12+
[![Closed Issues](https://img.shields.io/github/issues-closed-raw/foxifyjs/foxify-restify-odin.svg)](https://github.com/foxifyjs/foxify-restify-odin/issues?q=is%3Aissue+is%3Aclosed)
13+
[![Known Vulnerabilities](https://snyk.io/test/github/foxifyjs/foxify-restify-odin/badge.svg?targetFile=package.json)](https://snyk.io/test/github/foxifyjs/foxify-restify-odin?targetFile=package.json)
14+
[![Dependencies Status](https://david-dm.org/foxifyjs/foxify-restify-odin.svg)](https://david-dm.org/foxifyjs/foxify-restify-odin)
15+
[![Pull Requests](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg)](https://github.com/foxifyjs/foxify-restify-odin/pulls)
16+
[![License](https://img.shields.io/github/license/foxifyjs/foxify-restify-odin.svg)](https://github.com/foxifyjs/foxify-restify-odin/blob/master/LICENSE)
17+
[![Build Status](https://api.travis-ci.com/foxifyjs/foxify-restify-odin.svg?branch=master)](https://travis-ci.com/foxifyjs/foxify-restify-odin)
18+
[![Coverage Status](https://codecov.io/gh/foxifyjs/foxify-restify-odin/branch/master/graph/badge.svg)](https://codecov.io/gh/foxifyjs/foxify-restify-odin)
19+
[![Github Stars](https://img.shields.io/github/stars/foxifyjs/foxify-restify-odin.svg?style=social&label=Stars)](https://github.com/foxifyjs/foxify-restify-odin)
20+
[![Github Forks](https://img.shields.io/github/forks/foxifyjs/foxify-restify-odin.svg?style=social&label=Fork)](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+
[![Become A Patron](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/ardalanamini)
190+
191+
[![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/ardalanamini)

dist/builders/filter.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const filter: (model: any, filters: any) => any;
2+
export default filter;

dist/builders/filter.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const BASIC_OPERATORS = {
4+
eq: "=",
5+
gt: ">",
6+
gte: ">=",
7+
lt: "<",
8+
lte: "<=",
9+
ne: "<>",
10+
};
11+
const operate = (query, field, operator, value) => {
12+
switch (operator) {
13+
case "lt":
14+
case "lte":
15+
case "eq":
16+
case "ne":
17+
case "gte":
18+
case "gt":
19+
return query.where(field, BASIC_OPERATORS[operator], value);
20+
case "ex":
21+
if (value === true)
22+
return query.whereNotNull(field);
23+
return query.whereNull(field);
24+
case "in":
25+
return query.whereIn(field, value);
26+
case "nin":
27+
return query.whereNotIn(field, value);
28+
case "bet":
29+
return query.whereBetween(field, value[0], value[1]);
30+
case "nbe":
31+
return query.whereNotBetween(field, value[0], value[1]);
32+
default:
33+
throw new TypeError("Unknown operator");
34+
}
35+
};
36+
const and = (query, filters) => filters
37+
.reduce((prev, curr) => prev.where((q) => filter(q, curr)), query);
38+
const or = (query, filters) => filters.reduce((prev, curr, index) => {
39+
if (index === 0)
40+
return prev.where((q) => filter(q, curr));
41+
return prev.orWhere((q) => filter(q, curr));
42+
}, query);
43+
const filter = (model, filters) => {
44+
if (filters.and) {
45+
if (filters.or)
46+
throw new TypeError("filter can only have one of [\"and\", \"or\"]");
47+
return and(model, filters.and);
48+
}
49+
if (filters.or)
50+
return or(model, filters.or);
51+
return operate(model, filters.field, filters.operator, filters.value);
52+
};
53+
exports.default = filter;

dist/builders/include.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const _default: (model: import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default, relations: string[]) => import("@foxify/odin/dist/base/Query").default<{}, any>;
2+
export default _default;

dist/builders/include.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.default = (model, relations) => model.with(...relations);

dist/builders/limit.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as Odin from "@foxify/odin";
2+
declare const _default: (model: Odin<{}> | (import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default), limit: number) => any;
3+
export default _default;

dist/builders/limit.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.default = (model, limit) => model.limit(limit);

dist/builders/skip.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as Odin from "@foxify/odin";
2+
declare const _default: (model: Odin<{}> | (import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default), skip: number) => any;
3+
export default _default;

0 commit comments

Comments
 (0)