pubkey / rxdb

A fast, local first, reactive Database for JavaScript Applications https://rxdb.info/
https://rxdb.info/
Apache License 2.0
21k stars 1.02k forks source link

Bug / Feature Request: Unquoted Numeric Primary Key Values from HTTP Server are Interpreted as Integar. #6089

Closed MrChadMWood closed 1 month ago

MrChadMWood commented 1 month ago

BUG:

While using HTTP replication with Dixie storage, I've noticed that odd behaviors occur if the HTTP server provides an unquoted integer for the primaryKey value. A console error is thrown, stating that primaryKey.trim() is not a function. This somehow cascaded into other issues, and some unrelated React components began to act weird. I manually reviewed IndexedDB and found that the primary key values were in fact being stored as integers.

I don't think there's any actual standard on how to interpret integers versus strings in the query parameters, (REF: RFC3986), but it would be good IMO to try automatically casting the primaryKey value to string. I feel this way because RxDB seems to require string type for the primary key, so it's a bit strange to me that it wouldn't at-least try enforcing that via casting.

I resolved this issue by modifying my server component to quote the primary key value. Depending on perspective, I think this can be a bug or feature request.

pubkey commented 1 month ago

PrimaryKey must be a string. This is defined by RxDB.

MrChadMWood commented 1 month ago

That's a fair point. At the same time, why isn't an unquoted number interpreted as string if the requirement is that the value be a string? It would not need to be quoted if it contained any text, but if the value is all numbers then it will be assumed to be an integer even though that will cause strange side-effects. It is defined as a string in the collection schema, but then stored as an integer. The behavior seems less intuitive to me.

At the very least, perhaps a better error would be a good idea. Something like, "Error: primary key is not a string". Otherwise, users may spend much time debugging why their app is acting strange because the current error is just primaryKey.trim() is not a function.

I don't think theres any standard that says an unquoted number in query params must be interpreted as an integer. Dare I say that maybe RxDB misinterpreted my unquoted numeric string?

Not sure if I'm missing something though. Thanks for your attention, I love the product :)

pubkey commented 1 month ago

These transformations and checks would decrease performance. Just pass the correct data format into RxDB. Dev-Mode will throw already if you do not define the primary key as string. Are you using the dev-mode plugin?

MrChadMWood commented 1 month ago

I am using dev-mode plugin. An error was thrown when I tried defining primary key as another type, so I changed it to string and there were no more errors at that stage.

My API continued sending records as {“primaryKey”:1234}. RxDB seems to have stored the primary key as integer inside indexedDB, because it was unquoted (even with the schema defined primary key as string). As soon as I adjust the API to send {“primaryKey”:”1234”}, I no longer got the primaryKey.trim() is not a function error.

The solution is fine for me. I just modified the API to serialize the field into string type at response time. If I did not have such control over the backend, I’m not sure what the right approach would be. Maybe some modifications in the pull handler?