Thing
The Thing
class represents a record ID. See Record IDs for more information.
Importing
import { Thing } from "@tai-kun/surrealdb/decodeonly-datatypes";import { Thing } from "@tai-kun/surrealdb/encodable-datatypes";import { Thing } from "@tai-kun/surrealdb/standard-datatypes";
.constructor()
Creates a new Thing
object.
new Thing(source: [table: string, id: string | number | bigint | object]);new Thing(table: string, id: string | number | bigint | object);
Arguments
source
A pair representing the table and ID parts of the record ID.
table
The table part of the record ID.
id
The ID part of the record ID.
Return Value
When called with new
, Thing
returns a new instance of itself.
.table
instance property >=decode-only
The table part of the record ID. This property returns the string unescaped.
.table: string;
Example
This property returns the string unescaped.
import { Thing } from "@tai-kun/surrealdb/decodeonly-datatypes";
const thing = new Thing("foo-bar", 0);console.log(thing.table); // Note: Corrected to thing.table//-> foo-bar
.id
instance property >=decode-only
The ID part of the record ID.
.id: string | number | bigint | object;
.toString()
instance method >=encodable
Converts the Thing
object into a string usable as a variable in SurrealQL. The table name and ID are escaped and formatted identically to SurrealDB’s implementation. Object properties are sorted in ascending order.
.valueOf()
and .toJSON()
return the same result as this method.
.toString(): string;
Arguments
None.
Return Value
Returns a string formatted as a record ID.
Example
import { Thing, Uuid } from "@tai-kun/surrealdb/encodable-datatypes";
const thing = new Thing([ "foo-bar", new Uuid("018fb2c0-7bb7-7fca-8308-b24d0be065dc"),]);console.log(thing.toString());//-> ⟨foo-bar⟩:u'018fb2c0-7bb7-7fca-8308-b24d0be065dc'
.toSurql()
instance method >=encodable
Converts the Thing
object into a string embeddable in SurrealQL. Similar to .toString()
, but adds an r
prefix to signal to the query parser that the string is a record ID.
.toSurql(): string;
Arguments
None.
Return Value
Returns a string formatted as a record ID.
Example
import { Thing } from "@tai-kun/surrealdb/encodable-datatypes";
const thing = new Thing("foo", "bar");console.log(thing.toSurql());//-> r'foo:bar'
.clone()
instance method =standard
Creates a copy of the Thing
object. Note that the ID part is referenced, not copied.
.clone(): this;
Arguments
None.
Return Value
Returns a new Thing
object. If inheriting from the Thing
class, returns an instance of that class.
Advanced Usage
Type Guards
The table and ID parts of a Thing
can be type-safe using type parameters:
import { Thing, Uuid } from "@tai-kun/surrealdb/standard-datatypes";
function isUserRecordId(thing: unknown): thing is Thing<"user", Uuid> { return thing instanceof Thing && thing.table === "user" && thing.id instanceof Uuid}
Generic Thing
Object Check
To determine if a value is a Thing
object without relying on presets, it is recommended to use the isThing
function. This function does not use instanceof
, making it useful when the preset of the value being checked is unknown.
import { isThing } from "@tai-kun/surrealdb";