Last active
January 10, 2023 16:04
-
-
Save joshuaalpuerto/6b818373c35e793e0895d00a275fa57c to your computer and use it in GitHub Desktop.
Mongodb simple aggregate builder using Proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generatePipeline(pipelines = []) { | |
const callable = () => {}; | |
callable.pipelines = pipelines; | |
return new Proxy(callable, { | |
get(callable, propKey) { | |
return (props) => { | |
if (propKey === 'run') { | |
return callable.pipelines; | |
} | |
callable.pipelines.push({ [`$${propKey}`]: props }); | |
return generatePipeline(callable.pipelines); | |
}; | |
}, | |
}); | |
} |
Author
joshuaalpuerto
commented
Jan 10, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment