npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bitovi/objection-querystring-parser

v0.3.1

Published

This library builds on top of [`@bitovi/querystring-parser`](https://github.com/bitovi/querystring-parser/tree/main/packages/querystring-parser#readme) to transform CRUD-related querystrings into structured data for the [Objection ORM](https://vincit.gith

Downloads

78

Readme

objection-querystring-parser

This library builds on top of @bitovi/querystring-parser to transform CRUD-related querystrings into structured data for the Objection ORM.

Installation

npm install @bitovi/objection-querystring-parser

If you do not plan to use this library with Objection, please install @bitovi/querystring-parser.

Usage

  • This parser returns an array of results that can be chained together
  • Each result is an object that contains an fx, isNested and a parameters key in the format { fx: 'limit', isNested: false, parameters: [10] }
  • fx is the name of the function to be chained to the query
  • isNested is a boolean that indicates if a Query is Nested(AND, OR, NOT).
  • parameters where isNested is true is an array of results each with its own fx, isNested and parameters.
  • parameters where isNested is false is an array of parameters to be added to the function fx, the parameters value is an array that you would spread into your function fx.
  • The results are used in the format Query[fx1](...parameters1)[fx2](...parameters2).
  • This is better shown with an example here.
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]=1&page[size]=10");
console.log(result);
// {
//   orm: "objection",
//   data: [
//     [
//       {
//         fx: "offset",
//         isNested: false,
//         parameters: [0],
//       },
//       {
//         fx: "limit",
//         isNested: false,
//         parameters: [10],
//       },
//     ],
//   ],
//   errors: [],
// };

Fields Parameters

Reference: JSON:API - Inclusion of Related Resources

const result = querystringParser.parse("fields[people]=id,name");
console.log(result);
// {
//   orm: "objection",
//   data: [
//     [
//       {
//         fx: "select",
//         isNested: false,
//         parameters: ["id","name"],
//       },
//     ],
//   ],
//   errors: [],
// };

Include Parameters

Reference: JSON:API - Inclusion of Related Resources

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

Filter Parameters

const result = querystringParser.parse("filter=or(any(age,'10','20'),equals(name,'mike'))");
console.log(result);
// {
//   orm: "objection",
//   data: [
//     {
//       fx: "where",
//       isNested: true,
//       parameters: [
//         {
//           fx: "whereIn",
//           isNested: false,
//           parameters: ["age", [10, 20]],
//         },
//         {
//           fx: "orWhere",
//           parameters: ["name", "=", "mike"],
//         },
//       ],
//     },
//   ],
//   errors: [],
// };
const result = querystringParser.parse("filter=not(lessOrEqual(age,'10'),equals(name,null))");
console.log(result);
// {
//   orm: "objection",
//   data: [
//     {
//       fx: "whereNot",
//       isNested: true,
//       parameters: [
//         {
//           fx: "where",
//           isNested: false,
//           parameters: ["age", "<=", 10],
//         },
//         {
//           fx: "whereNull",
//           parameters: ["name"],
//         },
//       ],
//     },
//   ],
//   errors: [],
// };

Note: Database Validations should be done before or after passing the query to the library before the database call is made.

Example

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

Further Documentation

This library builds on @bitovi/querystring-parser. See its documentation for more on using querystring-parser.