Skip to content

Commit

Permalink
Fix record id printing, updates isows (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Oct 2, 2024
1 parent d15a376 commit b3e4228
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"tslib": "^2.6.3"
},
"dependencies": {
"isows": "^1.0.4",
"isows": "^1.0.6",
"uuidv7": "^1.0.1"
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/data/types/recordid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SurrealDbError } from "../../errors";
import { toSurrealqlString } from "../../util/to-surrealql-string";
import { Uuid } from "./uuid";

const MAX_i64 = 9223372036854775807n;
Expand Down Expand Up @@ -109,5 +110,5 @@ export function escape_id_part(id: RecordIdValue): string {
? escape_ident(id)
: typeof id === "bigint" || typeof id === "number"
? escape_number(id)
: JSON.stringify(id);
: toSurrealqlString(id);
}
28 changes: 14 additions & 14 deletions src/util/to-surrealql-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import {
} from "../data";

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

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

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

Expand All @@ -38,27 +38,27 @@ export function toSurrealqlString(input: unknown): string {
switch (Object.getPrototypeOf(input)) {
case Object.prototype: {
let output = "{ ";
for (const [k, v] of Object.entries(input as object)) {
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)},`;
const entries = Object.entries(input as object);
for (const [i, [k, v]] of entries.entries()) {
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)}`;
if (i < entries.length - 1) output += ", ";
}
output += " }";
return output;
}
case Map.prototype: {
let output = "{ ";
for (const [k, v] of (input as Map<unknown, unknown>).entries()) {
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)},`;
const entries = Array.from((input as Map<unknown, unknown>).entries());
for (const [i, [k, v]] of entries.entries()) {
output += `${JSON.stringify(k)}: ${toSurrealqlString(v)}`;
if (i < entries.length - 1) output += ", ";
}
output += " }";
return output;
}
case Array.prototype: {
let output = "[ ";
for (const v of input as unknown[]) {
output += `${toSurrealqlString(v)},`;
}
output += " ]";
return output;
const array = (input as unknown[]).map(toSurrealqlString);
return `[ ${array.join(", ")} ]`;
}
case Set.prototype: {
const set = new Set([...(input as [])].map(toSurrealqlString));
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/__snapshots__/jsonify.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ exports[`jsonify matches snapshot 1`] = `
"id_looks_like_number": "⟨some:thing⟩:⟨123⟩",
"null": null,
"num": 123,
"range": "r"bla:a"..z",
"range_unbounded": "..z",
"range": "r"bla:a"..s"z"",
"range_unbounded": "..s"z"",
"rid": "⟨some:thing⟩:under_score",
"rng_rid": "bla:a..z",
"str_rid": "⟨some:thing⟩:under_score",
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/recordid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ describe("record ids", () => {
);

// Objects and arrays
expect(new RecordId("table", { city: "London" }).toString()).toBe(
'table:{"city":"London"}',
);
expect(
new RecordId("table", {
city: "London",
date: new Date("2024-10-02T08:35:48.715Z"),
}).toString(),
).toBe('table:{ "city": s"London", "date": d"2024-10-02T08:35:48.715Z" }');

expect(new RecordId("table", ["London"]).toString()).toBe(
'table:["London"]',
'table:[ s"London" ]',
);
});
});

0 comments on commit b3e4228

Please sign in to comment.