Socket
Socket
Sign inDemoInstall

@bitovi/objection-querystring-parser

Package Overview
Dependencies
Maintainers
14
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bitovi/objection-querystring-parser

Consider the following situation:


Version published
Weekly downloads
1
decreased by-97.67%
Maintainers
14
Weekly downloads
 
Created
Source

objection-querystring-parser

Consider the following situation:

  • You're building a standard CRUD app that more-or-less follows the JSON:API specification
  • This app will receive HTTP GET requests with querystrings like those in the examples below:
    • ?filter[start_date][$gt]=2020-01-01
    • ?sort=-date,name&page[number]=1&page[size]=5
    • ?fields[articles]=title,body&fields[people]=name
  • You need to parse these query parameters to fetch the requested data. This library transforms CRUD-related querystrings into structured data for the Objection ORM.

Installation

npm install @bitovi/objection-querystring-parser --save

Usage

  • This parser returns an array of results that can be chained together
  • Each result is an object that contains an fx and a parameters key in the format { fx: 'limit', parameters: [10] }
  • fx is the name of the function to be chained to the query
  • parameters are the parameters to be added to the function, the parameters value is an array that you can spread into your function
  • In the format Query[fx1](...parameters1)[fx2](...parameters2).
const querystringParser = require("@bitovi/objection-querystring-parser");

Sort Parameters

Reference: JSON:API - Sorting

const result = querystringParser.parse("sort=-date,name");
console.log(result);
{
  orm: "objection",
  data: [
    {
      fx: "orderBy",
      parameters: [[
        { column: "date", order: "DESC" },
        { column: "name", order: "ASC" },
      ]],
    }
  ],
  errors: [],
};

Pagination Parameters

Reference: JSON:API - Pagination

const result = querystringParser.parse("page[number]=0&page[size]=10");
console.log(result);
{
  orm: "objection",
  data: [
    [
      {
        fx: "offset",
        parameters: [0],
      },
      {
        fx: "limit",
        parameters: [10],
      },
    ],
  ],
  errors: [],
};

Include Parameters

Reference: JSON:API - Inclusion of Related Resources

const result = querystringParser.parse("include=pets,dogs");
console.log(result);
{
  orm: "objection",
  data: [
    [
      {
        fx: "select",
        parameters: ["pets", "dogs"],
      },
    ],
  ],
  errors: [],
};

Filter Parameters

const result = querystringParser.parse(
  "filter=or(any('age','10','20'),equals('name','mike'))"
);
{
  orm: "objection",
  data: [
    {
      fx: "where",
      parameters: ["age", "IN", [10, 20]],
    },
    {
      fx: "orWhere",
      parameters: ["name", "=", "mike"],
    },
  ],
  errors: [],
};

Example

  • A more practical example on how to use this library in your project can be found here

lerna changes (to be removed)

Keywords

FAQs

Package last updated on 01 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc