-
Notifications
You must be signed in to change notification settings - Fork 151
Introduce 'disableLosslessIntegers' config option #323
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
Conversation
This boolean option allows users to make driver accept and represent integer values as native JavaScript `Number`s instead of driver's special `Integer`s. When this option is set to `true` driver will fail to encode `Integer` values passed as query parameters. All returned `Record`, `Node`, `Relationship`, etc. will have their integer fields as native `Number`s. Object returned by `Record#toObject()` will also contain native numbers. **Warning:** enabling this setting can potentially make driver return lossy integer values. This would be the case when database contain integer values which do not fit in `[Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]` range. Such integers will then be represented as `Number.NEGATIVE_INFINITY` or `Number.POSITIVE_INFINITY`, which is lossy and different from what is actually stored in the database. That is why this option is set to `false` by default. Driver's `Integer` class is able to represent integer values beyond `[Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]` and thus is a much safer option in the general case.
I'm unsure about two things:
|
As per your points;
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good to me.
What would you think about adding an toObject()
overload which will convert Integer
instances to number
types although Driver is not configured to use native numbers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me as well.
And I agree with the changes that if you opt out of using the integer type when receiving results you should also not send integer types as params.
btw, I'm +1 on @ali-ince param name suggestion |
agree with what @ali-ince has suggested - this config option could also be received by Lets say majority of my cases can be handled by JS Numbers and in a few cases I need the constructor - If, its the other way around, constructor - also, the param could specify that we are forcing JS Numbers. Native might be confusing as in - native to javascript or native to neo4j - hence |
@lutovich you could make it a generic, so the person decides what type it should use, as such: declare class Node<T = Integer> {
identity: T;
// ...
} that means, if the person decides to not explicitly decide which type |
New name seems more descriptive and emphasizes that numbers returned from driver with this setting set to `true` might result in loss of precision.
`Integer` will now be allowed in query parameters even when native JS numbers are configured on the driver level. This is done to not cut away a valid neo4j integer type. It will also result in better backwards compatibility - existing applications will not need to change all `Integer` query parameters when they set `disableLosslessIntegers=true`.
@pocesar thanks a lot for your reply! The only problem I see with Here is a possible solution: declare class Node<T extends (Integer | number) = Integer> {
identity: T;
// ...
} it's essentially the same thing but adds an additional generic type bound. So no code change for existing users, since What do you think? |
Previously TS declaration files only allowed returned integer types to be `neo4j.Integer`. This was not compatible with newly introduced `disableLosslessIntegers` config, which makes driver always return native JS numbers. This commit allows all integer properties to either be `neo4j.Integer` or `number`. `Integer` is default. Existing TS users will not need to change their code. TS users who set 'disableLosslessIntegers=true' will have to specify generic type explicitly like `Node<number>`. Property `identity` of such node will have type `number`.
PR updated and should now be ready for a final review. Changes:
See commit messages for more details. @eelsweb we've decided to only expose This change will hopefully be merged in 1.6 and go through alpha, beta and RC stages. Feedback is very valuable! |
@lutovich if the declare type NumberInteger = Integer | number;
declare class Node<T extends NumberInteger = Integer> {
} so it can be reused elsewhere |
To deduplicate and reuse `(number | Integer)` generic type declaration.
@lutovich this kind of over-riding is actually quite standard practice For instance - Google's Nodejs Lib for their APIs View the section Options
For this neo4j library, consider a case where 80 pc of my functions work with |
@eelsweb is this an actual use-case you have or just a theoretical concern? Imho |
Agreed 😄 |
This boolean option allows users to make driver accept and represent integer values as native JavaScript
Number
s instead of driver's specialInteger
s. When this option is set totrue
driver will fail to encodeInteger
values passed as query parameters. All returnedRecord
,Node
,Relationship
, etc. will have their integer fields as nativeNumber
s. Object returned byRecord#toObject()
will also contain native numbers.Warning: enabling this setting can potentially make driver return lossy integer values. This would be the case when database contain integer values which do not fit in
[Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]
range. Such integers will then be represented asNumber.NEGATIVE_INFINITY
orNumber.POSITIVE_INFINITY
, which is lossy and different from what is actually stored in the database. That is why this option is set tofalse
by default. Driver'sInteger
class is able to represent integer values beyond[Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER]
and thus is a much safer option in the general case.Fixes #322
Related to #245, #225, #122