项目作者: foxifyjs

项目描述 :
Easily restify odin databases
高级语言: TypeScript
项目地址: git://github.com/foxifyjs/foxify-restify-odin.git
创建时间: 2018-11-14T23:19:30Z
项目社区:https://github.com/foxifyjs/foxify-restify-odin

开源协议:MIT License

下载


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

  1. npm i -s foxify-restify-odin

Usage

  1. const Foxify = require('foxify');
  2. const restify = require('foxify-restify-odin');
  3. const User = require('./models/User');
  4. let app = new Foxify();
  5. app.get('/users', restify(User), async (req, res) => {
  6. res.json({
  7. users: await req.fro.query.get(),
  8. total_users: await req.fro.counter.count(),
  9. });
  10. });
  11. app.start();

Documentation

  1. type Operator = "lt" | "lte" | "eq" | "ne" | "gte" | "gt" |
  2. "exists" | "in" | "nin" | "bet" | "nbe" | "like" | "nlike";
  3. interface FilterObject {
  4. field: string;
  5. operator: Operator;
  6. value: string | number | boolean | any[] | object | Date;
  7. }
  8. interface Filter {
  9. and?: Array<FilterObject | Filter>;
  10. or?: Array<FilterObject | Filter>;
  11. has?: string | { relation: string, filter: Filter | FilterObject };
  12. }
  13. interface Query {
  14. filter?: Filter | FilterObject;
  15. include?: string[];
  16. sort?: string[];
  17. skip?: number;
  18. limit?: number;
  19. }
  20. interface RouteOptions {
  21. pre?: Foxify.Handler | Foxify.Handler[];
  22. post?: Foxify.Handler | Foxify.Handler[];
  23. }
  24. interface RoutesOptions {
  25. index: RouteOptions & { lean?: boolean; } | false;
  26. count: RouteOptions | false;
  27. store: RouteOptions | false;
  28. show: RouteOptions | false;
  29. update: RouteOptions | false;
  30. delete: RouteOptions | false;
  31. }
  32. interface Options {
  33. name: string;
  34. prefix: string;
  35. qs: QSParseOptions;
  36. defaults: Query;
  37. pre: Foxify.Handler | Foxify.Handler[];
  38. routes: Partial<RoutesOptions>;
  39. }
  40. 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

  1. qs.stringify({
  2. filter: {
  3. field: "username",
  4. operator: "eq",
  5. value: "ardalanamini",
  6. }
  7. })
  1. qs.stringify({
  2. filter: {
  3. or: [
  4. {
  5. field: "username",
  6. operator: "eq",
  7. value: "ardalanamini",
  8. },
  9. {
  10. and: [
  11. {
  12. field: "age",
  13. operator: "gte",
  14. value: 18,
  15. },
  16. {
  17. field: "email",
  18. operator: "exists",
  19. value: true,
  20. },
  21. ],
  22. },
  23. ],
  24. },
  25. })

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

  1. qs.stringify({
  2. include: [
  3. "relation1",
  4. "relation2.subRelation1.subRelation2",
  5. ]
  6. })

sort

  1. qs.stringify({
  2. sort: [
  3. "field1", // same as "+field1"
  4. "-field2",
  5. "+field3",
  6. ]
  7. })

skip

  1. qs.stringify({
  2. skip: 100,
  3. })

limit

  1. qs.stringify({
  2. limit: 10,
  3. })

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