Skip to content

Commit

Permalink
feat(postgres): Support nested sql fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasb committed Nov 6, 2023
1 parent 7e5686b commit 79a201e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/postgres/src/sql-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const validCases = [
[123, 'admin'],
],
},
{
input: (() => {
const column = fragment`foo`;
const filter = fragment`${column} = ${123}`;
return sqlTemplate`SELECT ${column} FROM table WHERE ${filter}`;
})(),
output: ['SELECT foo FROM table WHERE foo = $1', [123]],
},
];

describe('sql', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/postgres/src/sql-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Primitive = string | number | boolean | undefined | null;
export interface QueryFragment {
[fragmentSymbol]: true;
strings: TemplateStringsArray;
values: Primitive[];
values: (Primitive | QueryFragment)[];
}

const fragmentSymbol = Symbol('fragment');
Expand Down Expand Up @@ -68,7 +68,7 @@ function processTemplate(
*/
export function fragment(
strings: TemplateStringsArray,
...values: Primitive[]
...values: (Primitive | QueryFragment)[]
): QueryFragment {
if (!isTemplateStringsArray(strings) || !Array.isArray(values)) {
throw new VercelPostgresError(
Expand Down

0 comments on commit 79a201e

Please sign in to comment.