Skip to content

Commit

Permalink
Added defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Nov 15, 2018
1 parent 8a7eca2 commit 79e3ac8
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 11 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Changelog

## Emojis

- New Features -> :zap:
- Enhancements -> :star2:
- Breaking Changes -> :boom:
- Bugs -> :beetle:
- Pull Requests -> :book:
- Documents -> :mortar_board:
- Tests -> :eyeglasses:

---

## [v1.1.0](https://github.com/foxifyjs/foxify-restify-odin/releases/tag/v1.1.0) - *(2018-11-15)*

- :zap: Added ability to set `defaults` for decoded params

## [v1.0.0](https://github.com/foxifyjs/foxify-restify-odin/releases/tag/v1.0.0) - *(2018-11-15)*

- :tada: First Release
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ app.start();

## Documentation

```typescript
type Operator = "lt" | "lte" | "eq" | "ne" | "gte" | "gt" |
"ex" | "in" | "nin" | "bet" | "nbe";

interface FilterObject {
field: string;
operator: Operator;
value: string | number | boolean | any[] | object | Date;
}

interface Filter {
and?: Array<FilterObject | Filter>;
or?: Array<FilterObject | Filter>;
}

interface Query {
filter?: Filter | FilterObject;
include?: string[];
sort?: string[];
skip?: number;
limit?: number;
}

restify(Model: typeof Odin, defaults?: Query): Foxify.Handler;
```

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)

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
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "foxify-restify-odin",
"version": "1.0.0",
"version": "1.1.0",
"description": "Easily restify odin databases",
"author": "Ardalan Amini <[email protected]> [https://github.com/ardalanamini]",
"license": "MIT",
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ namespace restify {
}

export interface Query {
filter: Filter | FilterObject;
include: string[];
sort: string[];
skip: number;
limit: number;
filter?: Filter | FilterObject;
include?: string[];
sort?: string[];
skip?: number;
limit?: number;
}
}

const restify = (model: typeof Odin) => {
const restify = (model: typeof Odin, defaults: restify.Query = {}) => {
if (!(typeof model === typeof Odin)) {
throw new TypeError("Expected model to be a Odin database model");
}
Expand All @@ -37,7 +37,7 @@ const restify = (model: typeof Odin) => {
const foxify_restify_odin: Foxify.Handler = (req, res, next) => {
const parsed = parse((req.url as string).replace(/^.*\?/, ""));

const decoded = decoder(parsed) as restify.Query;
const decoded = Object.assign({}, defaults, decoder(parsed));

req.fro = {
decoded,
Expand Down
144 changes: 144 additions & 0 deletions test/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import * as Odin from "@foxify/odin";
import * as Foxify from "foxify";
import "prototyped.js";
import { stringify } from "qs";
import * as restify from "../src";

declare global {
namespace NodeJS {
interface Global {
__MONGO_DB_NAME__: string;
__MONGO_CONNECTION__: any;
}
}
}

const TABLE = "users";
const ITEMS = [
{
email: "[email protected]",
username: "ardalanamini",
age: 22,
name: {
first: "Ardalan",
last: "Amini",
},
},
{
email: "[email protected]",
username: "john",
age: 45,
name: {
first: "John",
last: "Due",
},
},
];

Odin.connections({
default: {
driver: "MongoDB",
database: global.__MONGO_DB_NAME__,
connection: global.__MONGO_CONNECTION__,
},
});

beforeAll((done) => {
Odin.DB.table(TABLE).insert(ITEMS, (err) => {
if (err) throw err;

Odin.DB.table(TABLE).get((err, items) => {
if (err) throw err;

ITEMS.length = 0;

ITEMS.push(...items);

done();
});
});
});

afterEach((done) => {
Odin.DB.table(TABLE).delete((err) => {
if (err) throw err;

Odin.DB.table(TABLE).insert(ITEMS, (err) => {
if (err) throw err;

done();
});
});
});

afterAll((done) => {
Odin.DB.table(TABLE).delete((err) => {
if (err) throw err;

done();
});
});

interface User extends Odin { }

class User extends Odin {
public static schema = {
email: User.Types.String.email.required,
username: User.Types.String.alphanum.min(3).required,
age: User.Types.Number.min(18).required,
name: {
first: User.Types.String.min(3).required,
last: User.Types.String.min(3),
},
};
}

it("Should limit 1 item by default", async () => {
expect.assertions(2);

const app = new Foxify();

app.get("/users", restify(User, { limit: 1 }), async (req, res) => {
expect(req.fro).toBeDefined();

res.json({
users: await req.fro.query.get(),
total: await req.fro.counter.count(),
});
});

const result = await app.inject(`/users`);

const users = ITEMS.initial();

expect(JSON.parse(result.body))
.toEqual({ users, total: ITEMS.length });
});

it("Should skip 1 item by default and sort by -age", async () => {
expect.assertions(2);

const app = new Foxify();

app.get("/users", restify(User, { skip: 1 }), async (req, res) => {
expect(req.fro).toBeDefined();

res.json({
users: await req.fro.query.get(),
total: await req.fro.counter.count(),
});
});

const result = await app.inject(`/users?${stringify(
{
sort: [
"-age",
],
},
)}`);

const users = ITEMS.orderBy("age", "desc").tail();

expect(JSON.parse(result.body))
.toEqual({ users, total: ITEMS.length });
});
2 changes: 1 addition & 1 deletion test/limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class User extends Odin {
};
}

it("Should limit 1", async () => {
it("Should limit 1 item", async () => {
expect.assertions(2);

const app = new Foxify();
Expand Down
28 changes: 27 additions & 1 deletion test/skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class User extends Odin {
};
}

it("Should skip 1", async () => {
it("Should skip 1 item", async () => {
expect.assertions(2);

const app = new Foxify();
Expand All @@ -118,3 +118,29 @@ it("Should skip 1", async () => {
expect(JSON.parse(result.body))
.toEqual({ users, total: ITEMS.length });
});

it("Should skip 0 item", async () => {
expect.assertions(2);

const app = new Foxify();

app.get("/users", restify(User), async (req, res) => {
expect(req.fro).toBeDefined();

res.json({
users: await req.fro.query.get(),
total: await req.fro.counter.count(),
});
});

const result = await app.inject(`/users?${stringify(
{
skip: 0,
},
)}`);

const users = ITEMS;

expect(JSON.parse(result.body))
.toEqual({ users, total: ITEMS.length });
});

0 comments on commit 79e3ac8

Please sign in to comment.