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

soql-build

v2.2.0

Published

Securely build SOQL string from JSON-based query structure

Downloads

37

Readme

soql-build Build Status

Securely build SOQL string from JSON-based query structure

Example

import { buildSOQL } from 'soql-build';

/**
 * Simple Query
 */
const soql1 = buildSOQL({
  fields: ['Id', 'Name', 'Account.Name'],
  table: 'Opportunity',
  condition: {
    operator: 'AND',
    conditions: [
      {
        field: 'Amount',
        operator: '>=',
        value: 5000,
      },
    ],
  },
});

console.log(soql1);
// => SELECT Id, Name, Account.Name FROM Opportunity WHERE Amount >= 5000

/**
 * Complex Condition / Order By / Limit / Offset
 */
const soql2 = buildSOQL({
  fields: ['Id', 'Name', 'Account.Name'],
  table: 'Opportunity',
  condition: {
    operator: 'AND',
    conditions: [
      {
        operator: 'NOT',
        condition: {
          field: 'Account.Name',
          operator: 'LIKE',
          value: '%a%',
        },
      },
      {
        field: 'Amount',
        operator: '>=',
        value: 5000,
      },
      {
        field: 'Type',
        operator: '!=',
        value: null,
      },
      {
        field: 'StageName',
        operator: 'IN',
        value: ['Prospecting', 'Value Proposition', 'Qualification'],
      },
      {
        operator: 'OR',
        conditions: [
          {
            field: 'Account.Owner.Username',
            operator: '!=',
            value: '[email protected]',
          },
          {
            field: 'Account.Owner.IsActive',
            operator: '=',
            value: false,
          },
          {
            operator: 'AND',
            conditions: [
              {
                field: 'CloseDate',
                operator: '>=',
                value: {
                  type: 'date',
                  value: '2008-01-01',
                },
              },
              {
                field: 'CloseDate',
                operator: '<',
                value: {
                  type: 'date',
                  value: 'TODAY',
                },
              },
            ],
          },
        ],
      },
    ],
  },
  sortInfo: [
    {
      field: 'Account.Type',
      direction: 'ASC',
      nullOrder: 'LAST',
    },
    {
      field: 'Amount',
      direction: 'DESC',
    },
  ],
  limit: 1000,
  offset: 50,
});

console.log(soql2);
// => SELECT Id, Name, Account.Name FROM Opportunity WHERE (NOT Account.Name LIKE '%a%') AND Amount >= 5000 AND Type != null AND StageName IN ('Prospecting', 'Value Proposition', 'Qualification') AND (Account.Owner.Username != '[email protected]' OR Account.Owner.IsActive = false OR (CloseDate >= 2008-01-01 AND CloseDate < TODAY)) ORDER BY Account.Type ASC NULLS LAST, Amount DESC LIMIT 1000 OFFSET 50

/**
 * Function / Child Relationship
 */
const soql3 = buildSOQL({
  fields: [
    'Id',
    'Name',
    {
      type: 'function',
      function: 'CALENDAR_YEAR',
      arguments: ['CloseDate'],
    },
    {
      type: 'subquery',
      fields: [
        'Id',
        'Subject',
        {
          type: 'function',
          function: 'toLabel',
          arguments: ['Status'],
        },
      ],
      relationship: 'ActivityHistories',
      condition: {
        operator: '=',
        field: 'IsTask',
        value: true,
      },
      sortInfo: [
        {
          field: 'ActivityDate',
          direction: 'DESC',
        },
      ],
      limit: 5,
    },
  ],
  table: 'Opportunity',
});

console.log(soql3);
// => SELECT Id, Name, CALENDAR_YEAR(CloseDate), (SELECT Id, Subject, toLabel(Status) FROM ActivityHistories WHERE IsTask = true ORDER BY ActivityDate DESC LIMIT 5) FROM Opportunity

/**
 * Group By Query
 */
const soql4 = buildSOQL({
  fields: [
    {
      type: 'function',
      function: 'MAX',
      arguments: ['CreatedDate'],
    },
  ],
  table: 'Account',
  grouping: {
    fields: ['Type', 'Industry'],
    subtotal: 'ROLLUP',
    having: {
      field: {
        type: 'function',
        function: 'MAX',
        arguments: ['CreatedDate'],
      },
      operator: '<=',
      value: {
        type: 'date',
        value: 'LAST_YEAR',
      },
    },
  },
});

console.log(soql4);
// => SELECT MAX(CreatedDate) FROM Account GROUP BY ROLLUP (Type, Industry) HAVING MAX(CreatedDate) <= LAST_YEAR