Skip to content

Feature Request for a toJSON method #189

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
ariesshrimp opened this issue Jan 8, 2017 · 10 comments
Closed

Feature Request for a toJSON method #189

ariesshrimp opened this issue Jan 8, 2017 · 10 comments

Comments

@ariesshrimp
Copy link

So far I find this pattern pretty frustrating:

// I know I'm getting just one result
session.run(query, params).then(({ records }) => records[0].get(some_key).properties)

// I know I'm getting a list of results
session.run(query, params).then(({ records }) => records.map(r => r.get(some_key).properties)

I'm sure there are lots of use cases for all of that other machinery on the response, but a quick "just give me some json that looks like my data" method would be sublimely convenient.

@legraphista
Copy link
Contributor

legraphista commented Jan 16, 2017

Hi @joefraley,

There is a method called .toObject() coming in 1.1.
You can find it here: https://github.com/neo4j/neo4j-javascript-driver/blob/1.1/src/v1/record.js#L84-L91

@lutovich
Copy link
Contributor

Hi @joefraley,

Thanks for raising this issue. What do you think about the solution suggested/added by @legraphista?

@ariesshrimp
Copy link
Author

Yeah, that's the idea. Are you open to making the method return a promise instead? A record could be pretty large. I'm thinking of the browser API fetch, whose response object has a json() method that does basically the same thing.

@lutovich
Copy link
Contributor

@joefraley method toObject() iterates over data that is already in memory, it does not perform network calls and does not fetch things lazily. That is why I think it is reasonable to have it return just an object. What do you think?

@ariesshrimp
Copy link
Author

That makes sense, I guess the value unwrapping would always happen in a promise chain anyway (after session.run()). I think there could be user performance gains by liberating the event loop, but they would only be noticeable for very large records. I think it's fine to leave toObject() as a synchronous operation. 👌

@sean-stanley
Copy link

Actually I tried using your toObject as is and it could benefit from some refinement for easy use still.

Basically for easy objectifying we want the keys and _fields array zipped together so that

 const keys = { keys: ['n._id', 'n.name'] }
const fields = { _fields: ['123124', 'Joe'] }

becomes the standard form { _id: '123124', name: 'joe' }

maybe there is a way to get closer to this kind of thing already? I am just learning neo4j and cypher now.

@lutovich
Copy link
Contributor

Hi @sean-stanley,

I think you can achieve this using aliases in Cypher.
Presumably your current query looks smth like MATCH (n) RETURN n._id, n.name. If you change it to MATCH (n) RETURN n._id AS _id, n.name AS name resulting object should be {_id: ..., name: ...}.

Hope this helps.

@jexp
Copy link
Member

jexp commented Jan 19, 2017

Hey @sean-stanley our colleague @oskarhane provided such a function in this demo:

https://jsfiddle.net/oskarhane/amxrwkmo/

@ariesshrimp
Copy link
Author

ariesshrimp commented Jan 20, 2017

@sean-stanley that's exactly what I expected actually, I haven't tried the method yet.

@lutovich I haven't run into any case where I would rather RETURN n.name AS name in my query than simply have the keys and values zipped together by toObject()

If you did RETURN n.name AS name and toObject() had zip behavior, would things break? Maybe that's the problem: being forced to support possible cypher queries of that form.

@sean-stanley
Copy link

Actually it does behave as expected my bad. I setup a sample case incorrectly. Using the n.name as name method also works well. It's a bit annoying when you need half a dozen fields to do that but for the simple queries it worked well.

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

5 participants