Skip to content

Commit b3e4228

Browse files
authored
Fix record id printing, updates isows (#351)
1 parent d15a376 commit b3e4228

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"tslib": "^2.6.3"
2323
},
2424
"dependencies": {
25-
"isows": "^1.0.4",
25+
"isows": "^1.0.6",
2626
"uuidv7": "^1.0.1"
2727
},
2828
"scripts": {

src/data/types/recordid.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SurrealDbError } from "../../errors";
2+
import { toSurrealqlString } from "../../util/to-surrealql-string";
23
import { Uuid } from "./uuid";
34

45
const MAX_i64 = 9223372036854775807n;
@@ -109,5 +110,5 @@ export function escape_id_part(id: RecordIdValue): string {
109110
? escape_ident(id)
110111
: typeof id === "bigint" || typeof id === "number"
111112
? escape_number(id)
112-
: JSON.stringify(id);
113+
: toSurrealqlString(id);
113114
}

src/util/to-surrealql-string.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import {
1111
} from "../data";
1212

1313
export function toSurrealqlString(input: unknown): string {
14-
if (typeof input === "object") {
15-
if (input === null) return "NULL";
16-
if (input === undefined) return "NONE";
14+
if (typeof input === "string") return `s${JSON.stringify(input)}`;
15+
if (input === null) return "NULL";
16+
if (input === undefined) return "NONE";
1717

18+
if (typeof input === "object") {
1819
// We explicitely use string prefixes to ensure compability with both SurrealDB 1.x and 2.x
1920
if (input instanceof Date) return `d${JSON.stringify(input.toISOString())}`;
2021
if (input instanceof Uuid) return `u${JSON.stringify(input.toString())}`;
2122
if (input instanceof RecordId || input instanceof StringRecordId)
2223
return `r${JSON.stringify(input.toString())}`;
23-
if (typeof input === "string") return `s${JSON.stringify(input)}`;
2424

2525
if (input instanceof Geometry) return toSurrealqlString(input.toJSON());
2626

@@ -38,27 +38,27 @@ export function toSurrealqlString(input: unknown): string {
3838
switch (Object.getPrototypeOf(input)) {
3939
case Object.prototype: {
4040
let output = "{ ";
41-
for (const [k, v] of Object.entries(input as object)) {
42-
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)},`;
41+
const entries = Object.entries(input as object);
42+
for (const [i, [k, v]] of entries.entries()) {
43+
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)}`;
44+
if (i < entries.length - 1) output += ", ";
4345
}
4446
output += " }";
4547
return output;
4648
}
4749
case Map.prototype: {
4850
let output = "{ ";
49-
for (const [k, v] of (input as Map<unknown, unknown>).entries()) {
50-
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)},`;
51+
const entries = Array.from((input as Map<unknown, unknown>).entries());
52+
for (const [i, [k, v]] of entries.entries()) {
53+
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)}`;
54+
if (i < entries.length - 1) output += ", ";
5155
}
5256
output += " }";
5357
return output;
5458
}
5559
case Array.prototype: {
56-
let output = "[ ";
57-
for (const v of input as unknown[]) {
58-
output += `${toSurrealqlString(v)},`;
59-
}
60-
output += " ]";
61-
return output;
60+
const array = (input as unknown[]).map(toSurrealqlString);
61+
return `[ ${array.join(", ")} ]`;
6262
}
6363
case Set.prototype: {
6464
const set = new Set([...(input as [])].map(toSurrealqlString));

tests/unit/__snapshots__/jsonify.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ exports[`jsonify matches snapshot 1`] = `
9292
"id_looks_like_number": "⟨some:thing⟩:⟨123⟩",
9393
"null": null,
9494
"num": 123,
95-
"range": "r"bla:a"..z",
96-
"range_unbounded": "..z",
95+
"range": "r"bla:a"..s"z"",
96+
"range_unbounded": "..s"z"",
9797
"rid": "⟨some:thing⟩:under_score",
9898
"rng_rid": "bla:a..z",
9999
"str_rid": "⟨some:thing⟩:under_score",

tests/unit/recordid.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ describe("record ids", () => {
2222
);
2323

2424
// Objects and arrays
25-
expect(new RecordId("table", { city: "London" }).toString()).toBe(
26-
'table:{"city":"London"}',
27-
);
25+
expect(
26+
new RecordId("table", {
27+
city: "London",
28+
date: new Date("2024-10-02T08:35:48.715Z"),
29+
}).toString(),
30+
).toBe('table:{ "city": s"London", "date": d"2024-10-02T08:35:48.715Z" }');
2831

2932
expect(new RecordId("table", ["London"]).toString()).toBe(
30-
'table:["London"]',
33+
'table:[ s"London" ]',
3134
);
3235
});
3336
});

0 commit comments

Comments
 (0)