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

@adviz/antd-query-utilty

v1.0.13

Published

## Overview

Downloads

297

Readme

Query Object Tool Documentation

Overview

This tool is used for parsing our custom filter, pagination and sorter JSON object (referred to as "Query Object" in the following text) into a Base64URL encoded and decoded string for usage in GET requests.

Design Considerations

GET Requests vs POST Requests

While some StackOverflow answers suggest that the best way to implement a filter endpoint is using something like POST /{entity}/filter where the filter object is sent in the request body, we came to a different conclusion.

The main disadvantages of using a POST request are:

  • Not Idempotent: POST requests are not idempotent and therefore not cacheable.
  • URL Reusability: You cannot copy the URL, paste it somewhere else, and get the same results.

An important consideration when using GET requests, however, is the size limitation of a URL. When using a Base64URL encoded string in the URL, it can become quite large and potentially exceed the default size limits for Express, Node.js, or the browser being used.

URL Length Limitations

| Software | Max URL Length | Additional Information | | -------- | -------------- | ------------------------------------------------------------------------------------------- | | Browser | 2083 | Chromium is the lowest by far; all others can handle upwards of 10,000 characters for URLs. | | Node.js | ~80,000 | Node.js HTTP GET URL length limitation. | | Express | - | Seems to have no explicit limitations other than Node.js. | | Axios | - | Seems to have no limitations by itself. |

Non-ASCII characters may decrease the maximum length that can be used.

Custom Base64 Encode/Decode Methods

Custom Base64 encode/decode methods are needed to make Base64 URL safe for both browser and Node.js usage. While Node.js has a method (Buffer.from(…….).toString('base64url')), no such methods are available in the browser.

Difference Between Base64 and Base64URL

The main differences are:

  • Characters: Characters 62 and 63 (+, /) are not URL safe. In Base64URL, these are replaced with - and _.
  • Trailing Equals: The trailing = is removed in Base64URL.

Tool Capabilities

This tool is able to:

  1. Validate and convert a Query Object into a string.
  2. Validate and convert such custom strings back into a Query Object.
  3. Convert a Query Object into an object usable by Prisma ORM

Query Object Definition

{
  "filters": undefined | {
    "property": string[]
  },
  "pagination": undefined | {
    "current": number, // min => 1, max => Ceil(totalEntries / pageSize)
    "pageSize": number // min 1, max 100
  },
  "sorter": undefined | {
    "field": string,
    "order": int // 0 = ascend, 1 = descend, undefined
  }
}

Example Usage

Creating a Query Object

const queryObject = {
  filters: {
    name: '[foo]',
  },
  sorter: {
    field: 'name',
    order: 'asc',
  },
  pagination: {
    skip: 0,
    take: 10,
  },
};

Converting Query Object into string

import AntdQueryUtility from '@adviz/antd-query-utilty';

const queryString = AntdQueryUtility.ToQueryString(queryObject);

Converting string back into Query Object

Be sure to not just call with req.url rather with req.query.queryString.

import AntdQueryUtility from '@adviz/antd-query-utilty';

const queryObject = AntdQueryUtility.ToQueryObject(queryString);

Converting a Query Object to a Prisma Query

import AntdQueryUtility from '@adviz/antd-query-utilty';

const prismaQuery = AntdQueryUtility.ToPrismaQueryFromQueryObject(queryObject);

Converting a Query String to a Prisma Query directly

import AntdQueryUtility from '@adviz/antd-query-utilty';

const prismaQuery = AntdQueryUtility.ToPrismaQueryFromQueryString(queryObject);

// with single relation to another entity
const prismaQuery = AntdQueryUtility.ToPrismaQueryFromQueryString(
  queryObject,
  true, // validation = true
  ['address'] // singe relation keys
);

// n:m relation to another entity
const prismaQuery = AntdQueryUtility.ToPrismaQueryFromQueryString(
  queryObject,
  true, // validation = true
  undefined,
  ['friends'] // multi relation keys
);

Special Where Keys

Some keys have to transformed differently than others, but it is not possible to differentiate them just from filter values. For this problem we have implemented the enum PrismaWhereKeyTypes

enum PrismaWhereKeyTypes {
  Enum = 'enum',
  EnumArray = 'enum[]',
  StringArray = 'string[]',
  ExactString = 'string',
  LikeString = 'likeString',
}

Every Array Type is filtered by an || link, && is currently not supported.

Usage

import AntdQueryUtility from '@adviz/antd-query-utilty';

const prismaQuery = AntdQueryUtility.ToPrismaQueryFromQueryString(queryObject);

const prismaQuery = AntdQueryUtility.ToPrismaQueryFromQueryString(
  queryObject,
  true, // validation = true
  undefined, // no single relation keys
  undefined, // no mulit relation keys
  {
    tags: PrismaWhereKeyTypes.StringArray,
  }
);

Cleanup Query Object

Antd for example popuplates the object with some key: undefined values that need to be cleaned up.

import AntdQueryUtility from '@adviz/antd-query-utilty';

const queryString = AntdQueryUtility.ToQueryString(queryObject, true); // true => withCleanup

OR

import AntdQueryUtility from '@adviz/antd-query-utilty';

const cleanedUpQueryObject = AntdQueryUtility.CleanUpObject(queryObject);
const queryString = AntdQueryUtility.ToQueryString(cleanedUpQueryObject);