Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure PreparedQuery has replacer context #296

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cbor/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Reader } from "./reader";
import { Tagged } from "./tagged";
import { infiniteBytes } from "./util";

interface DecodeOptions {
export interface DecodeOptions {
map?: "object" | "map";
replacer?: Replacer;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cbor/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PartiallyEncoded } from "./partial";
import { Tagged } from "./tagged";
import { Writer } from "./writer";

interface EncoderOptions<Partial extends boolean> {
export interface EncoderOptions<Partial extends boolean> {
replacer?: Replacer;
writer?: Writer;
partial?: Partial;
Expand Down
6 changes: 3 additions & 3 deletions src/cbor/partial.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Replacer } from "./constants";
import { encode } from "./encoder";
import { type EncoderOptions, encode } from "./encoder";
import { CborFillMissing } from "./error";
import type { Fill, Gap } from "./gap";
import { Writer } from "./writer";
Expand Down Expand Up @@ -41,12 +41,12 @@ export class PartiallyEncoded {

export function partiallyEncodeObject(
object: Record<string, unknown>,
fills?: Fill[],
options?: EncoderOptions<true>,
): Record<string, PartiallyEncoded> {
return Object.fromEntries(
Object.entries(object).map(([k, v]) => [
k,
encode(v, { fills, partial: true }),
encode(v, { ...options, partial: true }),
]),
);
}
9 changes: 8 additions & 1 deletion src/surreal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from "./types.ts";

import { type Fill, partiallyEncodeObject } from "./cbor";
import { replacer } from "./data/cbor.ts";
import { HttpEngine } from "./engines/http.ts";
import { WebsocketEngine } from "./engines/ws.ts";
import {
Expand Down Expand Up @@ -432,7 +433,13 @@ export class Surreal {
): Promise<Prettify<MapQueryResult<T>>> {
const params =
q instanceof PreparedQuery
? [q.query, partiallyEncodeObject(q.bindings, b as Fill[])]
? [
q.query,
partiallyEncodeObject(q.bindings, {
fills: b as Fill[],
replacer: replacer.encode,
}),
]
: [q, b];

await this.ready;
Expand Down
5 changes: 4 additions & 1 deletion src/util/PreparedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
encode,
partiallyEncodeObject,
} from "../cbor";
import { replacer } from "../data/cbor";

export type ConvertMethod<T = unknown> = (result: unknown[]) => T;
export class PreparedQuery<
Expand All @@ -15,7 +16,9 @@ export class PreparedQuery<

constructor(query: string, bindings?: Record<string, unknown>, convert?: C) {
this.query = new Encoded(encode(query));
this.bindings = partiallyEncodeObject(bindings ?? {});
this.bindings = partiallyEncodeObject(bindings ?? {}, {
replacer: replacer.encode,
});
}

build(gaps?: Fill[]): ArrayBuffer {
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/tests/querying.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ describe("template literal", async () => {
const res = await surreal.query(query, [gap.fill(789)]);
expect(res).toStrictEqual([[123, 456, 789]]);
});

test("has replacer context", async () => {
const id = new RecordId("test", 123);
const query = surql`RETURN ${id}`;
const res = await surreal.query(query);
expect(res).toStrictEqual([id]);
});
});

test("query", async () => {
Expand Down
Loading