Have ideas to improve npm?Join in the discussion! »

    @36node/query-normalizr
    TypeScript icon, indicating that this package has built-in type declarations

    0.1.4 • Public • Published

    @36node/query-normalizr

    version downloads

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

    Install

    yarn add @36node/query-normalizr

    Usage

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

    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&assignees=*&followers=none&level_gte=10&level_lte=20&plate_like=沪A&q=hello&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",
        },
        assignees: {
          $ne: [],
        },
        followers: {
          $eq: [],
        },
        level: {
          $gte: "10",
          $lte: "20",
        },
        plate: {
          $regex: {},
        },
        $text: {
          $search: "internet",
        },
        tag: {
          $ne: "pretty",
        },
        title: {
          $regex: {},
        },
        type: {
          $in: ["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",
        },
        assignees: {
          $ne: [],
        },
        followers: {
          $eq: [],
        },
        level: {
          $gte: "10",
          $lte: "20",
        },
        plate: {
          $regex: {},
        },
        $text: {
          $search: "internet",
        },
        tag: {
          $ne: "pretty",
        },
        title: {
          $regex: {},
        },
        type: {
          $in: ["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&q=hello&tag_ne=pretty&title_like=hello&type=test1&type=test2"

    What is query normalizr

    image

    Query in route (QIR)

    Array

    we use standard url query format to pass array data.

    a=1&a=2

    Filter

    Use . to access deep properties

    GET /posts?title=json-server&author=typicode
    GET /posts?id=1&id=2
    GET /comments?author.name=typicode

    Paginate

    Use _offset and optionally _limit to paginate returned data. (an X-Total-Count header is included in the response)

    GET /posts?_offset=10
    GET /posts?_offset=7&_limit=20

    node 10 items are return by default

    Sort

    Add _sort

    # asc
    GET /posts?_sort=views
     
    # desc
    GET /posts/1/comments?_sort=-votes

    For multiple fields, use the following format:

    GET /posts/1/comments?_sort=-votes&_sort=likes

    _prefixing a path with - will flag that sort is descending order. When a path does not have the - prefix, it is ascending order.

    Operators

    Add _gt, _lt, _gte or _lte for getting a range

    GET /posts?views_gte=10&views_lte=20

    Add _ne to exclude a value

    GET /posts?id_ne=1

    Add _like to filter (RegExp supported)

    GET /posts?title_like=server

    Array wildcard

    If a field is an array, like:

    1. assignees=* means assignees has at least one member.
    2. assignees=none means assignees is an empty array.

    Full-text search

    Add q

    GET /posts?q=internet

    Select

    Specifies which document fields to include or exclude

    GET /posts?_select=title&_select=body
    GET /posts?_select=-comments&_select=-views

    prefixing a path with - will flag that path as excluded. When a path does not have the - prefix, it is included A projection must be either inclusive or exclusive. In other words, you must either list the fields to include (which excludes all others), or list the fields to exclude (which implies all other fields are included).

    Query in service (QIS)

    {
      limit: 10,
      offset: 10,
      sort: "-createdBy", // if array should be: ["-createdBy", "views"]
      select: ["views", "body"], // if single should be: "views"
      populate: "author",
      filter: {
        age: {
          $lt: 10,  // age_lt
          $gt: 5,   // age_gt
        },
        tag: {
          $ne: "pretty",  // tag_ne
        },
        name: "sherry",
        title: {
          $regex: /^hello .* world$/i,  // like
        },
        assignees: { $ne: [] }, // *
        followers: { $eq: [] }, // none
        $text: { $search: "hello" },  // q
      }
    }

    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.1.4

    License

    MIT

    Unpacked Size

    24.5 kB

    Total Files

    8

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar
    • avatar