diff --git a/src/index.ts b/src/index.ts index 0252345..e4b485b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,4 +3,4 @@ export * from "./lib/find"; export * from "./lib/insert"; export * from "./lib/types"; export * from "./lib/update"; -export { pgFn } from "./lib/util"; +export { pgFn, raw } from "./lib/util"; diff --git a/src/lib/util.ts b/src/lib/util.ts index c073e59..7d46154 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -23,6 +23,11 @@ export function formatValue(value: unknown): unknown { ) .join(", ")})`; } + + if (typeof value === "string" && value.startsWith("RAW_FLAG:")) { + return value.replace("RAW_FLAG:", ""); + } + return typeof value === "string" && !valueRegex.test(value) ? `'${escapeStringLiteral(value)}'` : value; @@ -135,3 +140,35 @@ export function pgFn( return `${functionName}(${formattedArgs})`; } + +/** + * Constructs a raw SQL query string from template literals. + * + * This function takes a template literal and its values, and constructs a raw SQL query string. + * It adds a `RAW_FLAG:` prefix to indicate that the resulting string is a raw SQL query. + * + * @param {TemplateStringsArray} strings - The template strings array. + * @param {...unknown} values - The values to be interpolated into the template strings. + * @returns {string} The constructed raw SQL query string prefixed with `RAW_FLAG:`. + * + * @example + * const params: FindManyParams = { + * table: "employee", + * query: { + * select: { + * id: true, + * first_name: true, + * last_name: true, + * }, + * where: { + * id: { + * IN: raw`(SELECT id from users WHERE type = 'employee')`, + * }, + * }, + * limit: 10, + * }, + * }; + */ +export function raw(strings: TemplateStringsArray, ...values: unknown[]) { + return `RAW_FLAG:${strings.reduce((acc, str, i) => acc + str + (values[i] || ""), "")}`; +} diff --git a/src/test/index.test.ts b/src/test/index.test.ts index 027292d..d0455f1 100644 --- a/src/test/index.test.ts +++ b/src/test/index.test.ts @@ -26,4 +26,8 @@ describe("index", () => { it("should export pgFn function", () => { expect(index.pgFn).toBe(index.pgFn); }); + + it("should export raw function", () => { + expect(index.raw).toBe(index.raw); + }); }); diff --git a/src/test/util.test.ts b/src/test/util.test.ts index f79d38d..8527c1b 100644 --- a/src/test/util.test.ts +++ b/src/test/util.test.ts @@ -6,6 +6,7 @@ import { formatValue, pgFn, quoteIdentifier, + raw, } from "../lib/util"; describe("formatValue", () => { @@ -292,3 +293,26 @@ describe("pgFn", () => { expect(result).toBe('CONCAT("firstName", \' \', "lastName")'); }); }); + +describe("raw", () => { + it("should return a raw SQL string with interpolated values", () => { + const type = "admin"; + const result = raw`SELECT * FROM users WHERE type = ${type}`; + expect(result).toBe("RAW_FLAG:SELECT * FROM users WHERE type = admin"); + }); + + it("should handle multiple values", () => { + const result = raw`SELECT * FROM users WHERE id IN (${1}, ${2}, ${3})`; + expect(result).toBe("RAW_FLAG:SELECT * FROM users WHERE id IN (1, 2, 3)"); + }); + + it("should handle empty values", () => { + const result = raw`SELECT * FROM users WHERE id = ${undefined}`; + expect(result).toBe("RAW_FLAG:SELECT * FROM users WHERE id = "); + }); + + it("should handle no values", () => { + const result = raw`SELECT * FROM users`; + expect(result).toBe("RAW_FLAG:SELECT * FROM users"); + }); +});