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

@vepler/property-query-builder

v1.1.1

Published

The Propbar Property Query SDK is a TypeScript library that enables the easy construction of complex queries for searching and filtering data. It offers a fluent and intuitive API for building queries with multiple groups and conditions.

Downloads

116

Readme

Propbar Property Query SDK

The Propbar Property Query SDK is a TypeScript library that enables the easy construction of complex queries for searching and filtering data. It offers a fluent and intuitive API for building queries with multiple groups and conditions.

Todo

  • [] Add documentation for loadQuery method
  • [] Add documentation for getUniqueFilterFields method

Installation

To install the Propbar Property Query SDK, you can use npm:

npm install @vepler/property-query-builder

OR 

yarn add @vepler/property-query-builder

Example

Here's an example demonstrating how to use the Propbar Property Query SDK to construct a query:

This example illustrates how to add groups, add conditions, update conditions, remove conditions, update entire groups, and retrieve the current state of the query.

import { QueryBuilder } from '@vepler/property-query-builder';

const queryBuilder = new QueryBuilder();

// Add a group
const groupId1 = queryBuilder.addGroup('AND');

// Add conditions to the group
queryBuilder.addCondition(groupId1, {
  field: 'price',
  comparator: 'gt',
  value: 250000,
});
queryBuilder.addCondition(groupId1, {
  field: 'bedrooms',
  comparator: 'eq',
  value: 2,
});

// Add another group
const groupId2 = queryBuilder.addGroup('OR');

// Add conditions to the second group
queryBuilder.addCondition(groupId2, {
  field: 'price',
  comparator: 'gt',
  value: 300000,
});
queryBuilder.addCondition(groupId2, {
  field: 'bedrooms',
  comparator: 'eq',
  value: 3,
});

// Update a condition
queryBuilder.updateCondition(groupId1, 0, {
  field: 'price',
  comparator: 'gte',
  value: 200000,
});

// Remove a condition
queryBuilder.removeCondition(groupId2, 1);

// Update an entire group
queryBuilder.updateGroup(groupId2, 'AND', [
  {
    field: 'price',
    comparator: 'lt',
    value: 400000,
  },
  {
    field: 'bedrooms',
    comparator: 'gte',
    value: 2,
  },
]);

// Get the current state of the query
const query = queryBuilder.getQuery();
console.log(JSON.stringify(query, null, 2));

Usage

Importing the SDK

To use the Propbar Property Query SDK in your TypeScript project, import the QueryBuilder class:

import { QueryBuilder } from '@vepler/property-query-builder';

Creating a Query Builder Instance

Create an instance of the QueryBuilder class to begin building your query:

const queryBuilder = new QueryBuilder();

Adding Groups

To add a group to the query, use the addGroup method and specify the operator ('AND' or 'OR'):

const groupId = queryBuilder.addGroup('AND');

The addGroup method returns a generated group ID that you can use to reference the group when adding or modifying conditions.

Adding Conditions

To add a condition to a group, use the addCondition method and provide the group ID and the condition object:

queryBuilder.addCondition(groupId, {
  field: 'price',
  comparator: 'gt',
  value: 250000,
});

The condition object should have the following properties:

  • field: The field to apply the condition on.
  • comparator: The comparison operator ('eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'contains', 'startswith', 'endswith', or 'regex').
  • value: The value to compare against (can be a string, number, boolean, or an array of these types).

Removing Conditions

To remove a condition from a group, use the removeCondition method and provide the group ID and the index of the condition:

queryBuilder.removeCondition(groupId, conditionIndex);

Updating Conditions

To update a condition within a group, use the updateCondition method and provide the group ID, the index of the condition, and the updated condition object:

queryBuilder.updateCondition(groupId, conditionIndex, {
  field: 'price',
  comparator: 'gte',
  value: 200000,
});

Updating Groups

To update an entire group, use the updateGroup method and provide the group ID, the operator, and an array of conditions:

queryBuilder.updateGroup(groupId, 'AND', [
  {
    field: 'price',
    comparator: 'lt',
    value: 400000,
  },
  {
    field: 'bedrooms',
    comparator: 'gte',
    value: 2,
  },
]);

Removing Groups

To remove a group from the query, use the removeGroup method and provide the group ID:

queryBuilder.removeGroup(groupId);

Getting the Query

To obtain the current state of the query, use the getQuery method:

const query = queryBuilder.getQuery();

The getQuery method returns the query object that follows the defined schema.

Query Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "query": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "operator": {
            "type": "string",
            "enum": ["AND", "OR"]
          },
          "groups": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "conditions": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "field": {
                        "type": "string"
                      },
                      "comparator": {
                        "type": "string",
                        "enum": [
                          "eq",
                          "ne",
                          "gt",
                          "gte",
                          "lt",
                          "lte",
                          "in",
                          "nin",
                          "contains",
                          "startswith",
                          "endswith",
                          "regex"
                        ]
                      },
                      "value": {
                        "oneOf": [
                          {
                            "type": "string"
                          },
                          {
                            "type": "number"
                          },
                          {
                            "type": "boolean"
                          },
                          {
                            "type": "array",
                            "items": {
                              "oneOf": [
                                {
                                  "type": "string"
                                },
                                {
                                  "type": "number"
                                },
                                {
                                  "type": "boolean"
                                }
                              ]
                            }
                          }
                        ]
                      }
                    },
                    "required": ["field", "comparator", "value"]
                  }
                }
              },
              "required": ["conditions"]
            }
          }
        },
        "required": ["operator", "groups"]
      }
    }
  },
  "required": ["query"]
}

License

The Propbar Property Query SDK is open-source software licensed under the MIT License.