Miss any of our Open RFC calls?Watch the recordings here! »

@36node/query-normalizr

0.4.5 • Public • Published

@36node/query-normalizr

version downloads

query-normalizr 的作用: 将 url 中的 query 规则化成方便 sdk 和 service 层调用的数据格式。

Install

yarn add @36node/query-normalizr

Usage

import { queryNormalizr } from "@36node/query-normalizr";
 
// koa app
app.use(normalizr(queryNormalizr));

API

middleware

normalizr(options);
 
// return koa middleware

normalize

import { normalize } from "@36node/query-normalizr";
import qs from "query-string";
 
const queryStr =
  " _expand=department&_group=type&_limit=10&_offset=0&_populate=user&_select=name&_select=age&_sort=updatedAt&_sort=-createdAt&age_gt=10&age_lt=20&level_gte=10&level_lte=20&plate_like=沪A&plate_like=沪B&tag_ne=pretty&title_like=hello&type=test1&type=test2";
 
normalize(qs.parse(queryStr));
 
/*
return {
  limit: 10,
  offset: 0,
  sort: ["updatedAt", "-createdAt"],
  populate: "user",
  select: ["name", "age"],
  group: "type",
  filter: {
    age: {
      $gt: "10",
      $lt: "20",
    },
    level: {
      $gte: "10",
      $lte: "20",
    },
    plate: {
      $regex: [/沪A/i, /沪B/i],
    },
    tag: {
      $ne: "pretty",
    },
    title: {
      $regex: /hello/i,
    },
    type: ["test1", "test2"]
  },
  _expand: "department",
};
*/

denormalize

import { denormalize } from "@36node/query-normalizr";
import qs from "query-string";
 
const queryObj = {
  limit: 10,
  offset: 0,
  sort: ["updatedAt", "-createdAt"],
  populate: "user",
  select: ["name", "age"],
  group: "type",
  filter: {
    age: {
      $gt: "10",
      $lt: "20",
    },
    level: {
      $gte: "10",
      $lte: "20",
    },
    plate: {
      $regex: [/沪A/i, /沪B/i],
    },
    tag: {
      $ne: "pretty",
    },
    title: {
      $regex: /hello/i,
    },
    type: ["test1", "test2"],
  },
  _expand: "department",
};
 
qs.stringfy(denormalize(queryObj));
 
// return " _expand=department&_group=type&_limit=10&_offset=0&_populate=user&_select=name&_select=age&_sort=updatedAt&_sort=-createdAt&age_gt=10&age_lt=20&assignees=%2A&followers=none&level_gte=10&level_lte=20&plate_like=%E6%B2%AAA&plate_like=%E6%B2%AAB&q=hello&tag_ne=pretty&title_like=hello&type=test1&type=test2"

What is query normalizr

image

Query in route (QIR)

reference in url.md

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

query-normalizr © 36node, Released under the MIT License.

Authored and maintained by 36node with help from contributors (list).

github.com/zzswang · GitHub @36node · Twitter @y

Keywords

none

Install

npm i @36node/[email protected]

Version

0.4.5

License

MIT

Unpacked Size

35.4 kB

Total Files

9

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar