File tree Expand file tree Collapse file tree 4 files changed +39
-3
lines changed
Expand file tree Collapse file tree 4 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ export { DateToString } from "./documentOperations/DateToString";
2020export { ToDate } from "./documentOperations/ToDate" ;
2121export { Multiply } from "./documentOperations/Multiply" ;
2222export { MergeObject } from "./documentOperations/MergeObject" ;
23+ export { Expression } from "./documentOperations/Expression" ;
2324
2425export async function aggregate (
2526 collection : Collection ,
Original file line number Diff line number Diff line change 11import { OperationInt } from "../core/OperationInt" ;
22import { Collection } from "../core/Collection" ;
33import { NestedResolver } from "../Utils/NetsedResolver" ;
4+ import { isDocumentOperationInt } from "../core/DocumentOperationInt" ;
5+ import { AggregateOperation } from "../core/AggregateOperation" ;
46
57export 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 }
You can’t perform that action at this time.
0 commit comments