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

mongo-formula

v1.0.2

Published

Parser for MongoDB aggregation pipeline expression written in a formula-like style

Downloads

129

Readme

Mongo Formula

Parser for MongoDB aggregation expression and pipeline written in a formula-like style.

Overview

This project aims to simplify writing aggregation expression and pipeline.

Type Error

One problem of using aggregation pipeline is that type error can only be discovered in runtime. For example, expression { $add: [$str, 2] } is invalid if $str is a string.

Readability

Another problem is that complex expression is less readable. For example, expression { $subtract: [{ $add: [1, 2] }, { $multiply: [3, 4, 5] }] } could be compactly represented by 1 + 2 - 3 * 4 * 5 in common calculator syntax.

Solution

To address above issues, a syntax is defined and the corresponding parser is builded.

Getting Started

Npm

npm install --save mongo-formula

Yarn

yarn add mongo-formula

Usage

Setup

import { MongoFormulaParser, allFuncDict, allStageDict } from 'mongo-formula';
/* 
FuncDict/StageDict is the definition of functions/stages.
allFuncDict/allStageDict includes all the functions/stages defined in this library.
You may select only the required functions/stages to form a custom FuncDict/StageDict in order to reduce memory usage and bundle size.
*/
const schema = { num: { type: 'number' } }; // Schema is a type definition of the variables (document fields)
const options = {
	reservedSuffix: '___', // suffix reserved for variables used by this library. Default: ___
	undefinedTyp: { type: 'unknown' }, // typ of variables that are not defined in schema. Default: { type: 'unknown' }
	compatMode: true, // If true, some funcs/stages replaced by other more compatible funcs/stages. Default: true
};
const parser = new MongoFormulaParser(
	schema,
	allFuncDict,
	allStageDict,
	options
);

Parse Valid Formula to Expression

parser.parse(`num + 1`);
/*
output: (other fields are omitted for simplicity)
{
	typ: { type: 'number' },
	val: { $add: ['$num', 1] },
	dep: ['num'],
};
*/

Parse Valid Formula to Pipeline

parser.parse(`>> SET({ numPlusOne: num + 1 })`);
/*
output: (other fields are omitted for simplicity)
{
	typ: { type: 'pipeline' },
	val: [{ $addFields: { numPlusOne: { $add: ['$num', 1] } } }],
	dep: ['num'],
}
*/

Parse Invalid Formula

parser.parse(`num + "1"`);
/*
throw:
SMARTADD: Invalid expr1 typ. Received "stringL". Expected "number".

note:
It is invalid because "1" is string literal (stringL) which is not assignable to number.
SMARTADD is the underlying function called when using +.
*/

Parse Formula Template to Expression Creator

const createExpr = parser.parseTemplate([{ type: 'number' }])`${(
	...vs: any[]
) => vs[0]} + 1`;

createExpr(500);
/*
output: (other fields are omitted for simplicity)
{
	typ: { type: 'number' },
	val: { $add: [{ $ifNull: [null, 500] }, 1] },
	dep: [],
};

note:
{ $ifNull: [null, 500] } is identical to 500.
*/

Cli

This library provides a simple cli to use the mongoFormulaParser.

To start the cli, you can simply run mongo-formula in your project root. FuncDict used is allFuncDict. StageDict used is allStageDict. Options used are the defaults. To set schema, you can use the command setSchema.

> setSchema({ num: { type: 'number' } })
setSchema:
{ num: { type: 'number' } }
> num + 1
{
  typ: { type: 'number' },
  val: { '$add': [ '$num', 1 ] },
  dep: [ 'num' ]
}

Documentation

Full documentation can be found here.