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

Feature: Add a "reviver" function to jsonify #388

Open
2 tasks done
reinhard-sanz opened this issue Dec 10, 2024 · 0 comments
Open
2 tasks done

Feature: Add a "reviver" function to jsonify #388

reinhard-sanz opened this issue Dec 10, 2024 · 0 comments

Comments

@reinhard-sanz
Copy link

reinhard-sanz commented Dec 10, 2024

Is your feature request related to a problem?

I admit a very niche usecase, but i still think a potentially useful one.
I've got a Sveltekit app and i am sending db data from the server to the client. Sveltekit is able to to send POJOs to the client, and is even auto parsing Date instances accordingly, so the user does not need to add special handling to stringify and parse said Date.
What it (logically) is not able to handle stuff like RecordId, which can be nicely handled with jsonify though.
The thing if jsonify parses datetimes into strings, reintroducing the need to add some special handling on the client side to parse the datetime strings back into Date instances.

Describe the solution

It would be nice to have some sort of reviver function on jsonify. Similar to the reviver of JSON.parse(). Enabling users to add custom handling to specific fields if needed. This would enable me to easily ignore Date during conversion.

Maybe something like this:

return jsonify(entries, (key, value, resolve) => {
	// If value is instance of Date, do nothimg 
	if (value instanceof Date) {
            return value;
	}

        // on every other case call the default value resolve function
	return resolve(value);
});

Alternative methods

Implementing some custom handling like this:

const test = JSON.parse(JSON.stringify(users), (key, value) => {
	// one needs to know the keys to be able to do this
	if (key === 'datetime_field') {
		return new Date(value);
	}
	return value;
});

This seems very resource intensive and requires prior knowledge of the data structure.

SurrealDB version

surreal 2.1.2 for linux on x86_64

JavaScript SDK version

1.1.0

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
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

No branches or pull requests

1 participant