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

@bugslifesolutions/api-plugin-products

v2.1.0-next.11

Published

Products plugin for the Reaction API

Downloads

7

Readme

api-plugin-products

npm (scoped) CircleCI semantic-release

Summary

Products plugin for the Reaction API

Example on how to use Filter Conditions

We have a query endpoint defined in this plugin which allows us to query products collection based on the input GraphQL conditions object. This query endpoint is defined as filterProducts.js and it calls the generateFilterQuery function from the api-utils plugin to generate the MongoDB filter query.

The generateFilterQuery function expects the input GraphQL conditions object to be in the format of the FilterConditionsInput input type defined in the GraphQL Schemas (in api-core plugin) along with other parameters like context, collectionName and shopId.

Please go through a general introduction of how to use this function which can be found in the api-utils README before going through the examples below on how to use this function in the context of the products plugin.

In the query endpoint, we pass the FilterConditionsInput input type object as the conditions argument. This object is passed to the generateFilterQuery function along with other parameters like context, collectionName and shopId to generate the MongoDB filter query. The generateFilterQuery function is generic and can be used to generate filter queries for any collection. Since the parametes like context, collectionName and shopId are pretty self-explanatory, we shall focus on explaining the various ways in which the conditions object can be used.

  1. Single condition. Here we are querying products collection for entries with the handle as 'mens-waterproof-outdoor-rain-jacket'. Since it is single condition, using either all or any will not make difference.
import generateFilterQuery from "@reactioncommerce/api-utils/generateFilterQuery.js";

const conditions = {
      all: [
        {
          any:[
            {
              key: "handle",
              stringValue: "mens-waterproof-outdoor-rain-jacket",
              relationalOperator: eq,
              logicalNot: false
            }
          ]
        }
      ]
    }

  const { filterQuery } = generateFilterQuery(context, "Product", conditions, shopId);

  return Products.find(filterQuery);
  1. Two conditions.

Here we are querying products collection for entries which have either the handle as 'mens-waterproof-outdoor-rain-jacket' or 'title' begins with the text 'men'. Since we are using the any to connect the conditions, it translates to a mongo DB $or condition. Please note that the top level all condition is only to maintain the structure of the input GraphQL conditions object. It does not impact the results of the inner query.

import generateFilterQuery from "@reactioncommerce/api-utils/generateFilterQuery.js";

const conditions = {
      all: [
        {
          any:[
            {
              key: "handle",
              stringValue: "mens-waterproof-outdoor-rain-jacket",
              relationalOperator: eq,
              logicalNot: false
            }, 
            {
              key: "title",
              stringValue: "men",
              relationalOperator: beginsWith,
              logicalNot: false
              caseSensitive: false
            }
          ]
        }
      ]
    }

  const { filterQuery } = generateFilterQuery(context, "Product", conditions, shopId);

  return Products.find(filterQuery);
  1. Multiple conditions.

Here we are querying products collection for entries which confirms to multiple conditions. We have 3 distinct group of conditions in the inner level and the results of all these 3 are joined at the top level with all meaning $and in MongoDB.

The first group looks for entries matching either of the conditions handle as 'mens-waterproof-outdoor-rain-jacket' or title begins with the text 'men'. Since we are using the any to connect the conditions, it translates to a mongo DB $or condition.

The second group looks for entries matching the _id in the array ["DZwLHk4EAzitRni8F", "Hn4BRaBvLkYffMq36"] and isDeleted as false and workflow.status as new. Since we are using the all to connect the conditions, it translates to a mongo DB $and condition.

The third group looks for entries matching the price.min greater than 19.99 and type as simple. Since we are using the all to connect the conditions, it translates to a mongo DB $and condition.

As explained above, the final results are joined at the top level with all meaning $and in MongoDB.

import generateFilterQuery from "@reactioncommerce/api-utils/generateFilterQuery.js";

const conditions = {
      all: [
        {
          any:[
            {
              key: "handle",
              stringValue: "mens-waterproof-outdoor-rain-jacket",
              relationalOperator: eq,
              logicalNot: false
            }, 
            {
              key: "title",
              stringValue: "men",
              relationalOperator: beginsWith,
              logicalNot: false
              caseSensitive: false
            }
          ]
        },
        {
          all:[
            {
              key: "_id",
              stringArrayValue: ["DZwLHk4EAzitRni8F", "Hn4BRaBvLkYffMq36"],
              relationalOperator: in,
              logicalNot: false
            },
            {
              key: "isDeleted",
              booleanValue: false,
              relationalOperator: eq,
              logicalNot: false
            },
            {
              key: "workflow.status",
              stringValue: "new",
              relationalOperator: eq,
              logicalNot: false
            }
          ]
        },
        {
          all:[
            {
              key: "price.min",
              floatValue: 19.99,
              relationalOperator: gte,
              logicalNot: false
            },
            {
              key: "type",
              stringValue: "simple",
              relationalOperator: eq,
              logicalNot: false
            }
          ]
        }
      ]
    }

  const { filterQuery } = generateFilterQuery(context, "Product", conditions, shopId);

  return Products.find(filterQuery);

Developer Certificate of Origin

We use the Developer Certificate of Origin (DCO) in lieu of a Contributor License Agreement for all contributions to Reaction Commerce open source projects. We request that contributors agree to the terms of the DCO and indicate that agreement by signing all commits made to Reaction Commerce projects by adding a line with your name and email address to every Git commit message contributed:

Signed-off-by: Jane Doe <[email protected]>

You can sign your commit automatically with Git by using git commit -s if you have your user.name and user.email set as part of your Git configuration.

We ask that you use your real name (please no anonymous contributions or pseudonyms). By signing your commit you are certifying that you have the right have the right to submit it under the open source license used by that particular Reaction Commerce project. You must use your real name (no pseudonyms or anonymous contributions are allowed.)

We use the Probot DCO GitHub app to check for DCO signoffs of every commit.

If you forget to sign your commits, the DCO bot will remind you and give you detailed instructions for how to amend your commits to add a signature.

License

This Reaction plugin is GNU GPLv3 Licensed