Skip to content

Instantly share code, notes, and snippets.

@stalniy
Created October 16, 2020 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stalniy/e24c16a29490e878d9d7fce1536cac0d to your computer and use it in GitHub Desktop.
Save stalniy/e24c16a29490e878d9d7fce1536cac0d to your computer and use it in GitHub Desktop.
MongoDB query parser + JS interpreter
import { MongoQueryParser, allParsingInstructions } from '@ucast/mongo';
import { createJsInterpreter, allInterpreters } from '@ucast/js';
const parser = new MongoQueryParser(allParsingInstructions);
const interpret = createJsInterpreter(allInterpreters);
const ast = parser.parse({ authorId: 1, status: 'published' });
console.log(interpret(ast, { authorId: 1, status: 'published' })); // true
console.log(interpret(ast, { authorId: 2, status: 'published' })); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment