Skip to content

foxifyjs/foxify-restify-odin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

foxify-restify-odin

Easily restify odin databases

Npm Version Node Version TypeScript Version Package Quality Npm Total Downloads Npm Monthly Downloads Open Issues Closed Issues Known Vulnerabilities Dependencies Status Pull Requests License Build Status Coverage Status Github Stars Github Forks

Table on Contents

Getting Started

Prerequisites

Installation

npm i -s foxify-restify-odin

Usage

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

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<FilterObject | Filter>;
  or?: Array<FilterObject | Filter>;
  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<RoutesOptions>;
}

restify(model: typeof Odin, options: Partial<restify.Options> = {}): 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 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

qs.stringify({
  filter: {
    field: "username",
    operator: "eq",
    value: "ardalanamini",
  }
})
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<filter object>

possible operators:

lt | lte | eq | ne | gte | gt | exists | in | nin | bet | nbe | like | nlike

include

qs.stringify({
  include: [
    "relation1",
    "relation2.subRelation1.subRelation2",
  ]
})

sort

qs.stringify({
  sort: [
    "field1", // same as "+field1"
    "-field2",
    "+field3",
  ]
})

skip

qs.stringify({
  skip: 100,
})

limit

qs.stringify({
  limit: 10,
})

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Support

If my work helps you, please consider

Become A Patron

Buy Me A Coffee