Skip to content

Convert Hscript expressions into MongoDB queries

Notifications You must be signed in to change notification settings

Brave-Pi/bp_hquery

Repository files navigation

Haxe project

This is an example Haxe project scaffolded by Visual Studio Code.

Without further changes the structure is following:

  • src/Main.hx: Entry point Haxe source file
  • build.hxml: Haxe command line file used to build the project
  • README.md: This file

BP HQuery

Convert Hscript expressions into MongoDB queries (using aggregation pipeline operators).

Also supports the JS sequelize library.

Usage

Mongo Engine

Run: haxe mongo.hxml

Then:

const {MongoEngine} = require('./bin/js/mongo-engine').bp.hquery;
const engine = new MongoEngine();
const parsed = engine.parse("x == 5 && y <= 10");
if(parsed.ok) {
   console.log(parsed.result);
} else {
   console.error(parsed.error);
}

Sequelize Engine

Run: haxe sequelize.hxml

Then:

const sequelize = /* get a sequelize instance */
const {SequelizeEngine} = require('./bin/js/sequelize-engine').bp.hquery;
const engine = new SequelizeEngine(sequelize);
const parsed = engine.parse("foo.like('%bar%') && baz.notLike('%qux%')");
if(parsed.ok) {
   console.log(parsed.result);
} else {
   console.error(parsed.error);
}

Examples

foo == bar

becomes:

{
   "$eq": [
      "$foo",
      "$bar"
   ]
}
foo == "baz"

becomes:

{
   "$eq": [
      "$foo",
      "baz"
   ]
}
val.isIn([1,2,3])

becomes:

 {
   "$in": [
      "$val",
      [
         1,
         2,
         3
      ]
   ]
}
(x < 10 && y > 10) || (x > -10 && y < -10)

becomes:

{
   "$or": [
      {
         "$and": [
            {
               "$lt": [
                  "$x",
                  10
               ]
            },
            {
               "$gt": [
                  "$y",
                  10
               ]
            }
         ]
      },
      {
         "$and": [
            {
               "$gt": [
                  "$x",
                  -10
               ]
            },
            {
               "$lt": [
                  "$y",
                  -10
               ]
            }
         ]
      }
   ]
}

About

Convert Hscript expressions into MongoDB queries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages