Giter Club home page Giter Club logo

Comments (10)

pubkey avatar pubkey commented on July 19, 2024 1

Running this file on node.js reproduces the error. You need to have set "type": "module" in the package.json.

import { useOperators, OperatorType } from 'mingo/core';
import { Query } from 'mingo/query';
import { $project } from 'mingo/operators/pipeline/project';
import { $sort } from 'mingo/operators/pipeline/sort';
import { $and } from 'mingo/operators/expression/boolean/and';
import { $not } from 'mingo/operators/expression/boolean/not';
import { $or } from 'mingo/operators/expression/boolean/or';
import { $nor } from 'mingo/operators/query/logical/nor';
import { $eq } from 'mingo/operators/expression/comparison/eq';
import { $ne } from 'mingo/operators/expression/comparison/ne';
import { $gt } from 'mingo/operators/expression/comparison/gt';
import { $gte } from 'mingo/operators/expression/comparison/gte';
import { $lt } from 'mingo/operators/expression/comparison/lt';
import { $lte } from 'mingo/operators/expression/comparison/lte';
import { $regex } from 'mingo/operators/query/evaluation/regex';
import { $mod } from 'mingo/operators/query/evaluation/mod';
import { $elemMatch } from 'mingo/operators/projection/elemMatch';
import { $exists } from 'mingo/operators/query/element/exists';
import { $nin } from 'mingo/operators/query/comparison/nin';
import { $in } from 'mingo/operators/query/comparison/in';
import { $size } from 'mingo/operators/expression/array/size';
import { $type } from 'mingo/operators/expression/type/type';

let mingoInitDone = false;

export function getMingoQuery(
    selector
) {
    if (!mingoInitDone) {

        useOperators(OperatorType.PIPELINE, {
            $sort,
            $project
        });
        useOperators(OperatorType.QUERY, {
            $and,
            $eq,
            $elemMatch,
            $exists,
            $gt,
            $gte,
            $in,
            $lt,
            $lte,
            $ne,
            $nin,
            $mod,
            $nor,
            $not,
            $or,
            $regex,
            $size,
            $type,
        });
        mingoInitDone = true;
    }
    return new Query(selector);
}

getMingoQuery({
    age: {
        $gt: 10,
        $ne: 50
    }
});

from mingo.

kofrasa avatar kofrasa commented on July 19, 2024 1

Ok, unfortunately the issue is due to weak type of enforcement of the operator function registered for their corresponding types.

Comparison expressions have different signatures when used to query documents directly via Query or when used in the aggregation pipeline.

You have registered the expression operator versions of the comparisons for the query operator type.

import { $eq } from 'mingo/operators/expression/comparison/eq';
import { $ne } from 'mingo/operators/expression/comparison/ne';
import { $gt } from 'mingo/operators/expression/comparison/gt';
import { $gte } from 'mingo/operators/expression/comparison/gte';
import { $lt } from 'mingo/operators/expression/comparison/lt';
import { $lte } from 'mingo/operators/expression/comparison/lte';

These should be

import { $eq } from 'mingo/operators/query/comparison/eq';
import { $ne } from 'mingo/operators/query/comparison/ne';
import { $gt } from 'mingo/operators/query/comparison/gt';
import { $gte } from 'mingo/operators/query/comparison/gte';
import { $lt } from 'mingo/operators/query/comparison/lt';
import { $lte } from 'mingo/operators/query/comparison/lte';

To avoid all of this hassle prefer to use the following as the initialization.

require("mingo")

It would only load the basic operators as defined here

from mingo.

kofrasa avatar kofrasa commented on July 19, 2024

There is no method CreateListFromArrayLike in mingo. Is that defined somewhere in your library?

from mingo.

pubkey avatar pubkey commented on July 19, 2024

The error comes from sending wrong data into apply see

according to the stack trace, this comes from the createExpressionOperator() method of mingo:
Screenshot from 2023-01-30 04-02-54

from mingo.

pubkey avatar pubkey commented on July 19, 2024

Interestingly, running it on the browser returns a different but similar error:

TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
	    at eval (webpack://rxdb/./node_modules/mingo/es/operators/_predicates.js?:54:16)
	    at Query.processOperator (webpack://rxdb/./node_modules/mingo/es/query.js?:54:20)

from mingo.

kofrasa avatar kofrasa commented on July 19, 2024

Something funny happening with the transpiler. It should be passing in args as an array and not a direct value.

from mingo.

kofrasa avatar kofrasa commented on July 19, 2024

It compiles createQueryOperator correctly

function createQueryOperator(predicate) {
    return function (selector, value, options) {
        var opts = { unwrapArray: true };
        var depth = Math.max(1, selector.split(".").length - 1);
        return function (obj) {
            // value of field must be fully resolved.
            var lhs = (0, util_1.resolve)(obj, selector, opts);
            return predicate(lhs, value, __assign(__assign({}, options), { depth: depth }));
        };
    };
}

but for createExpressionOperator, it converts the call into an apply because I use the spread operator.

function createExpressionOperator(predicate) {
    return function (obj, expr, options) {
        var args = (0, core_1.computeValue)(obj, expr, null, options);
        // console.log(args) --> returns the value 10.
        return predicate.apply(void 0, args);
    };
}

from mingo.

kofrasa avatar kofrasa commented on July 19, 2024

The version of the comparison operators from the expression module must resolve to array as expected by the predicate.apply generated. The inner function of the createExpressionOperator should not even be evaluated in your example.

Unfortunately, these are some side effects of some design choices and a lack of good way to assert the type of functions. In a future version, I might consider wrapping operators in classes to get some instance based type checking.

Hope that helps.

from mingo.

pubkey avatar pubkey commented on July 19, 2024

I am not able to use require("mingo") because that includes all operators in my javascript bundle.
Also importing all operators directly from operators/query will end up in adding all operators to the webpack bundle. like import {$and } from 'mingo/operators/query';

I now changed all imports like you described and it works. Thank you for your time.

from mingo.

kofrasa avatar kofrasa commented on July 19, 2024

Using require("mingo") only adds the basic operators (comparison, booleans, and query) in your bundle. You can do also do require("mingo/init/basic") which gives the same effect and is more explicit.

from mingo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.