@wedesk/pipeline
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

Pipelines Package

The Pipelines package is designed to help you build and manage MongoDB aggregation pipelines with ease. This package allows you to create complex aggregation stages programmatically, making it simple to generate and update your pipeline data.

Installation

You can install the package via npm:

npm install @wedesk/pipeline

Usage

Importing the Package

import { Pipelines } from '@wedesk/pipeline';
import { PipelineData } from './pipeline-data';

Creating a Pipeline Instance

To create a new instance of the Pipelines class, you need to pass an array of PipelineData.

const data: PipelineData[] = [
  // Your pipeline data here
];

const pipelines = new Pipelines(data);

Updating Pipeline Data

You can update the pipeline data using the updateData method.

const newData: PipelineData[] = [
  // Your new pipeline data here
];

pipelines.updateData(newData);

Generating Aggregates

To generate the aggregation stages, use the toAggregates method.

const aggregates = pipelines.toAggregates();
console.log(aggregates);

Pipeline Data Structure

The pipeline data structure consists of several types, each representing a different aggregation stage. Below are the types of pipeline stages you can create:

Add Field

type PipelineAddFieldData = {
  key: 'addField';
  property: string;
  name: string;
  operator: OperatorType;
};

Group

type PipelineGroupData = {
  key: 'group';
  property: string;
  group: {
    field: string;
    operator: OperatorType;
    value: string;
  }[];
};

Filter

type PipelineFilterData = {
  key: 'filter';
  property: string;
  operator: OperatorType;
  value: string;
};

Formula

type PipelineFormulaData = {
  key: 'formula';
  name: string;
  property: string;
  operator: OperatorType;
};

Sort

type PipelineSortData = {
  key: 'sort';
  property: string;
  direction: 'asc' | 'desc';
};

Count

type PipelineCountData = {
  key: 'count';
};

Custom

type PipelineCustomData = {
  key: 'custom';
  pipeline: Record<string, string | number | object>;
};

Operator Types

The OperatorType defines various operators you can use in your pipeline stages. Here are the available operators:

type OperatorType =
  | 'add'
  | 'subtract'
  | 'multiply'
  | 'divide'
  | 'mod'
  | 'arrayElemAt'
  | 'concatArrays'
  | 'filter'
  | 'isArray'
  | 'size'
  | 'slice'
  | 'concat'
  | 'substr'
  | 'toLower'
  | 'toUpper'
  | 'strLenCP'
  | 'regexMatch'
  | 'and'
  | 'or'
  | 'not'
  | 'nor'
  | 'eq'
  | 'ne'
  | 'gt'
  | 'gte'
  | 'lt'
  | 'lte'
  | 'cmp'
  | 'dateFromString'
  | 'dateToString'
  | 'dayOfMonth'
  | 'dayOfWeek'
  | 'dayOfYear'
  | 'month'
  | 'year'
  | 'hour'
  | 'minute'
  | 'second'
  | 'millisecond'
  | 'isoWeek'
  | 'isoWeekYear'
  | 'convert'
  | 'toString'
  | 'toInt'
  | 'toLong'
  | 'toDouble'
  | 'toDecimal'
  | 'toBool'
  | 'toDate'
  | 'type'
  | 'literal'
  | 'mergeObjects'
  | 'cond'
  | 'ifNull'
  | 'sum'
  | 'avg'
  | 'min'
  | 'max'
  | 'push'
  | 'addToSet'
  | 'first'
  | 'last'
  | 'isNumber'
  | 'isString'
  | 'isBoolean'
  | 'isDate'
  | 'isArray'
  | 'isObject'
  | 'text'
  | 'search'
  | 'match'
  | 'group'
  | 'project'
  | 'limit'
  | 'skip'
  | 'unwind'
  | 'sort'
  | 'lookup'
  | 'graphLookup'
  | 'facet'
  | 'bucket'
  | 'bucketAuto'
  | 'count'
  | 'merge'
  | 'out'
  | 'replaceRoot'
  | 'replaceWith'
  | 'sortByCount'
  | 'redact'
  | 'sample'
  | 'indexStats'
  | 'listSessions'
  | 'listLocalSessions'
  | 'planCacheStats';

Example

Here is an example of how to use the package:

import { Pipelines } from '@wedesk/pipeline';
import { PipelineData } from './pipeline-data';

const data: PipelineData[] = [
  {
    key: 'addField',
    property: 'totalPrice',
    name: 'totalPriceWithTax',
    operator: 'multiply',
    include: true,
  },
  {
    key: 'group',
    property: 'category',
    group: [
      {
        field: 'totalSales',
        operator: 'sum',
        value: 'price',
      },
    ],
    include: true,
  },
  {
    key: 'sort',
    property: 'totalSales',
    direction: 'desc',
    include: true,
  },
];

const pipelines = new Pipelines(data);
const aggregates = pipelines.toAggregates();

console.log(JSON.stringify(aggregates, null, 2));

License

This project is licensed under the MIT License.


Feel free to customize the above README to match your specific package details, such as the package name and any additional instructions or information you want to include.

Package Sidebar

Install

npm i @wedesk/pipeline

Weekly Downloads

4

Version

0.1.3

License

MIT

Unpacked Size

21.3 kB

Total Files

19

Last publish

Collaborators

  • wedesk