Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow functions in the walk path of the query #186

Open
machadofisher opened this issue Mar 13, 2023 · 0 comments
Open

Allow functions in the walk path of the query #186

machadofisher opened this issue Mar 13, 2023 · 0 comments
Labels

Comments

@machadofisher
Copy link

machadofisher commented Mar 13, 2023

Motivation

Dear all, regarding ORM frameworks and the lazy load topic, I tried to introduce the feature of using jsonpath queries to walk
through lazy load data. To accomplish that objective we just need to allow functions names in path. The system
should test if the type of var object in the walkpath is a function or not, if is a function, the function should be invoked.
Functions with zero arguments, like gets should be enough. For example

User --> Cars but the cars property is not present, because ORM did not loaded it using eager policy
users = [
{
"name: "jonhDoe"
"cars" : []
}
]

but if we are using for example, sequelize framework, the method getCars() should be there in order to query database.

so the jsonpath query should be:
$.users[0].getCars.name

{
name: jonhDoe
cars : []
getCars: function(){ //sequelize stuff added by the sequlelize ORM }
}

The only problem of the async wait for the promises, where we have promises involved in middle

Current behavior

Currently the system do nothing when a function appears in the path, just print a warning I suppose

Desired behavior

System should invoke the function and go from there. In a first version the getters should be enough for most requirements.

Alternatives considered

Actually I have this working by changing the line ~ 370 of dist/index-node-cjs. by doing only this:

JSONPath.prototype._trace = function (expr, val, path, parent, parentPropName, callback, hasArrExpr, literalPriority) {
// No expr to follow? return path and value as the result of
// this trace branch
........
.......
if ((typeof loc !== 'string' || literalPriority) && val && hasOwnProp.call(val, loc)) {

  //------ OLD LINE:    addRet(this._trace(x, loc, push(path, loc), val, loc, callback, hasArrExpr));

  //NEW >>>>>>
  let varLoc = val[loc];
  if(typeof varLoc == "function")
  {
      varLoc = varLoc();
      //Check if it is a promise in order to await for it
      //await is not possible here because it is not an async function
  }
  addRet(this._trace(x, varLoc, push(path, loc), val, loc, callback, hasArrExpr));
  //NEW <<<<<

}

Many thanks

@machadofisher machadofisher changed the title Include functions walk in json scructure Allow functions in the walk path of the query Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant