Skip to content

Add dedicated @surrealdb/sqon package & add type-safe JSON format#591

Merged
macjuul merged 23 commits into
mainfrom
feat/sqon
May 28, 2026
Merged

Add dedicated @surrealdb/sqon package & add type-safe JSON format#591
macjuul merged 23 commits into
mainfrom
feat/sqon

Conversation

@macjuul
Copy link
Copy Markdown
Member

@macjuul macjuul commented Apr 7, 2026

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

What is the motivation?

SurrealDB offers a rich type system featuring an array of first party data values such as durations, record ids, and more. While the SDK offers bi-directional encoding and decoding for CBOR, it only offers one-way encoding for JSON, while also resulting in a loss of type information.

Without proper JSON encoding/decoding support, it becomes difficult to communicate SurrealDB values across application boundaries reliably. Some examples of where this new format will help include preserving type-safety across REST APIs, storing values in an easy to debug format, and communicating data with LLMs.

What does this change do?

The over al goal of this PR is to improve the portability of SurrealDB values (So called SQON-values). This is done by extracting the existing value classes, codec logic, and certain helper values to a new @surrealdb/sqon package. This allows for the parsing and use of SQON values without requiring the presence of the entire SDK.

Additionally, a new type-safe JsonCodec has been added which makes use of a new EJSON inspired representation. The resulting structure is fully JSON compliant, and can be parsed back into SQON values.

For backwards compability, the SDK re-exports all @surrealdb/sqon exports.

Example

import { DateTime, Decimal, Duration, JsonCodec, RecordId, Uuid } from "@surrealdb/sqon";

// encoding a value

const result = JsonCodec.default.encode({
    id: new RecordId("table", Uuid.v4()),
    date: new DateTime(),
    decimal: new Decimal("123.456"),
    duration: new Duration("1h30m"),
    uuid: new Uuid("123e4567-e89b-12d3-a456-426614174000"),
});

// results in

{
  "id": {
    "$recordId": {
      "tb": "table",
      "id": {
        "$uuid": "37532d5b-775a-4208-bacd-2b4af45987e7"
      }
    }
  },
  "date": {
    "$datetime": "2026-04-07T20:08:35.801599292Z"
  },
  "decimal": {
    "$decimal": "123.456"
  },
  "duration": {
    "$duration": "1h30m"
  },
  "uuid": {
    "$uuid": "123e4567-e89b-12d3-a456-426614174000"
  }
}

toJSON

The existing toJSON() implementations on value classes currently return a legacy JSON structure which does not preserve type information.

You can configure Value classes to return the new JSON format by calling the following function during initialization.

Value.useExperimentalToJson();

What is your testing strategy?

Added tests

Is this related to any issues?

Have you read the Contributing Guidelines?

@macjuul macjuul marked this pull request as ready for review May 27, 2026 12:58
@macjuul macjuul merged commit bb8dcf1 into main May 28, 2026
27 checks passed
@macjuul macjuul deleted the feat/sqon branch May 28, 2026 09:19
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.

Bug: new StringRecordId("") causes the connection to be dropped.

2 participants