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

feat: allow raw sql as value #9

Merged
merged 1 commit into from
Jul 24, 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/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
37 changes: 37 additions & 0 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] || ""), "")}`;
}
4 changes: 4 additions & 0 deletions src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
24 changes: 24 additions & 0 deletions src/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
formatValue,
pgFn,
quoteIdentifier,
raw,
} from "../lib/util";

describe("formatValue", () => {
Expand Down Expand Up @@ -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");
});
});