Skip to content
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

toJSON and StringRecordId #256

Merged
merged 6 commits into from
May 6, 2024
Merged

toJSON and StringRecordId #256

merged 6 commits into from
May 6, 2024

Conversation

kearfy
Copy link
Member

@kearfy kearfy commented May 6, 2024

Thank you for submitting this pull request! We appreciate you spending the time to work on these changes.

What is the motivation?

There is value for some scenarios being able to obtain a JSON representation of the response like you were able to do with the pre-v1 version of the JS library.

What does this change do?

This PR implements toJSON methods where required to make this possibly again.
Additionally, this PR also introduces a StringRecordId class, which will send over the string record id to the server to leave it to parse it.

Here's a simple example of what you will now be able to do:

// Use the selection, creation and altering methods with string record ids
const res = await db.select(new StringRecordId("person:john"));

// You will get back a response with native values
// However will `toJSON()` or `JSON.stringify()` you will get back a JSON representation of the result, the way that SurrealDB would also display it
const jsonlike = res.toJSON();
const jsonstring = JSON.stringify(res);

// You can also easily do this in one go
await db
    .select(new StringRecordId("person:john"))
    .then(res => res.toJSON());

What is your testing strategy?

Added a snapshot-based unit test which compares the JSON-encoded value against a snapshot of what it should be. Also added a test to ensure that a string record id sent to the server is properly parsed by the server.

Is this related to any issues?

Have you read the Contributing Guidelines?

@kearfy kearfy merged commit f63c95c into main May 6, 2024
2 checks passed
@kearfy kearfy deleted the micha/tojson-and-stringrecordid branch May 6, 2024 19:08
@kearfy kearfy mentioned this pull request May 8, 2024
1 task
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

Successfully merging this pull request may close these issues.

1 participant