Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/estebgonza/ts-mongo-aggregation-parser
TS Parser for Mongo pipeline aggregation queries
https://github.com/estebgonza/ts-mongo-aggregation-parser
Last synced: about 17 hours ago
JSON representation
TS Parser for Mongo pipeline aggregation queries
- Host: GitHub
- URL: https://github.com/estebgonza/ts-mongo-aggregation-parser
- Owner: estebgonza
- Created: 2023-03-12T21:19:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-09T15:04:44.000Z (24 days ago)
- Last Synced: 2024-09-09T18:27:36.384Z (24 days ago)
- Language: TypeScript
- Size: 155 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ts-mongo-aggregation-parser
[![Tests Status](https://github.com/estebgonza/ts-mongo-aggregation-parser/actions/workflows/tests.yaml/badge.svg)](https://github.com/estebgonza/ts-mongo-aggregation-parser/actions/workflows/tests.yaml/badge.svg)
This parser is generated using [pegjs](https://pegjs.org/). It parses a string containing a MongoDB aggregation pipeline and returns an AST.
The AST Types are defined in [src/ast-types.ts](src/ast-types.ts).
This package is compatible with CommonJS and ESM.## Motivations
Allow [Maggregor](https://github.com/maggregor/maggregor) to parse request pipelines on MongoDB.
Maggregor is a simple data-layer that speeds up MongoDB queries: https://github.com/maggregor/maggregor## Installation
```bash
npm install ts-mongo-aggregation-parser
yarn add ts-mongo-aggregation-parser
pnpm add ts-mongo-aggregation-parser
```## Parser usage
```typescript
import { astTypes, parse } from 'ts-mongo-aggregation-parser'const pipeline = `[
{
$group: {
_id: '$name',
count: {
$sum: 1,
},
},
},
]`const ast = parse(pipeline)
```## Using the AST Visitor
The Visitor AST provides a good way to traverse and manipulate our custom Abstract Syntax Trees (ASTs). To use the AST visitor, simply extend the `BaseASTVisitor` class and override the methods for the specific AST node types you're interested in. Below is a brief example:
```typescript
import {
ASTAggregationExpression,
ASTField,
ASTProperty,
ASTStageGroup,
ASTStageList,
ASTVisitor,
BaseASTVisitor,
} from './ts-mongo-aggregation-parser'class CustomASTVisitor extends BaseASTVisitor {
visitStageGroup(stageGroup: ASTStageGroup): void {
console.log(`StageGroup ID: ${stageGroup.id.name}`)
}visitAggregationExpression(agg: ASTAggregationExpression): void {
console.log(`Aggregation Operator: ${agg.operator}`)
}
}// Assuming you have created an AST
const ast = new ASTStageList() /* your stages here */// Use your custom visitor
const visitor = new CustomASTVisitor()
ast.accept(visitor)
```## Supported aggregation operators
- [x] $sum
- [x] $avg
- [x] $max
- [x] $min
- [ ] $push
- [ ] $addToSet
- [ ] $first
- [ ] $last
- [ ] $addToSet## Supported stages
- [x] $group
- [ ] $project
- [ ] $match
- [ ] $limit
- [ ] $skip
- [ ] $unwind
- [ ] $sort