-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support static placeholders with
sql
template
- Loading branch information
Showing
4 changed files
with
72 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { Primitive } from "./types"; | ||
|
||
export function sqlTemplate( | ||
strings: TemplateStringsArray, | ||
...values: Primitive[] | ||
): [string, Primitive[]] { | ||
if (!isTemplateStringsArray(strings) || !Array.isArray(values)) { | ||
throw new Error("[db0] invalid template invokation"); | ||
} | ||
|
||
const staticIndexes: number[] = []; | ||
|
||
let result = strings[0] || ""; | ||
for (let i = 1; i < strings.length; i++) { | ||
if (result.endsWith("{") && strings[i].startsWith("}")) { | ||
result = result.slice(0, -1) + values[i - 1] + strings[i].slice(1); | ||
staticIndexes.push(i - 1); | ||
continue; | ||
} | ||
result += `?${strings[i] ?? ""}`; | ||
} | ||
|
||
const dynamicValues = values.filter((_, i) => !staticIndexes.includes(i)); | ||
|
||
return [result.trim(), dynamicValues]; | ||
} | ||
|
||
function isTemplateStringsArray( | ||
strings: unknown, | ||
): strings is TemplateStringsArray { | ||
return ( | ||
Array.isArray(strings) && "raw" in strings && Array.isArray(strings.raw) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { sqlTemplate } from "../src/template"; | ||
|
||
describe("SQL Template", () => { | ||
const tests = [ | ||
{ | ||
sql: sqlTemplate`SELECT * FROM {${"users"}} WHERE age > ${25} AND type = ${"test"}`, | ||
query: "SELECT * FROM users WHERE age > ? AND type = ?", | ||
values: [25, "test"], | ||
}, | ||
{ | ||
sql: sqlTemplate`INSERT INTO {${"users"}} ({${"name"}}, {${"age"}}) VALUES (${25}, ${"test"})`, | ||
query: "INSERT INTO users (name, age) VALUES (?, ?)", | ||
values: [25, "test"], | ||
}, | ||
]; | ||
|
||
for (const test of tests) { | ||
const testName = `${test.query} (${test.values.join(", ")}))`; | ||
it(testName, () => { | ||
expect(test.sql).toEqual([test.query, test.values]); | ||
}); | ||
} | ||
}); |