feature: add optional flatMode
flag to flatten surrealdb responses to plain js objects
#244
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is the motivation?
Switching from
JSON
toCBOR
and the subsequent introduction of classes such asRecordId
significantly lowers the DX in its current form, for example, when working with frameworks like SvelteKit. SvelteKit only supports data transmission between the server and the client in JSON format, and attempting to send the current response from SurrealDB will result in an error Error: Data returned from 'load' while rendering / is not serializable: Cannot stringify arbitrary non-POJOs.The
toJSON()
method available in the RecordId class solves this problem, but it does not improve DX because before any data can be sent, it must first be destructured and the method used in all occurrences ofRecordId
.What does this change do?
This change introduces an optional
flatMode
flag in the constructor of theSurreal
class. WhenflatMode: true
, all responses from SurrealDB that may contain RecordId are recursively flattened into a JavaScript object format via the flatten() function.Creating new instances:
Regular:
new Surreal()
flatMode:
new Surreal({ flatMode: true })
Example Outputs:
Regular:
flatMode:
What is your testing strategy?
I added tests based on existing ones, each test includes the suffix
- flatMode
. I also compared the performance between thequery
andquery - flatMode
tests, which show a performance difference of less than1ms
.Is this related to any issues?
#247
Related to, but this is partial or alternative solution for #241 #242
Also I saw related messages on Discord (but this is partial or alternative solution):
To do
Have you read the Contributing Guidelines?