Skip to content

Commit 77a874e

Browse files
authored
Clean up the public API (#382)
1 parent 3f12cf6 commit 77a874e

22 files changed

+263
-71
lines changed

src/data/cbor.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,22 @@ export const replacer = {
160160

161161
Object.freeze(replacer);
162162

163+
/**
164+
* Recursively encode any supported SurrealQL value into a binary CBOR representation.
165+
* @param data - The input value
166+
* @returns CBOR binary representation
167+
*/
163168
export function encodeCbor<T>(data: T): ArrayBuffer {
164169
return encode(data, {
165170
replacer: replacer.encode,
166171
});
167172
}
168173

174+
/**
175+
* Decode a CBOR encoded SurrealQL value into object representation.
176+
* @param data - The encoded SurrealQL value
177+
* @returns The parsed SurrealQL value
178+
*/
169179
// biome-ignore lint/suspicious/noExplicitAny: Don't know what it will return
170180
export function decodeCbor(data: ArrayBufferLike): any {
171181
return decode(data, {

src/data/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export {
22
RecordId,
33
StringRecordId,
44
type RecordIdValue,
5-
escape_ident,
65
} from "./types/recordid.ts";
76
export {
87
Range,

src/data/types/decimal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Value } from "../value";
22

3+
/**
4+
* A SurrealQL decimal value.
5+
*/
36
export class Decimal extends Value {
47
readonly decimal: string;
58

src/data/types/duration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const durationPartRegex = new RegExp(
3232
`^(\\d+)(${Array.from(units.keys()).join("|")})`,
3333
);
3434

35+
/**
36+
* A SurrealQL duration value.
37+
*/
3538
export class Duration extends Value {
3639
readonly _milliseconds: number;
3740

src/data/types/future.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Value } from "../value";
22

3+
/**
4+
* An uncomputed SurrealQL future value.
5+
*/
36
export class Future extends Value {
47
constructor(readonly inner: string) {
58
super();

src/data/types/geometry.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Value } from "../value.ts";
22
import { Decimal } from "./decimal.ts";
33

4+
/**
5+
* A SurrealQL geometry value.
6+
*/
47
export abstract class Geometry extends Value {
58
abstract toJSON(): GeoJson;
69
abstract is(geometry: Geometry): boolean;
@@ -21,6 +24,9 @@ function f(num: number | Decimal) {
2124
return num;
2225
}
2326

27+
/**
28+
* A SurrealQL point geometry value.
29+
*/
2430
export class GeometryPoint extends Geometry {
2531
readonly point: [number, number];
2632

@@ -56,6 +62,9 @@ export class GeometryPoint extends Geometry {
5662
}
5763
}
5864

65+
/**
66+
* A SurrealQL line geometry value.
67+
*/
5968
export class GeometryLine extends Geometry {
6069
readonly line: [GeometryPoint, GeometryPoint, ...GeometryPoint[]];
6170

@@ -108,6 +117,9 @@ export class GeometryLine extends Geometry {
108117
}
109118
}
110119

120+
/**
121+
* A SurrealQL polygon geometry value.
122+
*/
111123
export class GeometryPolygon extends Geometry {
112124
readonly polygon: [GeometryLine, ...GeometryLine[]];
113125

@@ -153,6 +165,9 @@ export class GeometryPolygon extends Geometry {
153165
}
154166
}
155167

168+
/**
169+
* A SurrealQL multi-point geometry value.
170+
*/
156171
export class GeometryMultiPoint extends Geometry {
157172
readonly points: [GeometryPoint, ...GeometryPoint[]];
158173

@@ -193,6 +208,9 @@ export class GeometryMultiPoint extends Geometry {
193208
}
194209
}
195210

211+
/**
212+
* A SurrealQL multi-line geometry value.
213+
*/
196214
export class GeometryMultiLine extends Geometry {
197215
readonly lines: [GeometryLine, ...GeometryLine[]];
198216

@@ -231,6 +249,9 @@ export class GeometryMultiLine extends Geometry {
231249
}
232250
}
233251

252+
/**
253+
* A SurrealQL multi-polygon geometry value.
254+
*/
234255
export class GeometryMultiPolygon extends Geometry {
235256
readonly polygons: [GeometryPolygon, ...GeometryPolygon[]];
236257

@@ -275,6 +296,9 @@ export class GeometryMultiPolygon extends Geometry {
275296
}
276297
}
277298

299+
/**
300+
* A SurrealQL geometry collection value.
301+
*/
278302
export class GeometryCollection extends Geometry {
279303
readonly collection: [Geometry, ...Geometry[]];
280304

src/data/types/range.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { Tagged } from "../../cbor";
22
import { SurrealDbError } from "../../errors";
33
import { equals } from "../../util/equals";
4+
import { escapeIdent } from "../../util/escape";
45
import { toSurrealqlString } from "../../util/to-surrealql-string";
56
import { TAG_BOUND_EXCLUDED, TAG_BOUND_INCLUDED } from "../cbor";
67
import { Value } from "../value";
7-
import {
8-
type RecordIdValue,
9-
escape_id_part,
10-
escape_ident,
11-
isValidIdPart,
12-
} from "./recordid";
8+
import { type RecordIdValue, escapeIdPart, isValidIdPart } from "./recordid";
139

10+
/**
11+
* A SurrealQL range value.
12+
*/
1413
export class Range<Beg, End> extends Value {
1514
constructor(
1615
readonly beg: Bound<Beg>,
@@ -34,8 +33,8 @@ export class Range<Beg, End> extends Value {
3433
}
3534

3635
toString(): string {
37-
const beg = escape_range_bound(this.beg);
38-
const end = escape_range_bound(this.end);
36+
const beg = escapeRangeBound(this.beg);
37+
const end = escapeRangeBound(this.end);
3938
return `${beg}${getRangeJoin(this.beg, this.end)}${end}`;
4039
}
4140
}
@@ -49,6 +48,9 @@ export class BoundExcluded<T> {
4948
constructor(readonly value: T) {}
5049
}
5150

51+
/**
52+
* A SurrealQL record ID range value.
53+
*/
5254
export class RecordIdRange<Tb extends string = string> extends Value {
5355
constructor(
5456
public readonly tb: Tb,
@@ -78,9 +80,9 @@ export class RecordIdRange<Tb extends string = string> extends Value {
7880
}
7981

8082
toString(): string {
81-
const tb = escape_ident(this.tb);
82-
const beg = escape_id_bound(this.beg);
83-
const end = escape_id_bound(this.end);
83+
const tb = escapeIdent(this.tb);
84+
const beg = escapeIdBound(this.beg);
85+
const end = escapeIdBound(this.end);
8486
return `${tb}:${beg}${getRangeJoin(this.beg, this.end)}${end}`;
8587
}
8688
}
@@ -99,13 +101,13 @@ function isValidIdBound(bound: Bound<unknown>): bound is Bound<RecordIdValue> {
99101
: true;
100102
}
101103

102-
function escape_id_bound(bound: Bound<RecordIdValue>): string {
104+
function escapeIdBound(bound: Bound<RecordIdValue>): string {
103105
return bound instanceof BoundIncluded || bound instanceof BoundExcluded
104-
? escape_id_part(bound.value)
106+
? escapeIdPart(bound.value)
105107
: "";
106108
}
107109

108-
function escape_range_bound(bound: Bound<unknown>): string {
110+
function escapeRangeBound(bound: Bound<unknown>): string {
109111
if (bound === undefined) return "";
110112
const value = bound.value;
111113

src/data/types/recordid.ts

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { SurrealDbError } from "../../errors";
22
import { equals } from "../../util/equals";
3+
import { escapeIdent, escapeNumber } from "../../util/escape";
34
import { toSurrealqlString } from "../../util/to-surrealql-string";
45
import { Value } from "../value";
56
import { Uuid } from "./uuid";
67

7-
const MAX_i64 = 9223372036854775807n;
88
export type RecordIdValue =
99
| string
1010
| number
@@ -13,6 +13,9 @@ export type RecordIdValue =
1313
| unknown[]
1414
| Record<string, unknown>;
1515

16+
/**
17+
* A SurrealQL record ID value.
18+
*/
1619
export class RecordId<Tb extends string = string> extends Value {
1720
public readonly tb: Tb;
1821
public readonly id: RecordIdValue;
@@ -38,12 +41,15 @@ export class RecordId<Tb extends string = string> extends Value {
3841
}
3942

4043
toString(): string {
41-
const tb = escape_ident(this.tb);
42-
const id = escape_id_part(this.id);
44+
const tb = escapeIdent(this.tb);
45+
const id = escapeIdPart(this.id);
4346
return `${tb}:${id}`;
4447
}
4548
}
4649

50+
/**
51+
* A SurrealQL string-represented record ID value.
52+
*/
4753
export class StringRecordId extends Value {
4854
public readonly rid: string;
4955

@@ -77,41 +83,6 @@ export class StringRecordId extends Value {
7783
}
7884
}
7985

80-
export function escape_number(num: number | bigint): string {
81-
return num <= MAX_i64 ? num.toString() : `⟨${num}⟩`;
82-
}
83-
84-
export function escape_ident(str: string): string {
85-
// String which looks like a number should always be escaped, to prevent it from being parsed as a number
86-
if (isOnlyNumbers(str)) {
87-
return `⟨${str}⟩`;
88-
}
89-
90-
let code: number;
91-
let i: number;
92-
let len: number;
93-
94-
for (i = 0, len = str.length; i < len; i++) {
95-
code = str.charCodeAt(i);
96-
if (
97-
!(code > 47 && code < 58) && // numeric (0-9)
98-
!(code > 64 && code < 91) && // upper alpha (A-Z)
99-
!(code > 96 && code < 123) && // lower alpha (a-z)
100-
!(code === 95) // underscore (_)
101-
) {
102-
return `⟨${str.replaceAll("⟩", "\\⟩")}⟩`;
103-
}
104-
}
105-
106-
return str;
107-
}
108-
109-
export function isOnlyNumbers(str: string): boolean {
110-
const stripped = str.replace("_", "");
111-
const parsed = Number.parseInt(stripped);
112-
return !Number.isNaN(parsed) && parsed.toString() === stripped;
113-
}
114-
11586
export function isValidIdPart(v: unknown): v is RecordIdValue {
11687
if (v instanceof Uuid) return true;
11788

@@ -127,12 +98,12 @@ export function isValidIdPart(v: unknown): v is RecordIdValue {
12798
}
12899
}
129100

130-
export function escape_id_part(id: RecordIdValue): string {
101+
export function escapeIdPart(id: RecordIdValue): string {
131102
return id instanceof Uuid
132103
? `u"${id}"`
133104
: typeof id === "string"
134-
? escape_ident(id)
105+
? escapeIdent(id)
135106
: typeof id === "bigint" || typeof id === "number"
136-
? escape_number(id)
107+
? escapeNumber(id)
137108
: toSurrealqlString(id);
138109
}

src/data/types/table.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { SurrealDbError } from "../../errors";
22
import { Value } from "../value";
33

4+
/**
5+
* A SurrealQL table value.
6+
*/
47
export class Table<Tb extends string = string> extends Value {
58
public readonly tb: Tb;
69

src/data/types/uuid.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { UUID, uuidv4obj, uuidv7obj } from "uuidv7";
22
import { Value } from "../value";
33

4+
/**
5+
* A SurrealQL UUID value.
6+
*/
47
export class Uuid extends Value {
58
private readonly inner: UUID;
69

src/data/value.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* A complex SurrealQL value type
3+
*/
14
export abstract class Value {
25
/**
36
* Compare equality with another value.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export * from "./util/version-check.ts";
1313
export * from "./util/get-incremental-id.ts";
1414
export * from "./util/string-prefixes.ts";
1515
export * from "./util/to-surrealql-string.ts";
16+
export * from "./util/escape.ts";
1617
export {
1718
ConnectionStatus,
1819
AbstractEngine,

0 commit comments

Comments
 (0)