surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
271 stars 46 forks source link

Feature: Convert RecordId from Class to JSON-object while maintaining JSON-structure #301

Open OTheNonE opened 4 days ago

OTheNonE commented 4 days ago

Is your feature request related to a problem?

I have a problem regarding using surrealdb.js on the server and having to send data from the server to the client. I want to send the RecordId of an record to the client in a JSON format instead of a tb:id-string. Though when trying to do this, i stumble upon the following problems:

Therefore, i have no simple way of returning data from the server to the client while maintaining the JSON-object in the recordId.

Describe the solution

It would be nice to have a utility function or a method implemented on the class that could convert the recordId class into JSON and maintain the JSON-structure:

const rid = new RecordId("recording", { city: "London", data: 123 });
const rid_json = rid.toJSONObject()
rid_json.city // "London"

// Send the rid_json:
fetch("https://example.com/abc", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(rid_json),
})

Alternative methods

Write my own utility function, or also store the data elsewhere than in the recordId.

SurrealDB version

surreal 1.5.3

SurrealDB.js version

1.0.0-beta-12

Contact Details

olavnon@gmail.com

Is there an existing issue for this?

Code of Conduct

kearfy commented 4 days ago

Heya! You should be able to simply obtain the id part by selecting the id property from the RecordId. The jsonify utility simply outputs the JSON-like value that SurrealDB would