-
Notifications
You must be signed in to change notification settings - Fork 150
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
Comments
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 You could use 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 Hope this helps. |
Perfect. That code snippet could do with going in the docs. Very useful. |
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 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! |
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()),
);
}; |
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
The text was updated successfully, but these errors were encountered: