Skip to content

Properties(node) with integer values returned with high/low object #225

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

Closed
fancellu opened this issue Mar 30, 2017 · 5 comments
Closed

Properties(node) with integer values returned with high/low object #225

fancellu opened this issue Mar 30, 2017 · 5 comments

Comments

@fancellu
Copy link

fancellu commented Mar 30, 2017

e.g.

match (n) return properties(n), labels(n), id(n)

So, for a node with properties: name: "Dino", age: 50, I'll get back the following JS object

{
          "name": "Dino",
          "age": {
            "low": 50,
            "high": 0
          }
        }```

Is there a way to get Neo4J to return something less surprising? 

The browser does this when displaying the value. If not, is there a recommended way to take the returned
object and recurse through it turning it back to a JS integer?

The properties could of course be N layers deep, and I just want to JSON.stringify() on it, 
but the high/low stuff looks odd.

Thanks

@lutovich
Copy link
Contributor

Hi @fancellu,

The problem here is that Neo4j database supports wider integer type than JS. So to make sure large integer property, that is stored in the database, can be safely (without loss of precision) retrieved using the JS driver we had to introduce Integer class. This is the thing you see with high & low properties. A bit more info about this can be found here in README: https://github.com/neo4j/neo4j-javascript-driver#a-note-on-numbers-and-the-integer-type.

You could use Record#toObject() function and then recursively traverse the returned object checking properties with neo4j.isInt() function and then reassigning them with result of Integer.toString().

Following sample code:

it('transform integers to strings', () => {
  const object = {
    name: 'Dino',
    age: int(50),
    info: {
      salary: int(1000),
      car: {
        brand: 'BMW',
        price: int(1000)
      }
    }
  };

  console.log(object);
  transform(object);
  console.log(object);
});

function transform(object) {
  for (let property in object) {
    if (object.hasOwnProperty(property)) {
      const propertyValue = object[property];
      if (isInt(propertyValue)) {
        object[property] = propertyValue.toString();
      } else if (typeof propertyValue === 'object') {
        transform(propertyValue);
      }
    }
  }
}

outputs this before transformation:

{"name":"Dino","age":{"low":50,"high":0},"info":{"salary":{"low":1000,"high":0},"car":{"brand":"BMW","price":{"low":1000,"high":0}}}}

and this after transformation:

{"name":"Dino","age":"50","info":{"salary":"1000","car":{"brand":"BMW","price":"1000"}}}

note the use of isInt() function.

Hope this helps.

@fancellu
Copy link
Author

Perfect. That code snippet could do with going in the docs. Very useful.

@lutovich
Copy link
Contributor

lutovich commented Mar 30, 2017

I guess use-case of pretty-printing records might be kind of special. Usually application would act on the received data somehow (transform to objects of other kind or make some decision based on received values). So inspection of data will happen as part of the app logic and there Integer class should not be that problematic. I can definitely be wrong here.

Will close this issue for now. We will consider adding this to docs or even adding some convenience API function if this is a common pain-point.

Thanks!

@lutovich
Copy link
Contributor

Hi @fancellu.

Please check #323 which makes it possible to create driver that only exposes native numbers.

@bjornlll
Copy link

I also wrote up my own integer to string transformation function before I found the solution above.

This function, below, loops over all properties in an object, recursively, converting all neo4j integers to strings.

const neo4jIntsToStrings = (json) => {
  const pluckAndModify = (isMatch, transformValue) =>
    Object.entries(json)
      .filter(isMatch)
      .reduce((acc, [key, value]) => ({ ...acc, [key]: transformValue(value) }), {});
  return Object.assign(
    json,
    pluckAndModify(([, value]) => typeof value === 'object', neo4jIntsToStrings),
    pluckAndModify(([, value]) => neo4j.isInt(value), value => value.toString()),
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants