Description
Neo4j Version: 4.0 Enterprise Docker
Neo4j Mode: Single instance
Driver version: JS driver ^4.0.2
Operating System: Windows 10
Steps to reproduce
- Start Neo4j
- Run a query with the driver (return datetime())
Expected behavior
I expect the datetime() that is being returned to be in a string format.
Actual behavior
It parses the value into a custom object with properties like day, month, year, etc.
I can see how this can be useful but the string value is just as valuable. If I want to send the date property to the frontend app I would always have to convert it otherwise the toString function will be lost on the object.
It would be nice to have an option on a per query basis or even better a global options setting to disable parsing the datetime and just return the string value.
This is my horrible temporary workaround.
if (result.records) {
for (const record of result.records) {
for (const recordKey of record.keys) {
const object = record.get(recordKey);
if (!Array.isArray(object)) {
for (const key in object) {
if (object.hasOwnProperty(key) && isDateTime(object[key])) {
object[key] = object[key].toString();
}
}
}
}
}
}
PS: this does not take into account nested objects or arrays.
Activity
Overdrivr commentedon Sep 7, 2022
I'm facing a similar issue, is it due to the fact that
neo4j-driver
serializes as a JSON object (with fields year, month, day, etc.) instead of an ISO date string ?Most libs that deal with dates try to stick to native JS Dates, is there a motivation for not doing this here ?
bigmontz commentedon Sep 15, 2022
Neo4j has a well define type system, so different temporal types has their own representation inside the database. These types are also mapped in specialised structures in the client/server communication and in the code itself. This is the reason we have all this temporal types in the javascript driver.
We could provide
toJSON
method to these classes, this could avoid the extra-steps when you just want to serialize the record to json.bigmontz commentedon Sep 21, 2022
We actually could not provide the
.toJSON
method since it will be a breaking change since it is possible to have code depending on the current behaviour.