Skip to content

Commit 5d4d656

Browse files
committed
add the Expression Document Operation to add custom JS execution
1 parent b387f23 commit 5d4d656

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "js-aggregate",
33
"author": "Axel77g",
4-
"version": "0.0.3",
4+
"version": "0.0.4",
55
"type": "module",
66
"scripts": {
77
"dev": "tsc --watch --resolveJsonModule",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { DocumentOperationInt } from "../core/DocumentOperationInt";
2+
import { CollectionDocument } from "../core/CollectionDocument";
3+
4+
type ExpressionCallback = (document: CollectionDocument) => Promise<any>;
5+
6+
export class Expression implements DocumentOperationInt {
7+
private readonly callback: ExpressionCallback;
8+
constructor(callback: ExpressionCallback) {
9+
this.callback = callback;
10+
}
11+
async execute(document: CollectionDocument): Promise<any> {
12+
return this.callback(document);
13+
}
14+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export { DateToString } from "./documentOperations/DateToString";
2020
export { ToDate } from "./documentOperations/ToDate";
2121
export { Multiply } from "./documentOperations/Multiply";
2222
export { MergeObject } from "./documentOperations/MergeObject";
23+
export { Expression } from "./documentOperations/Expression";
2324

2425
export async function aggregate(
2526
collection: Collection,

src/operations/Match.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { OperationInt } from "../core/OperationInt";
22
import { Collection } from "../core/Collection";
33
import { NestedResolver } from "../Utils/NetsedResolver";
4+
import { isDocumentOperationInt } from "../core/DocumentOperationInt";
5+
import { AggregateOperation } from "../core/AggregateOperation";
46

57
export class Match implements OperationInt {
68
private query: object;
@@ -13,9 +15,28 @@ export class Match implements OperationInt {
1315
for (const doc of collection) {
1416
let match = true;
1517
for (const key in this.query) {
16-
const values = NestedResolver.getNestedValue(doc, key);
1718
// @ts-ignore
18-
if (values !== this.query[key]) {
19+
let targetValue = this.query[key];
20+
21+
if (targetValue instanceof AggregateOperation) {
22+
const operation = targetValue.getOperation();
23+
const args = targetValue.getArgs();
24+
const operationInstance = new operation(...args);
25+
if (isDocumentOperationInt(operationInstance)) {
26+
targetValue = await operationInstance.execute(doc);
27+
} else throw new Error("Operation is not a DocumentOperation");
28+
}
29+
30+
const values = NestedResolver.getNestedValue(doc, key);
31+
32+
if (targetValue === true) {
33+
if (!values) {
34+
match = false;
35+
break;
36+
}
37+
continue;
38+
}
39+
if (values !== targetValue) {
1940
match = false;
2041
break;
2142
}

0 commit comments

Comments
 (0)