surrealdb / surrealdb.js

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

Feature: Parse object- and array based record ID #215

Closed OTheNonE closed 3 months ago

OTheNonE commented 5 months ago

Is your feature request related to a problem?

I have the following table (shown in JS):

let record = {
    id: "measurement:{ date: "2024.01.01T00.00.00", device: "my-device" }"
    temperature: 23.5,
    pressure: 102456,
    humidity: 34,
}

I want to use the date value in this record, but find myself having to parse the id in order to use it. Instead, i just add a duplicate field of date to the record:

let record = {
    id: "measurement:{ date: "2024.01.01T00.00.00", device: "my-device" }"
    date: "2024.01.01T00.00.00"
    temperature: 23.5,
    pressure: 102456,
    humidity: 34,
}

Describe the solution

When using object- or array based record ID's, it would be nice to have the id parse. It could look something like this:

let record = {
    table: "measurement",
    id: { 
        date: "2024.01.01T00.00.00", 
        device: "my-device" 
    },
    temperature: 23.5,
    pressure: 102456,
    humidity: 34,
}

In this example, the id is split into two properties, table and id which might not be preferable, but something along this way would be awesome!

Alternative methods

Alternative methods are to parse the id, or add extra properties on the record.

SurrealDB version

surreal 1.2.0 for windows

SurrealDB.js version

0.11.0

Contact Details

olavnon@gmail.com

Is there an existing issue for this?

Code of Conduct

OTheNonE commented 5 months ago

I notice now in the Docs, that they are duplicating the values, which was my workaround solution. Does it take up more space in the database by having duplicates, or does the id and the date refer to the same storage value? My overall concerns around this problem is trying to have the records sizes as small as possible in the database.

kearfy commented 5 months ago

Hey @OTheNonE, since the introduction of a CBOR protocol I've been working on a refreshed version of this library in the jsv2 branch, which adds native support for all SurrealQL including Record IDs. Will hopefully ship it sooner than later!

OTheNonE commented 5 months ago

Well awesome! Can’t wait to see the result!

kearfy commented 3 months ago

Hey @OTheNonE, the v1.0.0-beta.x releases now ship with a RecordId class, adding support for this functionality! 😃

OTheNonE commented 3 months ago

Awesome! Cant wait to try it out, thank you for your great effort💪🏻