Skip to content

Table

The Table class represents a table name as a record ID.

Import

import { Table } from "@tai-kun/surrealdb/decodeonly-datatypes";
import { Table } from "@tai-kun/surrealdb/encodable-datatypes";
import { Table } from "@tai-kun/surrealdb/standard-datatypes";

.constructor()

Creates a new Table object.

new Table(source: string); // decode-only, encodable
new Table(source: string | { name: string }); // standard

Arguments

source

The table name or a Table object. If the preset is less than standard, only the table name can be passed.

Return Value

When called with new, Table returns its instance.

.name instance property >=decode-only

The table name. The string is not escaped when retrieving this property.

.name: string;

Example

The string is not escaped when retrieving this property.

import { Table } from "@tai-kun/surrealdb/decodeonly-datatypes";
const table = new Table("foo-bar");
console.log(table.name);
//-> foo-bar

.toString() instance method >=encodable

Converts the Table object into a string usable as a variable in SurrealQL.

.valueOf() and .toJSON() return the same result as this method.

.toString(): string;

Arguments

None.

Return Value

Returns the table name.

Example

import { Table } from "@tai-kun/surrealdb/encodable-datatypes";
const table = new Table("foo-bar");
console.log(table.toString());
//-> foo-bar

.toSurql() instance method >=encodable

Converts the Table object into a string embeddable in SurrealQL. Similar to .toString(), but the table name is escaped as implemented in SurrealDB.

.toSurql(): string;

Arguments

None.

Return Value

Returns the escaped string of the table name.

Example

import { Table } from "@tai-kun/surrealdb/encodable-datatypes";
const table = new Table("foo-bar");
console.log(table.toSurql());
//-> `foo-bar`

.clone() instance method =standard

Creates a copy of the Table object.

.clone(): this;

Arguments

None.

Return Value

Returns a new Table object. If inheriting from the Table class, an instance of that class is returned.

Advanced Usage

Type Guard

You can make the table name of Table type-safe using type parameters:

import { Table } from "@tai-kun/surrealdb/standard-datatypes";
function isUserTable(table: unknown): table is Table<"user"> {
return table instanceof Table
&& table.name === "user"
}

Generic Table Object Check

To determine if a value is a Table object regardless of the preset, it is recommended to use the isTable function. This function does not use instanceof, making it useful when the preset of the value being checked is unknown.

import { isTable } from "@tai-kun/surrealdb";