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.
You can install the package via npm:
npm install @wedesk/pipeline
import { Pipelines } from '@wedesk/pipeline';
import { PipelineData } from './pipeline-data';
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);
You can update the pipeline data using the updateData
method.
const newData: PipelineData[] = [
// Your new pipeline data here
];
pipelines.updateData(newData);
To generate the aggregation stages, use the toAggregates
method.
const aggregates = pipelines.toAggregates();
console.log(aggregates);
The pipeline data structure consists of several types, each representing a different aggregation stage. Below are the types of pipeline stages you can create:
type PipelineAddFieldData = {
key: 'addField';
property: string;
name: string;
operator: OperatorType;
};
type PipelineGroupData = {
key: 'group';
property: string;
group: {
field: string;
operator: OperatorType;
value: string;
}[];
};
type PipelineFilterData = {
key: 'filter';
property: string;
operator: OperatorType;
value: string;
};
type PipelineFormulaData = {
key: 'formula';
name: string;
property: string;
operator: OperatorType;
};
type PipelineSortData = {
key: 'sort';
property: string;
direction: 'asc' | 'desc';
};
type PipelineCountData = {
key: 'count';
};
type PipelineCustomData = {
key: 'custom';
pipeline: Record<string, string | number | object>;
};
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';
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));
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.