toSurql
The toSurql
function converts JavaScript values into SurrealQL strings.
Import
import { toSurql } from "@tai-kun/surrealdb/utils";
Usage
function toSurql(value: unknown): string;
Arguments
value
A JavaScript value. It accepts one of the following types:
string
number
bigint
boolean
undefined
null
{ toSurql(): string }
Date
{ toJSON(): string }
unknown[]
{ [key: string]: unknown }
Set<unknown>
Map<unknown, unknown>
unknown
represents a value composed of the types listed above.
The conversion prioritizes the types in the order listed. That is, even if a plain object has a .toJSON
function, if it also has a .toSurql
function, the latter will take precedence.
The is-plain-obj
library is used to determine if an object is a plain object. Therefore, if a plain object does not have .toSurql
or .toJSON
methods, and it has a Symbol.toStringTag
or Symbol.iterator
property, it will not be treated as a plain object and will throw an error.
Return Value
A SurrealQL string.
Example
import { toSurql } from "@tai-kun/surrealdb/utils";
const surql = toSurql({ bigint: 9007199254740992n, // Number.MAX_SAFE_INTEGER + 1 boolean: [true, false], date: new Date("2024-06-01T12:34:56.789Z"), map: new Map([ [0, { toJSON: () => [0, 1], }], ]), null: null, number: 123, set: new Set([{ toSurql: () => `<bytes>"hello"`, }]), string: "String", undefined: undefined,});
console.log(surql);
Output:
{bigint:9007199254740992,boolean:[true,false],date:d'2024-06-01T12:34:56.789Z',map:{0:[0,1]},null:NULL,number:123,set:[<bytes>"hello"],string:'String',undefined:NONE}